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
  • 📊 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

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.2.0.tar.gz (67.4 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.2.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_rdf-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e1d9055cb6957f85957af6855fe7d99ded59025e2ac4fa35a5ca8e922ec9117f
MD5 deef38a7eddb78362b9316c4de8ba16e
BLAKE2b-256 9a5a1231b6e90cff4ddb6fa44c159593af737b45dc1d4738c63faa33ebe0e0be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_rdf-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 603b3b62b00970655c87bc4b7b3fa415679ffb585d4b8605127ecb4d20f1ad5c
MD5 a5ceabaa404e0d7d0e2ede80e4e8bdc4
BLAKE2b-256 51e4ad85698626ec4d6d8ee3cd569a78191ef7ab1d6d729a346dadd6d445429f

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