Pydantic2

A powerful AI framework with structured Pydantic response handling, LLM integration, and advanced agent capabilities.

Python
Pydantic
LiteLLM
Instructor
OpenRouter
SmoLAgents
Django
FastAPI

Pydantic2

Pydantic2 is a powerful AI framework that provides structured response handling through Pydantic models, seamless LLM integration, and advanced agent capabilities. It's designed to make working with language models more efficient and type-safe.

Key Features

  • Structured Responses

    • Type-safe responses using Pydantic models
    • Automatic validation and parsing
    • IDE support with autocompletion
    • Custom response models with field descriptions
    • Nested model support
  • LLM Integration 🔌

    • Support for multiple LLM providers
    • Unified API for all models
    • Easy provider switching
    • Automatic retries and fallbacks
    • Streaming support
  • Budget Control 💰

    • Built-in cost tracking
    • Budget limits per request/user
    • Usage statistics and analytics
    • Cost estimation before requests
    • Detailed usage reports
  • Message Handling 📝

    • System and user messages
    • Conversation history
    • Structured data support
    • Support for code blocks
    • Support for JSON and DataFrame inputs
  • Agent System 🛠️

    • Custom tools and functions
    • Gradio UI integration
    • Extensible framework
    • Tool decorators
    • Memory management

Quick Start

from pydantic import BaseModel, Field
from typing import List
from pydantic2 import LiteLLMClient, Request

class MovieReview(BaseModel):
    title: str = Field(description="The title of the movie")
    rating: float = Field(description="The rating of the movie")
    pros: List[str] = Field(description="The pros of the movie")
    cons: List[str] = Field(description="The cons of the movie")

client = LiteLLMClient(Request(
    model="openrouter/openai/gpt-4o-mini-2024-07-18",
    answer_model=MovieReview
))

response = client.generate_response(
    prompt="Review the movie 'Inception'"
)

print(f"Title: {response.title}")
print(f"Rating: {response.rating}/5")
print("Pros:", ", ".join(response.pros))
print("Cons:", ", ".join(response.cons))

Why Pydantic2?

  • Type Safety: Get structured responses with proper type hints and validation
  • Efficiency: Reduce boilerplate code and focus on your application logic
  • Reliability: Production-tested with comprehensive error handling
  • Flexibility: Support for multiple LLM providers and frameworks
  • Scalability: Built for both small projects and enterprise applications
  • Cost Control: Built-in budget management and usage tracking
  • Framework Support: Seamless integration with Django, FastAPI, and more
  • Developer Experience: Great IDE support and documentation

Tech Stack

  • Pydantic: Type-safe data handling
  • LiteLLM: Core LLM routing
  • Instructor: Structured outputs
  • OpenRouter: Default model provider
  • SmoLAgents: Agent functionality

Links