KLR-Pattern/pydantic-resolve

pydantic-resolve is a progressive data assembly framework which follows the stye of clean architecture

What it solves

pydantic-resolve is a data-assembly framework that prevents the "N+1 query problem" and decouples API response models from database schemas. In traditional FastAPI projects, developers often manually fetch related data in routes or services, leading to repetitive code and inefficient database queries. This framework provides a structured way to define how related data should be loaded and computed, ensuring that data is fetched in batches regardless of the depth of the response tree.

How it works

The framework uses a progressive layering system based on Clean Architecture:

  • Resolvers (resolve_*): You define methods on Pydantic models to declare which fields need external data. The framework collects these requests across the entire response tree and uses Loaders to fetch them in a single batch query.
  • Post-processors (post_*): These methods run after all nested dependencies are resolved, making them ideal for calculating derived fields like counts or sums.
  • ER Diagrams: For complex projects, you can centralize relationship definitions in an Entity-Relationship (ER) diagram. This allows the framework to automatically load relationships (AutoLoad) without writing individual resolver methods for every view.
  • Coordination Helpers: ExposeAs allows parents to pass context down to children, while Collector and SendTo allow children to aggregate data back up to parents.

Who it’s for

Python developers building APIs (especially with FastAPI) who deal with complex nested data relationships and want to avoid the performance pitfalls of N+1 queries while maintaining a clean separation between their database layer and their API contract.

Highlights

  • N+1 Safe: Built-in batch loading ensures efficient data retrieval.
  • Clean Architecture: Strictly separates Enterprise rules (Entities), Application rules (Resolvers), and Interface Adapters (Loaders).
  • Progressive Adoption: You can start with simple resolvers and move to ER Diagrams and automated loading as the project grows.
  • AI Agent Ready: Includes native support for the Model Context Protocol (MCP), allowing AI agents to query your data graph via GraphQL-like interfaces.
  • Multi-ORM Support: Can import relationship metadata from SQLAlchemy, Django, and Tortoise ORM.

Related

  • Project
  • Project
  • Project
  • Project
  • Project