Skip to main content

Bridge between Pydantic V2 models and RDF graphs

Project description

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_rdf-0.1.0.tar.gz (63.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_rdf-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_rdf-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_rdf-0.1.0.tar.gz
  • Upload date:
  • Size: 63.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.17

File hashes

Hashes for pydantic_rdf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 faf6983d8a4c6418c8555875d533e070d41542275711306acf4ccf9ebab3a01d
MD5 62a91b7927a56015d82bca1d2ef0bc79
BLAKE2b-256 2017a43597f58ac1cd7b539f11bc09666e9456b0ebec09d26699f08105567362

See more details on using hashes here.

File details

Details for the file pydantic_rdf-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_rdf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb6c8e48e93468dfa3fdd17e3c610aca8e0d8cc01af6d226b7bbb50b432cb2e3
MD5 c72c17967696c8e1f881bb462a6c4c7c
BLAKE2b-256 999322c28df5c8c6bc54ed3604dbf6394ea4db9f06b97e8ad70d8dc437d362e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page