Skip to main content
Version: latest

Schema file format

This page lists every key and every short form that load_schema accepts. For a worked example, see Define a graph schema.

load_schema(source, *, extends=None) returns a GraphSchema. dump_schema(schema) returns the full form of a schema as a dict. load_schema(dump_schema(schema)) returns an equal schema.

Where a schema can come from​

SourceExample
A GraphSchemaload_schema(schema)
A dictload_schema({"entities": ["Person"]})
A list of labels and relationsload_schema(["Person", "Vendor", "Person-WORKS_FOR->Vendor"])
JSON textload_schema('{"entities": ["Person"]}')
A .json, .yaml or .yml fileload_schema("schema.yaml")
A Turtle .ttl file (install the schema-formats extra)load_schema("ontology.ttl")

Top-level keys​

KeyMeaningDefault
nameA short name for the schema.custom, or the file name without its suffix.
versionThe schema version. Raise it when types or patterns change. A number is read as text. Quote 1.10."1"
extendsThe base schema. See Build on a base schema.none
excludeBase types or relations to remove.none
entitiesThe entity types.none
relationsThe relation types.none

Any other key is an error. The error names the closest valid key.

Entity types​

You can write entities in these forms:

FormExample
A list of labels[Person, Vendor]
A list of objects[{label: Vendor, description: A seller., parents: [Organization]}]
A label-to-description map{Vendor: A seller.}
A label-to-object map{Vendor: {description: A seller., parents: [Organization]}}

An entity object has these keys:

KeyMeaningDefault
labelThe label. Leave it out in a map form: the key is the label.required
descriptionOne short sentence for the extractor.empty
parentsThe labels this type narrows. A single label is a list of one. A type inherits the patterns of its parents.none
propertiesThe typed properties.none
overridetrue replaces a base type of the same label.false

Properties​

FormExample
A list of names[email, phone] (type string)
A name-to-type map{born: integer}
A name-to-object map{status: {type: string, required: true, enum: [open, shipped]}}
A list of objects[{name: born, type: integer}]

A property object has these keys:

KeyMeaningDefault
typeOne of string, integer, float, boolean or date.string
requiredWhether an entity of this type should carry the property.false
enumAllowed values as text. Each value must be a valid form of type.none
descriptionGuidance for the extractor.empty

Relation types​

You can write relations in these forms:

FormExample
A list of strings["Person-WORKS_FOR->Organization"]
A list of strings with alternatives["Person|Organization-OWNS->Software|Dataset"] (four patterns)
A list of triples (Python only)[("Person", "WORKS_FOR", "Organization")]
A list of objects[{label: OWNS, description: A person owns software., patterns: ["Person->Software"]}]
A label-to-patterns map{OWNS: "Person->Software"}
A label-to-object map{OWNS: {description: Ownership., patterns: ["Person->Software"]}}

Strings and triples with the same label join into one relation.

A relation object has these keys:

KeyMeaningDefault
labelThe label. Letters, digits and underscores only.required
descriptionOne short sentence.empty
patternsThe allowed pairs. Write "Source->Target" or [source, target].required
add_patternsPairs to add to a base relation.none
cardinality, symmetricThe limits that the schema model defines.model default
overridetrue replaces a base relation of the same label.false

Build on a base schema​

extends names the base. It can be a path, a dict, a GraphSchema, a Preset or the names default, operations and legal. You can set it in the file or in the call, but not both. If you leave it out, your schema stands alone and replaces the default schema.

RuleBehavior
Add a new labelAllowed.
Declare a label that the base hasError, unless the item has override: true.
override: trueReplaces the whole base item. The new item keeps the base position. The loader logs a warning.
Loosen a constraint in an overrideError. You cannot drop a property, change its type, make a required property optional, widen its enum, or raise a cardinality limit. You can change patterns.
exclude: [Label]Removes a base type or relation.
Relations that use an excluded typeThe loader removes the patterns that name it. It removes a relation that has no pattern left.
A base type that has an excluded type as a parentError. Exclude the child as well.
A new relation that names an excluded typeError.
add_patternsAdds pairs to a base relation. Do not combine it with patterns or override.

Settings are not part of a schema​

Thresholds, the model name and the data type are extraction settings. Set them in ExtractionSettings. A schema key with one of these names is an error. A schema stays the same when you tune the extractor.

Errors​

load_schema raises SchemaLoadError for every invalid source. The message lists every problem, one per line, with the path to the key:

invalid schema in vendors.yaml (2 errors):
entities[2].parents[0]: parent 'Organizaton' is not a declared entity type. Did you mean 'Organization'?
relations[0]: cannot read 'Person WORKS_FOR Org' as a relation. Write it as 'Source-LABEL->Target', for example 'Person-WORKS_FOR->Organization'.

SchemaLoadError.issues holds the same problems as SchemaIssue(path, message) values. An item that came from the base schema has the path extends.<Label>. A file that cannot be read or parsed raises one error at once.