Pydantic Models for LLM Requests
What you'll learn: How to use Pydantic models to validate and structure incoming LLM API requests with type safety and automatic error handling.
Why Pydantic for LLM Requests?
When building AI APIs (as introduced in your previous FastAPI lessons), you need to validate that incoming requests contain proper data before sending them to expensive LLM providers. Pydantic models act as strict contracts — think of them as blueprints that ensure every request has the right shape, valid values, and required fields.
Defining a Request Schema
Here's a typical LLM request model:
from pydantic import BaseModel, Field
from typing import Optional
class LLMRequest(BaseModel):
prompt: str = Field(..., min_length=1, max_length=5000)
temperature: float = Field(default=0.7, ge=0.0, le=2.0)
max_tokens: int = Field(default=256, gt=0, le=4096)
model: str = Field(default="gpt-3.5-turbo")
system_message: Optional[str] = None
What's happening:
promptis required and must be 1-5000 characterstemperaturedefaults to 0.7 but must be between 0.0-2.0max_tokenshas range validation (1-4096)modelhas a sensible defaultsystem_messageis optional
FastAPI automatically validates requests against this model. Invalid data? Users get clear error messages before you waste an API call.
The Power of Validation
Without Pydantic, you'd write dozens of if statements checking bounds and types. Pydantic handles this declaratively — you describe what's valid, and it enforces it automatically. This prevents common errors like negative temperatures or empty prompts from reaching your LLM provider.
Key Takeaway: Pydantic models provide type-safe, declarative request validation for LLM APIs, catching invalid parameters before expensive API calls and giving users clear feedback automatically.