A Python library that bridges Pydantic V2 models and RDF graphs, enabling seamless bidirectional conversion between typed data models and semantic web data. PydanticRDF combines Pydantic's powerful validation with RDFLib's graph capabilities to simplify working with linked data.
- ✅ Type Safety: Define data models with Pydantic V2 and map them to RDF graphs
- 🔄 Serialization: Convert Pydantic models to RDF triples with customizable predicates
- 📥 Deserialization: Parse RDF data into validated Pydantic models
- 🧩 Complex Structures: Support for nested models, lists, and circular references
- 📊 JSON Schema: Generate valid JSON schemas from RDF models with proper URI handling
pip install pydantic-rdf
from rdflib import Namespace
from pydantic_rdf import BaseRdfModel, WithPredicate
from typing import Annotated
# Define a model
EX = Namespace("http://example.org/")
class Person(BaseRdfModel):
rdf_type = EX.Person
_rdf_namespace = EX
name: str
age: int
email: Annotated[str, WithPredicate(EX.emailAddress)]
# Create and serialize
person = Person(uri=EX.person1, name="John Doe", age=30, email="[email protected]")
graph = person.model_dump_rdf()
# The resulting RDF graph looks like this:
# @prefix ex: <http://example.org/> .
# @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#
# ex:person1 a ex:Person ;
# ex:age 30 ;
# ex:emailAddress "[email protected]" ;
# ex:name "John Doe" .
# Deserialize
loaded_person = Person.parse_graph(graph, EX.person1)
- Python 3.11+
- pydantic >= 2.11.3
- rdflib >= 7.1.4
For complete documentation, visit https://omegaice.github.io/pydantic-rdf/
MIT