Models and fields
SPARQLModel subclasses Pydantic v2
pydantic.BaseModel. Field types and constraints are validated on construction and when
loading from the graph (hydration). For patterns and the validation stack, see
Models and Pydantic validation.
ORM entity base class (SPARQLModel); use with SPARQLSession.
- class sparqlmodel.model.SPARQLModelMetaclass(cls_name, bases, namespace, __pydantic_generic_metadata__=None, __pydantic_reset_parent_namespace__=True, _create_model_module=None, **kwargs)[source]
Bases:
ModelMetaclassMetaclass enabling
Model.field == valuequery expressions.- Return type:
- class sparqlmodel.model.SPARQLModel(*, id=None)[source]
Bases:
TripleModelORM entity mapped to RDF; persist and query via
SPARQLSession.- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'forbid', 'str_strip_whitespace': False, 'validate_assignment': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- classmethod get_prefixes()[source]
Return namespace prefixes for this model (includes built-in RDF prefixes).
- classmethod iter_sparql_fields()[source]
Yield (name, field_info, annotation) for mapped fields excluding id.
- subject_uri(*, uri=None)[source]
Return the RDF subject IRI for this instance (expanded when compact).
- Return type:
- model_dump_jsonld()[source]
Serialize to a JSON-LD dict for APIs (cascade-aware ORM view).
For RDF files or HTTP bodies with all triples, use
serialize(format="json-ld")(inherited from TripleModel) instead.
- classmethod model_validate_jsonld(data)[source]
Deserialize from a JSON-LD dict (ORM presentation layer).
For RDF files, use
parse()(inherited from TripleModel) orimport_graph().- Return type:
ORM field and relationship definitions for SPARQLModel.
- sparqlmodel.fields.Field(predicate, *, lang=None, inverse=None, literal_datatype=None, transitive=False, back_populates=None, **kwargs)[source]
Map a model attribute to an RDF predicate.
- Parameters:
predicate (str) – Compact or absolute IRI (e.g.
schema:name).lang (str | None) – Default language tag for string literals (
Langmetadata).inverse (str | None) – Inverse predicate IRI for import-only triples.
literal_datatype (str | None) – XSD datatype CURIE for typed literals.
transitive (bool) – Expand multi-valued object URIs transitively on import.
back_populates (Any) –
BackPopulates,inverse_pair, or(Model, field)tuple.**kwargs (Any) – Additional arguments passed to
pydantic.Field.
- Return type:
- sparqlmodel.fields.Relationship(predicate, *, model=None, cascade=True, inverse=None, back_populates=None, **kwargs)[source]
Map a model attribute to an RDF object relationship.
- Parameters:
predicate (str) – Compact or absolute IRI (e.g.
schema:worksFor).model (type[Any] | None) – Related
SPARQLModelclass (inferred from annotation when omitted).cascade (bool) – When
False, nested resources are not included in put/delete cascade.inverse (str | None) – Inverse predicate for import (not with
ref_field).back_populates (Any) – Paired inverse field on another model (TripleModel 0.12+).
**kwargs (Any) – Additional arguments passed to
pydantic.Field.
- Return type:
- class sparqlmodel.fields.SPARQLFieldMetadata(predicate, is_relationship=False, related_model=None, cascade=True)[source]
Bases:
objectMetadata attached to a SPARQL-mapped model field.
- __init__(predicate, is_relationship=False, related_model=None, cascade=True)
- sparqlmodel.fields.field_cardinality_for(field_info)[source]
TripleModel cardinality for a mapped field.
- Return type:
- sparqlmodel.fields.get_field_metadata(field_info)[source]
Extract SPARQL metadata from a Pydantic field info object.
- Return type:
SPARQLFieldMetadata | None
- sparqlmodel.fields.inverse_pair(model, field)[source]
Shorthand for
BackPopulates.Pass a class or a string name (
inverse_pair("Organization", "employees")) when the peer model is declared later in the same module.- Return type:
- sparqlmodel.fields.is_relationship_field(field_info, meta)[source]
True when the field maps to object references or nested models.
- Return type:
- sparqlmodel.fields.iter_relationship_values(value)[source]
Normalize scalar or collection relationship values to a sequence.
- sparqlmodel.fields.predicate_uri_for_field(field_info, prefixes)[source]
Expanded predicate IRI for a mapped field (TripleModel or SparqlModel metadata).
- Return type:
str | None
- sparqlmodel.fields.relationship_allows_iri(annotation)[source]
True when the relationship annotation includes
IRI(composition or reference).- Return type:
- sparqlmodel.fields.relationship_is_nullable(annotation)[source]
True when a relationship annotation includes
None(optional link).- Return type:
- sparqlmodel.fields.relationship_is_ref_link(field_info)[source]
True when the field is a URI reference (
ref_field), not embedded composition.- Return type:
IRI and namespace utilities.
- sparqlmodel.types.is_compact_iri(value)[source]
Return True if value is a prefix:local compact IRI (not a plain literal with a colon).
- Return type:
- sparqlmodel.types.is_absolute_iri(value)[source]
Return True if
valueis an absolute IRI string (http, https, or urn).- Return type:
- class sparqlmodel.types.IRI[source]
Bases:
strRDF IRI identifier (compact or absolute).
- sparqlmodel.types.expand_iri(iri, prefixes=None)[source]
Expand compact IRIs using prefix map.
- Return type:
- sparqlmodel.types.compact_iri(iri, prefixes=None)[source]
Compact an absolute IRI when a known prefix matches.
- Return type: