Vocabulary API¶
The semantic layer (pycharter.semantic.concepts) provides types, parsers, and validators for field bindings, concept vocabularies, and lineage. Field bindings can be stored with data contracts and used for validation and governance.
See the Vocabulary 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/concepts/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/concepts/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[FieldRelationship, ...] = (),
)
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[FieldRelationship, ...]
|
Relationships to other fields. |
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/semantic/concepts/models.py
Concept
¶
Bases: BaseModel
The canonical concept model (VC-5).
Promoted from the concept-graph ConceptNode — the one shape
carrying tier, slots, origin, and provenance that
governance and the contribution flywheel require — absorbing the
SKOS extras (alt_labels, examples, exact_match,
tags, replaced_by) as optional fields.
concept_key is the stable identifier used for cross-artifact
references; id is the database UUID and is None until the
concept is persisted. Document serialization (ontologies.yaml
/ persisted concept_scheme artifacts, format "1") keeps its
historical keys via :meth:to_document / :meth:from_document —
the document format is a stable configuration contract; only the
in-memory model changed.
from_document
classmethod
¶
from_document(data: dict[str, Any]) -> Concept
Build a canonical concept from an ontologies.yaml entry.
The document keys (id, label, definition,
broader) predate VC-5 and are a stable configuration
contract — they map onto the canonical field names here.
Source code in src/pycharter/semantic/concepts/models.py
to_document
¶
Serialize to the ontologies.yaml entry shape.
Round-trip safe with the YAML schema. tier is carried in the
document (only when non-default) so an explicit governance tier
survives save/load and reaches the persisted concept row that
bind_field matching consults. The remaining canonical-only
fields (slots, origin, provenance, timestamps) are not part of
the document format and are intentionally omitted.
Source code in src/pycharter/semantic/concepts/models.py
ConceptScheme
dataclass
¶
ConceptScheme(
id: str,
version: str,
description: str = "",
base_uri: str | None = None,
namespace: str | None = None,
category: str = "domain",
concepts: list[Concept] = list(),
relationships: list[Relationship] = list(),
)
A complete concept scheme (vocabulary).
The single vocabulary artifact (D6): what used to be a separate
ConceptSchemeTemplate kind is now a scheme whose category is
pattern. Concepts and their edges live side by side, so a starter
vocabulary and a governed one have exactly the same shape.
Mutable because concepts are appended during parsing.
to_dict
¶
Convert to dictionary.
The top-level format_version is the persisted artifact format
(DG-2), distinct from scheme.version which is the vocabulary's
content version.
Source code in src/pycharter/semantic/concepts/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
¶
Bases: BaseModel
A typed, directed relationship between two concepts (VC-5 canonical).
Promoted from the concept-graph ConceptEdge: the single
relationship shape shared by the graph API, the store mappers, and
the codecs.
Enumerations¶
Concept tiers and types are metaschema-driven since VC-6 — the
sets live in data/seed/vocabulary_metaschema.yaml and are served by
the loader functions below (no Python enums).
get_tiers
¶
get_concept_types
¶
validate_tier
¶
validate_concept_type
¶
RelationshipType
¶
Bases: StrEnum
Canonical relationship vocabulary (CF-6).
EQUIVALENT_TO
class-attribute
instance-attribute
¶
Strict identity with another concept (SKOS exactMatch / OWL ≡).
CLOSE_MATCH
class-attribute
instance-attribute
¶
Near identity (SKOS closeMatch).
RELATED_TO
class-attribute
instance-attribute
¶
General semantic relationship (SKOS related).
BROADER
class-attribute
instance-attribute
¶
The other concept is broader (SKOS broader).
NARROWER
class-attribute
instance-attribute
¶
The other concept is narrower (SKOS narrower).
SAME_AS
class-attribute
instance-attribute
¶
Logical identity across vocabularies (OWL sameAs).
REFERENCES
class-attribute
instance-attribute
¶
Foreign-key style pointer.
DERIVED_FROM
class-attribute
instance-attribute
¶
Computed from another concept or field.
IMPLEMENTS
class-attribute
instance-attribute
¶
Concrete realization of another concept.
DESCRIBES
class-attribute
instance-attribute
¶
Property / attribute of another concept.
Parsing¶
parse_field_bindings
¶
parse_field_bindings(
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/concepts/parser.py
parse_field_bindings_file
¶
parse_field_bindings_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/concepts/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/concepts/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/concepts/validator.py
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 181 182 183 184 185 186 187 188 189 190 191 192 | |
Taxonomy¶
classify_concept
¶
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 |
|---|---|
str
|
The governance tier for this concept |
Source code in src/pycharter/semantic/concepts/taxonomy.py
register_core_concept
¶
register_core_concept(
name: str,
tier: str,
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
|
str
|
Governance tier |
required |
broader
|
str | None
|
Parent concept name (optional) |
None
|
description
|
str
|
Human-readable description |
''
|
Source code in src/pycharter/semantic/concepts/taxonomy.py
See also¶
- Vocabulary guide
- Contract Builder — contracts include the
field_bindingsartifact - Contract Store —
get_field_bindings/store_field_bindingsby contract name and version
Vocabulary facade (VC-7)¶
One entry point for vocabulary work — schemes, concepts, suggestions, bindings, health, and context packs behind a single object:
Vocabulary
¶
Vocabulary(
store: SemanticStoreClient,
*,
session: Any | None = None,
)
Facade over the vocabulary services (VC-7).
Construct with a connected :class:SemanticStoreClient (and, for
the session-backed services, a SQLAlchemy session), or use
:meth:from_url to wire both from a connection string.
Source code in src/pycharter/vocabulary.py
schemes
property
¶
schemes: SchemeArtifacts
Versioned concept-scheme artifacts (save/get/list/set_status).
from_url
classmethod
¶
from_url(connection_string: str) -> Vocabulary
Build a connected facade from a database URL.
Supports the semantic store's SQLite and PostgreSQL backends; the same connection powers both the store surfaces and the session-backed services.
Source code in src/pycharter/vocabulary.py
health
¶
Construct the semantic-health service bound to this session.
Source code in src/pycharter/vocabulary.py
calibration
¶
Fit reliability weights from recorded suggestion decisions (VC-12).
Source code in src/pycharter/vocabulary.py
suggestions
¶
Construct the AI suggestion pipeline (provider from config/env).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calibrated
|
bool
|
When True, fit :func: |
False
|
**kwargs
|
Any
|
Forwarded to :class: |
{}
|
Source code in src/pycharter/vocabulary.py
context
¶
context(
anchor_key: str,
*,
scheme_id: str,
depth: int = 2,
token_budget: int = 2000,
**kwargs: Any,
) -> ContextPack
Build a token-efficient context pack around an anchor concept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor_key
|
str
|
|
required |
scheme_id
|
str
|
UUID of the scheme whose graph is read. |
required |
depth
|
int
|
Traversal depth around the anchor. |
2
|
token_budget
|
int
|
Approximate token ceiling for the pack. |
2000
|
**kwargs
|
Any
|
Forwarded to
:func: |
{}
|
Source code in src/pycharter/vocabulary.py
SchemeArtifacts
¶
SchemeArtifacts(store: SemanticStoreClient)
Versioned-artifact verbs over concept-scheme documents.
Reached as :attr:SemanticStoreClient.schemes; holds no state of
its own beyond the owning store reference.
Source code in src/pycharter/semantic_store/_scheme_artifacts.py
save
¶
save(
name: str,
version: str,
document: dict[str, Any],
*,
created_by: str | None = None,
sync_graph: bool = True,
) -> str
Persist a scheme document snapshot (and sync the live graph).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Scheme key (exact; no normalization is applied). |
required |
version
|
str
|
Content version string. |
required |
document
|
dict[str, Any]
|
|
required |
created_by
|
str | None
|
Audit author. |
None
|
sync_graph
|
bool
|
When True (default) the live concept graph is upserted to match the saved document. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Snapshot ID. |
Raises:
| Type | Description |
|---|---|
ArtifactExistsError
|
On a duplicate |
UnsupportedFormatVersion
|
For an unknown |
Source code in src/pycharter/semantic_store/_scheme_artifacts.py
get
¶
Return the envelope for a snapshot (latest when unpinned).
Source code in src/pycharter/semantic_store/_scheme_artifacts.py
list
¶
list(name: str | None = None) -> list[dict[str, Any]]
Return envelope summaries (no document bodies).
set_status
¶
Set a snapshot's lifecycle status.