TripleModel

Pydantic models for RDF graphs. Map typed Python classes to RDF triples in a pyoxigraph-backed Store and back — without hand-writing triples for every field.

Person(slug="alice", name="Alice")  →  (ex:alice, foaf:name, "Alice")  →  Person(...)

PyPI

pip install triplemodel

Python

3.10+

Status

Beta (0.12.0)

Features

  • Pydantic v2 with declarative RDF mapping (class Rdf, rdf_field, Predicate)

  • Multi-valued fieldsset[T] for multiple objects per predicate; list[T] for ordered rdf:List

  • Language tags & opaque literalsLangString, MultiLangString, Lang(), OpaqueLiteral, ResourceRef

  • Typed literals per objectTypedLiteral, TypedLiteralList on set / list fields

  • Ontology hintsOntologyRegistry, apply_hints_to_model (owl:inverseOf, subclasses)

  • Paired inverse metadataBackPopulates, inverse_pair, subjects_via_back_populates

  • Nested models — embed child TripleModel resources (IRI or experimental blank node)

  • Sync modessync_to_graph and to_graph(..., mode=) (add, replace, patch)

  • Prefixes & CURIEsRdf.prefixes, compact predicates, Turtle PREFIX output

  • Literal registryDecimal, UUID, Enum, and custom types

  • Graph helpersmerge_graphs, graph_value, graph_set, objects_for_field

SparqlModel (session / ORM layer) is planned to build on TripleModel — see TripleModel and SparqlModel — separation of responsibilities.

Indices