GitHunt
OM

Omegaice/pydantic-rdf

A bridge between Pydantic V2 models and RDF graphs

PydanticRDF

github
PyPI
Python
docs
license

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.

Features

  • โœ… 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

Installation

pip install pydantic-rdf

Quick Example

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="john@example.com")
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 "john@example.com" ;
#     ex:name "John Doe" .

# Deserialize
loaded_person = Person.parse_graph(graph, EX.person1)

Requirements

  • Python 3.11+
  • pydantic >= 2.11.3
  • rdflib >= 7.1.4

Documentation

For complete documentation, visit https://omegaice.github.io/pydantic-rdf/

License

MIT

Contributors

MIT License
Created April 26, 2025
Updated January 14, 2026