Skip to content

pydantic_rdf ¤

PydanticRDF - Bridge between Pydantic V2 models and RDF graphs.

This library allows you to define data models with Pydantic's powerful validation while working with semantic web technologies using rdflib. It enables seamless serialization between Pydantic models and RDF graphs, and vice versa.

Example
from rdflib import Namespace
from pydantic_rdf import BaseRdfModel, WithPredicate
from typing import Annotated

# Define a namespace
EX = Namespace("http://example.org/")

# Define your model
class Person(BaseRdfModel):
    rdf_type = EX.Person  # RDF type for this model
    _rdf_namespace = EX   # Default namespace for properties

    name: str
    age: int
    email: Annotated[str, WithPredicate(EX.emailAddress)]  # Custom predicate

Modules:

Classes:

  • BaseRdfModel

    Base class for RDF-mappable Pydantic models.

  • WithDataType

    Annotation to specify a custom RDF datatype for a field.

  • WithPredicate

    Annotation to specify a custom RDF predicate for a field.

BaseRdfModel pydantic-model ¤

Bases: BaseModel

Base class for RDF-mappable Pydantic models.

Config:

  • arbitrary_types_allowed: True

Fields:

  • uri (PydanticURIRef)

uri pydantic-field ¤

uri: PydanticURIRef

The URI identifier for this RDF entity

all_entities classmethod ¤

all_entities(graph: Graph) -> list[T]

Return all entities of this model's RDF type from the graph.

Returns:

  • list[T]

    A list of model instances for each entity of this RDF type in the graph.

Raises:

Example
entities = MyModel.all_entities(graph)

model_dump_rdf ¤

model_dump_rdf() -> Graph

Serialize this model instance to an RDF graph.

Returns:

  • Graph

    An RDFLib Graph representing this model instance.

Example
graph = instance.model_dump_rdf()

parse_graph classmethod ¤

parse_graph(graph: Graph, uri: URIRef, _cache: RDFEntityCache | None = None) -> T

Parse an RDF entity from the graph into a model instance.

Uses a cache to prevent recursion and circular references.

Parameters:

  • _cache (RDFEntityCache | None, default: None ) –

    Optional cache for already-parsed entities.

Returns:

  • T

    An instance of the model corresponding to the RDF entity.

Raises:

Example
model = MyModel.parse_graph(graph, EX.some_uri)

WithDataType dataclass ¤

WithDataType(data_type: URIRef)

Annotation to specify a custom RDF datatype for a field.

This annotation allows you to define a specific RDF datatype to use when serializing a model field to RDF literals, instead of using the default datatype inference.

Parameters:

  • data_type (URIRef) –

    The RDF datatype URI to use for this field

Example
from typing import Annotated
from pydantic_rdf import BaseRdfModel, WithDataType
from rdflib.namespace import XSD

class Product(BaseRdfModel):
    # This will use xsd:decimal datatype instead of the default
    price: Annotated[float, WithDataType(XSD.decimal)]

Methods:

  • extract

    Extract from field annotation if present.

extract classmethod ¤

extract(field) -> URIRef | None

Extract from field annotation if present.

WithPredicate dataclass ¤

WithPredicate(predicate: URIRef)

Annotation to specify a custom RDF predicate for a field.

This annotation allows you to define a specific RDF predicate to use when serializing a model field to RDF, instead of using the default predicate generated from the field name.

Parameters:

  • predicate (URIRef) –

    The RDF predicate URI to use for this field

Example
from typing import Annotated
from pydantic_rdf import BaseRdfModel, WithPredicate
from rdflib import Namespace

EX = Namespace("http://example.org/")

class Person(BaseRdfModel):
    # This will use the EX.emailAddress predicate instead of the default EX.email
    email: Annotated[str, WithPredicate(EX.emailAddress)]

Methods:

  • extract

    Extract from field annotation if present.

extract classmethod ¤

extract(field) -> URIRef | None

Extract from field annotation if present.