Ontology API¶
The semantic layer (pycharter.semantic.ontology) provides types, parsers, and validators for field-level semantic annotations, concept vocabularies, and lineage. Field mappings (ontology) can be stored with data contracts and used for validation and governance.
See the Ontology guide for the conceptual overview.
Core models¶
FieldMapping
dataclass
¶
FieldMapping(version: str, domain_schema: str | None = None, field_bindings: dict[str, FieldBinding | FieldConceptBinding] = dict())
A versioned field mapping artifact for a data contract.
Maps data contract fields to ontology concepts. Supports two formats:
- New format:
domain_schema+field_bindingsas FieldBinding - Legacy format:
field_bindingsas FieldConceptBinding
Attributes:
| Name | Type | Description |
|---|---|---|
version |
str
|
Field mapping version string. |
domain_schema |
str | None
|
Reference to a published LinkML schema
in |
field_bindings |
dict[str, FieldBinding | FieldConceptBinding]
|
Mapping of field path to its semantic annotation. Values may be FieldBinding (new) or FieldConceptBinding (legacy). |
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/semantic/ontology/models.py
FieldBinding
dataclass
¶
FieldBinding(class_name: str, slot_name: str, tags: tuple[str, ...] = (), owner: str | None = None, lineage: Lineage | None = None)
Binds a physical schema field to a LinkML class and slot.
This is the class/slot-centric replacement for FieldConceptBinding. Used by the new ontology artifact format that references a published LinkML domain schema.
Attributes:
| Name | Type | Description |
|---|---|---|
class_name |
str
|
LinkML class name the field belongs to. |
slot_name |
str
|
LinkML slot name within that class. |
tags |
tuple[str, ...]
|
Additional field-level tags. |
owner |
str | None
|
Team or person owning this field. |
lineage |
Lineage | None
|
Data lineage information. |
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/semantic/ontology/models.py
FieldConceptBinding
dataclass
¶
FieldConceptBinding(concept: str, broader: str | None = None, definition: str | None = None, tags: tuple[str, ...] = (), owner: str | None = None, description: str | None = None, deprecated: bool = False, lineage: Lineage | None = None, relationships: tuple[Relationship, ...] = ())
Semantic annotation for a single field.
Attributes:
| Name | Type | Description |
|---|---|---|
concept |
str
|
The semantic concept this field represents. |
broader |
str | None
|
Parent concept in the hierarchy (deprecated -- use ontologies.yaml). |
definition |
str | None
|
Human-readable definition (deprecated -- use ontologies.yaml). |
tags |
tuple[str, ...]
|
Tags for categorization. |
owner |
str | None
|
Team or person owning this field. |
description |
str | None
|
Field-specific business description. |
deprecated |
bool
|
Whether this field mapping is deprecated. |
lineage |
Lineage | None
|
Data lineage information. |
relationships |
tuple[Relationship, ...]
|
Relationships to other fields. |
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/semantic/ontology/models.py
Concept
dataclass
¶
Concept(id: str, label: str, definition: str | None = None, broader: str | None = None, tags: tuple[str, ...] = (), alt_labels: tuple[str, ...] = (), notation: str | None = None, examples: tuple[str, ...] = (), exact_match: str | None = None, deprecated: bool = False, replaced_by: str | None = None, concept_type: str | None = None)
Full definition of a single concept in the vocabulary.
Mirrors the extended ontologies.yaml schema and can be losslessly converted to/from SKOS JSON-LD.
to_dict
¶
Convert to dictionary (round-trip safe with YAML schema).
Source code in src/pycharter/semantic/ontology/models.py
ConceptScheme
dataclass
¶
ConceptScheme(id: str, title: str, version: str, description: str = '', base_uri: str | None = None, concepts: list[Concept] = list())
A complete concept scheme (vocabulary).
Mirrors the top-level scheme: + concepts: structure in
ontologies.yaml. Mutable because concepts are appended during parsing.
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/semantic/ontology/models.py
Lineage
dataclass
¶
Tracks the origin of a field.
Attributes:
| Name | Type | Description |
|---|---|---|
derived_from |
str | None
|
Field this was derived from (if any). |
source_system |
str | None
|
System that produces this field. |
Relationship
dataclass
¶
A relationship between two fields.
Attributes:
| Name | Type | Description |
|---|---|---|
target |
str
|
Target field path (e.g., "customers.id"). |
type |
str
|
Type of relationship (string, validated against schema). |
Enumerations¶
ConceptTier
¶
Bases: StrEnum
Built-in governance tiers for concepts.
These are the default tiers shipped with pycharter. Custom tiers
can be added via the ontology schema without modifying this enum.
Use schema_loader.validate_tier() for runtime validation.
ConceptType
¶
Bases: StrEnum
Built-in concept types for the knowledge graph.
These are the default types shipped with pycharter. Custom types
can be added via the ontology schema without modifying this enum.
Use schema_loader.validate_concept_type() for runtime validation.
RelationshipType
¶
Bases: StrEnum
Built-in relationship types between fields or concepts.
These are the default types shipped with pycharter. Custom types
can be added via the ontology schema without modifying this enum.
Use schema_loader.validate_relationship_type() for runtime validation.
REFERENCES
class-attribute
instance-attribute
¶
Foreign key or reference relationship.
DERIVED_FROM
class-attribute
instance-attribute
¶
Field is computed from another field.
PRECEDES
class-attribute
instance-attribute
¶
X happens before Y (temporal ordering).
IMPLEMENTS
class-attribute
instance-attribute
¶
X realizes or is a concrete form of Y.
DESCRIBES
class-attribute
instance-attribute
¶
X is a property or attribute of Y.
EQUIVALENT_TO
class-attribute
instance-attribute
¶
X represents the same thing as Y.
Parsing¶
parse_field_mapping
¶
parse_field_mapping(ontology_data: dict[str, Any]) -> FieldMapping
Parse a field mapping dictionary into a FieldMapping.
Auto-detects whether the payload uses the new LinkML-backed format
(domain_schema + class/slot bindings) or the legacy concept-centric
format (fields + concept bindings).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_data
|
dict[str, Any]
|
Field mapping data as dictionary. |
required |
Returns:
| Type | Description |
|---|---|
FieldMapping
|
FieldMapping with parsed bindings. |
Raises:
| Type | Description |
|---|---|
OntologyError
|
If the data is invalid. |
Source code in src/pycharter/semantic/ontology/parser.py
parse_field_mapping_file
¶
parse_field_mapping_file(file_path: str) -> FieldMapping
Load and parse a field mapping file (YAML or JSON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to field mapping file. |
required |
Returns:
| Type | Description |
|---|---|
FieldMapping
|
FieldMapping with parsed bindings. |
Raises:
| Type | Description |
|---|---|
OntologyError
|
If the file cannot be read or parsed. |
Source code in src/pycharter/semantic/ontology/parser.py
Validation¶
validate_field_mapping
¶
validate_field_mapping(artifact: FieldMapping) -> list[dict[str, str]]
Validate a FieldMapping instance.
Performs the same checks as validate_field_mapping_payload but on a parsed artifact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artifact
|
FieldMapping
|
Parsed FieldMapping. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of error dicts with 'path' and 'message' keys. |
list[dict[str, str]]
|
Empty list means valid. |
Source code in src/pycharter/semantic/ontology/validator.py
validate_field_mapping_payload
¶
Validate raw field mapping data before parsing.
Checks structural requirements: - Must have 'version' field - Must have 'field_bindings' dict - Each field must have 'concept' - Relationship types must be valid - No circular broader references
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_data
|
dict[str, Any]
|
Raw field mapping dictionary. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of error dicts with 'path' and 'message' keys. |
list[dict[str, str]]
|
Empty list means valid. |
Source code in src/pycharter/semantic/ontology/validator.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
Taxonomy¶
classify_concept
¶
classify_concept(concept_name: str) -> ConceptTier
Classify a concept into a governance tier.
Core concepts get their registered tier. Unknown concepts are classified as free-form (Tier 3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
concept_name
|
str
|
Name of the concept |
required |
Returns:
| Type | Description |
|---|---|
ConceptTier
|
The governance tier for this concept |
Source code in src/pycharter/semantic/ontology/taxonomy.py
register_core_concept
¶
register_core_concept(name: str, tier: ConceptTier, broader: str | None = None, description: str = '') -> None
Register a new core concept in the taxonomy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Concept name |
required |
tier
|
ConceptTier
|
Governance tier |
required |
broader
|
str | None
|
Parent concept name (optional) |
None
|
description
|
str
|
Human-readable description |
''
|
Source code in src/pycharter/semantic/ontology/taxonomy.py
See also¶
- Ontology guide
- Contract Builder — contracts can include
field_mapping(ontology) - Contract Store —
get_field_mapping/store_field_mappingby contract name and version