Flat tables in. Typed object graphs out.¶
graphty turns flat rows from SQL, SPARQL, CSV or any dataframe into nested, validated Pydantic objects.
You describe the shape, graphty builds it.
Flat data and a model
import json
from pydantic import BaseModel
from graphty import ConfigDict, ModelMaterializer
rows = [
{"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},
]
class Work(BaseModel):
title: str
year: int
class Author(BaseModel):
model_config = ConfigDict(group_by="name")
name: str
works: list[Work]
authors = ModelMaterializer(model=Author, data=rows).generate_models()
print(json.dumps([author.model_dump() for author in authors], indent=4))
The result, serialized as JSON