Skip to main content

tests coverage Ruff uv

graphty is a Python library for materializing Pydantic object graphs from relational data.

WARNING: This project is in an early stage of development and should be used with caution.

The core idea of graphty is to utilize Pydantic models as a declarative DSL for building Polars query plans.

Instead of writing imperative data transformation code - group this, aggregate that, nest this inside that - users should be able to define the shape of what they want as a Pydantic model hierarchy; and graphty figures out the respective materialization from flat tabular data.

graphty interprets Pydantic models as DataFrame transformation specifications.

Introduction

The graphty library addresses the structural impedance mismatch between flat relational data representations and hierarchical object models. It extends Pydantic with a small declarative DSL for expressing grouping, aggregation, and deduplication operations. These transformations are compiled into Polars expressions, yielding records that are subsequently validated and materialized as Pydantic model objects.

Although originally developed for implementing typed REST APIs over SPARQL endpoints, the model materializer can be applied to any tabular data representation, including SQL query results, CSV files, dataframes, etc.

Installation

graphty is a PEP 621-compliant package and available on PyPI.

Usage

As mentioned, graphty uses Pydantic model definitions as declarative data transformation instructions, extending Pydantic with a small DSL for grouping and aggregation.

Nested models are resolved recursively, list types are interpreted as aggregation targets and require a group_by definition in ConfigDict.

Basic Example

Given simple relational Author/Work data

data = [
    {"name": "Tolkien", "title": "The Hobbit", "year": 1937},
    {"name": "Tolkien", "title": "The Lord of the Rings", "year": 1954},
    {"name": "Tolkien", "title": "The Silmarillion", "year": 1977},
    {"name": "Orwell", "title": "Animal Farm", "year": 1945},
    {"name": "Orwell", "title": "1984", "year": 1949},
]

one can define and materialize a Pydantic model like so:

from collections.abc import Iterator
from pydantic import BaseModel
from graphty import ConfigDict, ModelMaterializer

class Work(BaseModel):
    title: str
    year: int

class Author(BaseModel):
    model_config = ConfigDict(group_by="name")

    name: str
    works: list[Work]
 
models: Iterator[Author] = ModelMaterializer(model=Author, data=data).generate_models()

Here, the Author model defines a model aggregation target for the Author.works field; the graphty planner will therefore partition the underlying data according to the "name" key and aggregate Work objects into a list.

Note that graphty is recursive on all code paths and ergo enables materialization of arbitrarily nested and aggregated object graphs.

The above validates against the Author model and serializes to the following JSON representation:

[
    {
        "name": "Tolkien",
        "works": [
            {
                "title": "The Hobbit",
                "year": 1937
            },
            {
                "title": "The Lord of the Rings",
                "year": 1954
            },
            {
                "title": "The Silmarillion",
                "year": 1977
            }
        ]
    },
    {
        "name": "Orwell",
        "works": [
            {
                "title": "Animal Farm",
                "year": 1945
            },
            {
                "title": "1984",
                "year": 1949
            }
        ]
    }
]

Metadata

Release files for graphty 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphty 0.6.0
File Size Uploaded
graphty-0.6.0.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for graphty 0.6.0
File Interpreter ABI Platform
graphty-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / graphty-0.6.0.tar.gz

Download URL graphty-0.6.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ecc066b4b099393c89d7250e336914e5f9d55381cb06c7f09d0ced9c24395976
BLAKE2b-256 checksum
How to use checksums
625401f7256e7fe28224a63f5ae3732f7f3965566aabcece3b9b2c375329abb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.11","id":"zokor","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / graphty-0.6.0-py3-none-any.whl

Download URL graphty-0.6.0-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2fe6954b18b332a98767724b9cd13a798a7a2293b334cc3d1ea2cbcd88ed9755
BLAKE2b-256 checksum
How to use checksums
a0afdc017ea1dcfff3cd5757c987d73ed80bfe3b08287fb35f62f4218e8ec6e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.11","id":"zokor","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

0.7.1

2 release files

0.7.0

2 release files

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page