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 predicatesSPARQLSession— 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
putupserts the model and any embedded nestedSPARQLModelvalues (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 asIRIonly are external references and are not cascade-deleted.addinserts triples without removing existing ones; re-adding the sameidcan leave stale literal values.deleteremoves owned triples for the model and cascaded embedded resources (same rules asput). Shared resources linked only byIRIreference 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(raisesQueryError). - Nested filters require the related resource to have the expected
rdf:typein the graph.
Known limitations
- Shared embedded resources — If the same IRI is embedded from multiple parents,
put/deleteon one parent can remove that resource’s triples. UseIRIreferences for shared entities. addvsput—addnever removes triples; duplicateaddafter in-place edits can leave stale literals. Preferputfor 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 only —
put/deleteremove triples for mapped predicates plusrdf:type, not arbitrary extension triples on a subject. - JSON-LD —
model_dump_jsonld()/model_validate_jsonld()differ fromexport_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 viaensure_id()whenidis 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f292f651202ceff65f2755dceeaf23c20b1631189cd36c9f98722884910ee62c
|
|
| MD5 |
0649084f173fe42f079d5ce2bfa4594d
|
|
| BLAKE2b-256 |
5ed3b299ebd0f9dfb45e41072a635fe01f01ac9c89546dbabc75f4dc9225372a
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
561f9ae53132e4f754d44e886134a071db623c56adcc1a9df7f7009a2dcb09e7
|
|
| MD5 |
9dd2449740cf29feb32b266fa2d0f4dc
|
|
| BLAKE2b-256 |
70689256788298775935448406b8d9b420453665c5fb1bcc42b28c8e53cbcf25
|