Registry / serialization / rdflib

rdflib

JSON →
library7.6.0pypypi✓ verified 29d ago

RDFLib is a Python library for working with RDF (Resource Description Framework), a simple yet powerful language for representing information. It provides a Graph data structure, parsers and serializers for various RDF formats (e.g., Turtle, N-Triples, RDF/XML, JSON-LD), and SPARQL query capabilities. RDFLib is actively maintained with frequent minor releases and major versions typically every 1-2 years.

pip install rdflib
INSTALL
IMPORT
SIG · RDFLIB
R
rdflib
serializationpythonv7.6.0
Install
2.3s avg
Import
300ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v7.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.320s · 27.2MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 2.3s · import 0.280s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Graph
✓ from rdflib import Graph
Namespace
✓ from rdflib import Namespace
URIRef
✓ from rdflib import URIRef
BNode
✓ from rdflib import BNode
Literal
✓ from rdflib import Literal
FOAF
✓ from rdflib.namespace import FOAF
✗ from rdflib import FOAF
Commonly used namespaces like FOAF, RDFS, RDF, XSD are found in `rdflib.namespace`.

This quickstart demonstrates how to create an RDF Graph, define namespaces, add triples using URIRef, BNode, and Literal, and serialize the graph to the Turtle format. It's a fundamental example of how to represent and manage RDF data programmatically.

from rdflib import Graph, Literal, Namespace, URIRef from rdflib.namespace import FOAF, XSD # Create a Graph g = Graph() # Define a custom namespace EX = Namespace("http://example.org/people/") # Create URIs for resources john = EX.johnDoe jane = EX.janeDoe # Add triples to the graph g.add((john, FOAF.name, Literal("John Doe"))) g.add((john, FOAF.mbox, URIRef("mailto:john.doe@example.org"))) g.add((john, FOAF.age, Literal(30, datatype=XSD.integer))) g.add((jane, FOAF.name, Literal("Jane Doe"))) g.add((jane, FOAF.age, Literal(25, datatype=XSD.integer))) # Serialize the graph to Turtle format and print it print(g.serialize(format="turtle")) # You can also parse from a file or string # g.parse("path/to/my_data.ttl", format="turtle")
rdfpipe --version
Debug
Known issues
breakingThe handling of the `publicID` parameter in `Graph.parse` and `Dataset.parse` methods changed in version 7.0.0. It now explicitly sets the base URI for parsing, potentially altering how relative URIs are resolved in some cases.
fix
Review calls to `Graph.parse` or `Dataset.parse` that use `publicID`. If your data contains relative URIs, ensure the `publicID` is correctly set to define the base URI, or use `base` parameter explicitly if appropriate.
affects: >=7.0.0
deprecatedSeveral methods and attributes of the `Dataset` class have been deprecated in version 7.3.0 and are scheduled for removal in the next major release (likely 8.0.0). This aims to improve consistency and align `Dataset` more closely with RDF specifications.
fix
Consult the RDFLib documentation for `Dataset` to identify deprecated methods and their recommended alternatives. Update your code to use the newer, more consistent API to avoid breakage in future major versions.
affects: >=7.3.0
gotchaMany parsers, serializers, and advanced features (e.g., SPARQL, JSON-LD, HTML5 parsing, specific datatype handling) rely on optional dependencies that are not installed by default. Without them, you might encounter `ImportError` or incomplete functionality.
fix
Install RDFLib with the necessary extras, e.g., `pip install "rdflib[json-ld,sparql,isodate,lxml,rdf4j,graphdb]"`, or install individual missing packages as needed (e.g., `pip install isodate`). Refer to the documentation for specific feature requirements.
affects: All versions
gotchaPython 3.8 support was temporarily removed in version 7.1.2 and then re-added in 7.1.3 due to a typing-related regression. Users on specific patch versions of 7.1.x might experience unexpected behavior or installation issues on Python 3.8.
fix
Ensure you are using RDFLib >= 7.1.3 if you target Python 3.8. It's generally recommended to use the latest patch release for your major version.
affects: 7.1.2
Errors
Common errors & fixes
rdflib.exceptions.ParserError: Error parsing RDF
The input data (string or file) provided to the `Graph.parse()` method is not valid RDF in the specified format, or the format was incorrectly identified.
fix
Ensure your RDF input string or file strictly adheres to the specified RDF format (e.g., Turtle, N-Triples, RDF/XML, JSON-LD) and is well-formed. Double-check the `format` argument passed to `g.parse()`.
rdflib.plugins.sparql.parser.ParseError: Expected {SelectQuery, ConstructQuery, DescribeQuery, AskQuery}
The SPARQL query string provided to `Graph.query()` has a syntax error and cannot be parsed by RDFLib's SPARQL engine.
fix
Review and correct the syntax of your SPARQL query to ensure it conforms to the SPARQL 1.1 grammar. Common mistakes include missing keywords (like `SELECT` or `WHERE`), incorrect curly brace placement, or malformed prefixes.
TypeError: can only concatenate str (not URIRef) to str
You are attempting to concatenate an `rdflib.URIRef` (or `Literal`, `BNode`) object directly with a Python string without explicitly converting the RDFLib object to a string first.
fix
Convert the `URIRef` or `Literal` object to a string using `str()` before concatenating it with other strings, or use f-strings which handle conversion automatically.
ImportError: 'pyld' must be installed to use JSON-LD serialiser/parser
RDFLib uses the external `pyld` library for JSON-LD parsing and serialization, but this dependency is not installed in your Python environment.
fix
Install the `pyld` package using pip: `pip install pyld`. If you also need other optional parsers/serializers, check their respective documentation for required dependencies.
Upgrade
Version history
7.6.0latest on PyPI · released Feb 13, 2026
Audit
Dependencies
isodateoptionalRequired for parsing and serializing XSD date/time literals.
lxmloptionalRequired for faster RDF/XML parsing and validation, and for some XML-based serialization formats.
html5rdfoptionalRequired for parsing RDF embedded in HTML using RDFa or Microdata.
pyparsingoptionalUsed for SPARQL parsing. Often installed as a transitive dependency.
rdflib-jsonldoptionalProvides JSON-LD parsing and serialization support.
rdflib-sparqlstoreoptionalEnables connection to remote SPARQL endpoints as an RDFLib Store.
rdf4j-clientoptionalEnables integration with RDF4J servers and stores (introduced in 7.5.0).
graphdb-clientoptionalEnables integration with GraphDB instances (introduced in 7.6.0).
Agent activity
3 hits · last 30 days
node
2
Resources
rdflib — pip install rdflib · libregistry