Namespaces and CURIEs

Compact prefix:local predicates keep models readable and produce Turtle PREFIX lines when you serialize the graph.

Declare prefixes on the model

from triplemodel.vocab import FOAF

class Person(TripleModel):
    class Rdf:
        namespace = "http://example.org/people/"
        type_uri = f"{FOAF}Person"
        id_field = "slug"
        prefixes = {"foaf": str(FOAF)}

    slug: str
    name: str = rdf_field("foaf:name")
    nick: list[str] = rdf_field("foaf:nick", default_factory=list)

list[str] maps to an rdf:List, not multiple foaf:nick triples. Use set[str] for several objects on one predicate — see RDF lists and language-tagged literals.

On to_graph() / module-level model_to_graph() / sync_to_graph() with a new graph, prefixes are bound automatically when bind=True (default for sync on empty graphs). Instance sync_to_graph() does not take bind=; use module-level sync_to_graph(..., bind=False) when needed.

CURIE expansion

Module helper:

from triplemodel import expand_curie

print(expand_curie("foaf:name", {"foaf": "http://xmlns.com/foaf/0.1/"}))

Output:

http://xmlns.com/foaf/0.1/name

Absolute IRIs pass through unchanged. Unknown prefixes raise ValueError.

Turtle output

graph = person.to_graph()
ttl = graph.serialize(format="turtle")
# PREFIX foaf: <http://xmlns.com/foaf/0.1/> ...

For file round-trips, prefer person.serialize(format="turtle") and Person.parse_file("person.ttl") — see File I/O (parse and serialize).

Manual bind on an existing graph

from triplemodel import bind_namespaces

bind_namespaces(graph, {"foaf": "http://xmlns.com/foaf/0.1/"}, strategy="core")

strategy

Effect

"core"

graph.bind(prefix, namespace) for each entry

"none"

No-op

Pass bind=False to module-level sync_to_graph or model_to_graph when merging into a graph that already has prefix bindings.

Vocabulary shortcuts

from triplemodel.vocab import FOAF, DCTERMS, SKOS, OWL, RDFS, XSD

These are bundled namespace helpers so you can write f"{FOAF}name" in full-IRI form and still add prefixes = {"foaf": str(FOAF)} for CURIE fields.

Next: Custom literals →