Course contentsShow
← AI Engineering
Lesson 975 of 1,88624. Building AI APIsFree lesson

Validating LLM API Requests with Pydantic

Use Pydantic models to validate incoming request structure, types, and constraints before hitting LLM endpoints.

Validating LLM API Requests with Pydantic

You'll learn how to use Pydantic models to catch invalid requests before they waste time, money, or LLM tokens.

Why Validation Matters

When users send requests to your LLM API, they might provide malformed data: missing fields, wrong types, or values outside acceptable ranges. Without validation, these bad requests could trigger expensive LLM calls that fail, produce nonsense, or worse—expose security vulnerabilities. Pydantic acts as your API's bouncer, rejecting invalid requests at the door.

The Core Pattern

You've already learned to define Pydantic request models (step 965). Now you leverage their validation superpowers to enforce constraints before hitting LLM endpoints:

from pydantic import BaseModel, Field, field_validator
from fastapi import FastAPI, HTTPException

class ChatRequest(BaseModel):
    message: str = Field(..., min_length=1, max_length=1000)
    temperature: float = Field(default=0.7, ge=0.0, le=2.0)
    max_tokens: int = Field(default=150, ge=1, le=4000)
    
    @field_validator('message')
    @classmethod
    def message_not_empty_whitespace(cls, v):
        if not v.strip():
            raise ValueError('Message cannot be only whitespace')
        return v.strip()

app = FastAPI()

@app.post("/chat")
async def chat(request: ChatRequest):
    # If we reach here, request is GUARANTEED valid
    # Call LLM knowing inputs are safe and constrained
    ...

Think of it like this: Pydantic is a type-checking guard with a clipboard at a concert venue. It checks tickets (field types), ensures ages meet requirements (constraints), and turns away troublemakers—all before they reach the expensive venue inside (your LLM).

What Happens Automatically

FastAPI + Pydantic automatically returns 422 Unprocessable Entity with detailed error messages when validation fails. No bad data reaches your expensive LLM calls.

Key Takeaway: Use Pydantic field constraints and custom validators to enforce data quality at your API boundary—preventing wasted LLM calls and ensuring predictable behavior.