from good_ass_pydantic_integrator import GAPI
gapi = GAPI(class_name="User")
gapi.add_object_from_dict(
{
"id": "3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a",
"name": "Ada",
"created_at": "2025-01-01T12:00:00Z",
"tags": ["admin", "early-access"],
}
)
print(gapi.get_pydantic_model_content())
# ───────────────────────── Printed output ─────────────────────────
from typing import TYPE_CHECKING
from pydantic import AwareDatetime, BaseModel, ConfigDict
if TYPE_CHECKING:
from uuid import UUID
class User(BaseModel):
model_config = ConfigDict(extra="forbid")
id: UUID
name: str
created_at: AwareDatetime
tags: list[str]
Overview
Good Ass Pydantic Integrator (GAPI) is a python library that generates Pydantic v2 models from raw JSON data (or JSON schemas), lets you customize the result, and provides a base client that automatically regenerates models when the schema changes.
Why
datamodel-code-generator is an excellent tool for generating Pydantic schemas, but it was missing some features that are added by GAPI.
-
No directory input support for JSON files: While not explicitly stated in the documentation, attempting to process a directory of JSON files will raise an error.
-
No string-to-object parsing: When using JSON files as input, it doesn't detect objects saved as strings that could be parsed into more complex types like datetime, date, time, timedelta, IPv4Address, IPv6Address, UUID, etc.
GAPIClient
GAPIClient is a base class that builds Pydantic models from sets of JSON files. It can be used to create models for API endpoints that regenerate automatically when the schema changes, keeping them in sync without any manual updates.
Usage
GAPI is the foundation for the following API projects: