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

Response Models and Serialization

Structure LLM responses with Pydantic models including generated text, tokens used, and metadata for clean APIs.

Response Models and Serialization

You'll learn how to structure LLM API responses with Pydantic models to deliver clean, predictable data that includes generated text, usage metrics, and metadata.

What This Means

When your LLM endpoint generates a response, you're not just sending back raw text—you're packaging a rich data structure. Response models use Pydantic (which you've already used for requests) to define exactly what your API returns: the generated content, token counts, model information, timestamps, and any other metadata clients need.

Think of it like a shipping receipt: the customer gets their product (the generated text), but they also receive a detailed invoice showing what was used, when, and at what cost. This transparency makes debugging, billing, and monitoring effortless.

Structuring LLM Responses

Here's a typical response model:

from pydantic import BaseModel, Field
from datetime import datetime

class LLMResponse(BaseModel):
    generated_text: str = Field(..., description="The LLM's output")
    tokens_used: int = Field(..., description="Total tokens consumed")
    prompt_tokens: int
    completion_tokens: int
    model: str = Field(..., description="Model identifier used")
    timestamp: datetime = Field(default_factory=datetime.utcnow)
    request_id: str

Your FastAPI endpoint automatically serializes this:

@app.post("/generate", response_model=LLMResponse)
async def generate(request: LLMRequest):
    # Call LLM, get response
    return LLMResponse(
        generated_text=llm_output,
        tokens_used=total_tokens,
        prompt_tokens=prompt_tokens,
        completion_tokens=completion_tokens,
        model="gpt-4",
        request_id="abc123"
    )

FastAPI validates and converts this to clean JSON automatically—no manual dictionary wrangling.

Key Takeaway: Response models turn messy LLM outputs into structured, validated contracts that make your API professional, debuggable, and easy to consume.