Skip to main content

SPARQL ORM for Python — sessions, queries, and graph persistence on RDF stores

Project description

SparqlModel

PyPI version Python Documentation License: MIT

The SQLModel of SPARQLPydantic v2 entity models mapped to RDF, a persistent session, and Python filters that compile to SPARQL.

Build knowledge-graph and metadata apps with typed SPARQLModel classes, with SPARQLSession() as session:, and ORM-style put, get, nested relationships, and a query builder — on in-memory graphs or remote SPARQL 1.1 endpoints. Same validation ergonomics as FastAPI and SQLModel: invalid data fails at construction and on load, before bad triples reach the store.

Requires Python 3.10+ · Built on TripleModel 0.10 + pyoxigraph · Changelog (0.5.2)


Features

Area What you get
Models SPARQLModel, Field, Relationship, IRIPydantic v2 validation (model_validate, constraints, extra="forbid")
RDF mapping rdf_type, compact predicates, TripleModel sync_to_graph / from_graph under the hood
Session add, put, delete, get, identity map, flush / pending queue
Queries session.query(Person).where(Person.name == "x") → SPARQL (&, |, in_, comparisons, multi-hop)
Stores MemoryStore (default), HttpStore for Fuseki/Jena-style endpoints
FastAPI SessionDep, http_store_lifespan, Turtle/JSON-LD responses
Cascade Composition on put/delete; Relationship(..., cascade=False) for references

Install

pip install sparqlmodel
pip install "sparqlmodel[http]"      # HttpStore (httpx)
pip install "sparqlmodel[fastapi]"   # FastAPI session + RDF responses
pip install -e ".[dev,http,fastapi]" # development

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)

with SPARQLSession() as session:
    session.put(odos)

    found = session.query(Person).where(Person.name == "Odos").first()
    team = session.query(Person).where(Person.works_for.name == "Acme Corp").all()
    full = session.get(Person, odos.id, depth=1)

Pydantic models

SPARQLModel subclasses pydantic.BaseModel. You get the same advantages as in FastAPI or SQLModel: typed fields, IDE support, and validation on create and on load.

When What runs
Person(...) / API body Pydantic validates types and Field constraints
session.put(model) Validated instance → sync_to_graph (0.4+: same SPARQLModel instance subclasses TripleModel)
session.get / query hydration Graph → model_validateSPARQLModel instance
# Field forwards pydantic.Field kwargs (min_length, ge, description, …)
class Person(SPARQLModel):
    rdf_type = "schema:Person"
    __prefixes__ = {"schema": "https://schema.org/"}
    id: IRI
    name: str = Field("schema:name", min_length=1)
  • extra="forbid" — unknown fields on a model raise at validation time (safer for APIs).
  • FastAPI — reuse the same SPARQLModel classes for request/response bodies (see FastAPI below).
  • JSON-LDmodel_dump_jsonld() / model_validate_jsonld() on each model (serializers delegate to TripleModel; prefer TripleModel load_graph / dump_graph for file I/O).

Details: Models guide · ORM guide


Session

SPARQLSession is the unit of work. Use it as a context manager: flush pending writes on success, roll back the pending queue on error, close HTTP stores when done.

Method Purpose
add(model) Append triples (no delete of existing subject data)
put(model) Upsert with cascade and orphan cleanup
delete(model) Remove owned triples for root + composition tree
get(Model, iri, depth=0) Load one resource; depth 0–2 eager-loads relationships
query(Model).where(...) Fluent query; filters compile to SPARQL
execute(sparql) Raw SPARQL SELECT (auto-prefixes when configured)
flush() / rollback_pending() Apply or discard put(..., flush=False) queue
expire(Model, iri) Evict identity map and hydration cache

Nested SPARQLModel values are composition (cascade on put/delete). Use Relationship(..., cascade=False) or an IRI when the target is owned elsewhere.


Query DSL

with SPARQLSession() as session:
    session.query(Person).where(Person.name == "Odos").all()

    session.query(Person).where(
        (Person.name == "Odos") | (Person.name == "Ada")
    ).all()

    session.query(Person).where(
        Person.works_for.located_in.name == "Boston"
    ).all(depth=2)

    session.query(Person).where(Person.name.in_(("Odos", "Ada"))).all()

    session.query(Person).where(Person.name != "Other").all()
    # pre-0.5.2 inequality (excludes unbound): .use_inequality_for_ne()

