Skip to main content

SPARQL-native object graph mapper for RDF triple stores

Project description

SparqlModel

SPARQL-native object graph mapper for RDF triple stores.

SparqlModel brings SQLModel-style ergonomics to RDF: typed Pydantic models, Pythonic queries that compile to SPARQL, in-memory persistence via RDFLib, and JSON-LD serialization.

Install

pip install sparqlmodel

For development:

pip install -e ".[dev]"

Quickstart

from sparqlmodel import Field, IRI, Relationship, SPARQLModel, SPARQLSession

class Organization(SPARQLModel):
    rdf_type = "schema:Organization"
    __prefixes__ = {"schema": "https://schema.org/"}

    id: IRI
    name: str = Field("schema:name")

class Person(SPARQLModel):
    rdf_type = "schema:Person"
    __prefixes__ = {"schema": "https://schema.org/"}

    id: IRI
    name: str = Field("schema:name")
    works_for: Organization | None = Relationship(
        "schema:worksFor", model=Organization
    )

acme = Organization(id=IRI("urn:org:acme"), name="Acme Corp")
odos = Person(id=IRI("urn:person:odos"), name="Odos", works_for=acme)

session = SPARQLSession()
session.put(odos)

# Query with Python expressions
found = session.query(Person).where(Person.name == "Odos").first()
print(found.name)  # Odos

# Nested filter (single relationship hop)
team = session.query(Person).where(Person.works_for.name == "Acme Corp").all()

# Hydrate relationships
full = session.get(Person, odos.id, depth=1)
print(full.works_for.name)  # Acme Corp

# Export RDF
from sparqlmodel.serializers import export_model

print(export_model(odos, format="turtle"))

Features (0.1.x)

  • SPARQLModel — Pydantic v2 models mapped to RDF predicates
  • SPARQLSession — in-memory CRUD against an RDFLib graph
  • Query builder — session.query(Model).where(Model.field == value)
  • SPARQL compiler — scalar and single-hop nested filters
  • Graph hydration — session.get(Model, iri, depth=0|1|2)
  • Serializers — Turtle, N-Triples, RDF/XML, JSON-LD

Persistence semantics

  • put upserts the model and any embedded nested SPARQLModel values (composition). Owned triples are removed for the root, nested objects in the current tree, and relationship targets previously stored in the graph for that subject (orphan cleanup). Relationship values stored as IRI only are external references and are not cascade-deleted.
  • add inserts triples without removing existing ones; re-adding the same id can leave stale literal values.
  • delete removes owned triples for the model and cascaded embedded resources (same rules as put). Shared resources linked only by IRI reference are kept.

Query filters

  • == — exact match on a predicate value.
  • != — matches subjects that have some value for the predicate that is not equal to the right-hand side (subjects with no value are excluded).
  • Combine filters with .where(a, b) or (a) & (b).
  • Filter values cannot be None (raises QueryError).
  • Nested filters require the related resource to have the expected rdf:type in the graph.

Known limitations

  • Shared embedded resources — If the same IRI is embedded from multiple parents, put/delete on one parent can remove that resource’s triples. Use IRI references for shared entities.
  • add vs putadd never removes triples; duplicate add after in-place edits can leave stale literals. Prefer put for updates.
  • Multi-valued predicates — Only the first object per predicate is loaded; multiple != filters on multi-valued predicates follow RDF existential semantics (see SPECS).
  • Declared predicates onlyput/delete remove triples for mapped predicates plus rdf:type, not arbitrary extension triples on a subject.
  • JSON-LDmodel_dump_jsonld() / model_validate_jsonld() differ from export_model(..., format="json-ld") (RDFLib serialization). Typed literals and relationship lists are not fully supported in the custom JSON-LD path.
  • Export side effect — RDF export may assign a urn:uuid:… id via ensure_id() when id is unset.

For development, run tests from the project virtualenv: .venv/bin/pytest.

Roadmap (0.2)

  • HTTP SPARQL 1.1 endpoint store (httpx)
  • FastAPI optional integration
  • Identity map and session caching
  • Richer query compiler (OR, numeric/date comparisons)

Documentation

License

MIT — see LICENSE.

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

sparqlmodel-0.1.4.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

sparqlmodel-0.1.4-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file sparqlmodel-0.1.4.tar.gz.

File metadata

  • Download URL: sparqlmodel-0.1.4.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for sparqlmodel-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f292f651202ceff65f2755dceeaf23c20b1631189cd36c9f98722884910ee62c
MD5 0649084f173fe42f079d5ce2bfa4594d
BLAKE2b-256 5ed3b299ebd0f9dfb45e41072a635fe01f01ac9c89546dbabc75f4dc9225372a

See more details on using hashes here.

File details

Details for the file sparqlmodel-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: sparqlmodel-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for sparqlmodel-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 561f9ae53132e4f754d44e886134a071db623c56adcc1a9df7f7009a2dcb09e7
MD5 9dd2449740cf29feb32b266fa2d0f4dc
BLAKE2b-256 70689256788298775935448406b8d9b420453665c5fb1bcc42b28c8e53cbcf25

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