Operators: ==, !=, &, |, <, >, <=, >=, .in_(tuple) (also accepts lists), multi-hop paths (Person.works_for.name), .limit(n), .use_inequality_for_ne(), .use_optional_for_comparisons().


Stores

MemoryStore (default) — in-memory triplemodel.Store (pyoxigraph); tests and single-process apps:

with SPARQLSession() as session:
    session.put(model)

HttpStore — SPARQL 1.1 over HTTP with a local mirror for get and cascade (sparqlmodel[http]):

from sparqlmodel import HttpStore, SPARQLSession

with SPARQLSession(store=HttpStore("http://localhost:3030/ds/sparql")) as session:
    session.put(odos)

query / execute use the remote endpoint; get and cascade read the mirror updated by this store’s writes. See the production guide for mirror semantics and deployment notes.


FastAPI

Per-request sessions with a shared store — same pattern as SQLModel + SQLAlchemy:

from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException, Request
from sparqlmodel import IRI
from sparqlmodel.fastapi import SessionDep, http_store_lifespan, negotiated_response

@asynccontextmanager
async def lifespan(app: FastAPI):
    async with http_store_lifespan(app, "http://localhost:3030/ds/sparql"):
        yield

app = FastAPI(lifespan=lifespan)

@app.get("/person/{iri}")
def person(iri: str, request: Request, session: SessionDep) -> object:
    model = session.get(Person, IRI(iri))
    if model is None:
        raise HTTPException(status_code=404)
    return negotiated_response(request, model)

Export

from sparqlmodel.serializers import export_model

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

Long term, file I/O moves to TripleModel parse / serialize; see the roadmap.


Documentation

Guide Description
Read the Docs Full site: install, guides, API reference, troubleshooting
Getting started Quickstart and first session
Guides Models (Pydantic), sessions, queries, FastAPI
ORM guide Lifecycle, cascade, hydration, when to use SparqlModel vs TripleModel
Technical specification Normative API; production checklist
Production guide HttpStore, sessions, deployment
Roadmap 0.5–1.3 milestones; SQLModel parity
Project plan Vision and release strategy
Ecosystem SparqlModel vs TripleModel boundaries

Known limitations (0.5.2)

  • Multi-valued predicates: first value per predicate on load; prefer put over add for upserts
  • HttpStore: mirror may lag behind the remote dataset for get / cascade (production mirror sync planned 1.0)
  • No async session or AsyncHttpStore yet — planned 0.6 (roadmap)
  • Query: limit only — offset / order_by / count planned 0.8 (roadmap)
  • session.graph is a triplemodel.Store (pyoxigraph), not an rdflib Graph — use TripleModel I/O for file round-trip
  • Default != uses NOT EXISTS (includes resources with no value); use .use_inequality_for_ne() for pre-0.5.2 inequality semantics
  • Multi-hop != still requires relationship hops (inner-join); missing works_for is not treated as “null name”
  • Sessions are not thread-safe; one session per request/task
  • Each model field must map to a unique RDF predicate; duplicate predicates raise ConfigurationError at class definition
  • Cyclic embedded models raise ConfigurationError on put / model_to_graph
  • Shared embedded resources referenced from multiple roots are preserved on put when another subject still links to them

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.5.2.tar.gz (38.2 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.5.2-py3-none-any.whl (42.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sparqlmodel-0.5.2.tar.gz
  • Upload date:
  • Size: 38.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sparqlmodel-0.5.2.tar.gz
Algorithm Hash digest
SHA256 50c72559fbcb409b902942293f9e88db6699550cd7f14ab7c80924fb29c1da0e
MD5 1e1687dcafc42bcdf0473901b70bdc2d
BLAKE2b-256 08539d1fe5f3dbe0e6cc3a697a1eee34b1424ee6ce306733e7835109b84384ae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sparqlmodel-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 42.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sparqlmodel-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c22bc938fa50c476adf2053617c8a240749ac9da555a14b6e31b9119aba74fa8
MD5 80b81f8b8464d9ff57f4cff65c70b1c9
BLAKE2b-256 9665ba53f8fbbe031c1444b05a528c9d6f243103ca6b75f7f61bd1b3d7c3a14a

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