Skip to course content
Free FastAPI backend course

FastAPI for AI Backend Development

Unit 13.02: Validating what the model returned

Model output is untrusted input that arrived from inside your own system.

Validate it like a request body

A schema constraining the category to an allowed set and the confidence to a range.

The code validates four returned objects.

from pydantic import BaseModel, Field, ValidationError
from typing import Literal


class ModelOutput(BaseModel):
    category: Literal["billing", "technical", "account", "other"]
    confidence: float = Field(ge=0, le=1)


RETURNED = [
    {"category": "billing", "confidence": 0.9},
    {"category": "Billing Department", "confidence": 0.9},
    {"category": "billing", "confidence": 1.4},
    {"category": "billing"},
]
for raw in RETURNED:
    try:
        print(f"OK    {ModelOutput(**raw)}")
    except ValidationError as exc:
        e = exc.errors()[0]
        print(f"FAIL  {str(raw)[:44]:46} {e['loc'][0]}: {e['type']}")

# Model output is untrusted input arriving from inside your own system.
# Validate it against the same kind of schema you use for requests -- a
# confidence of 1.4 will otherwise be serialised straight to your caller.

A confidence of 1.4 and a category of "Billing Department" both look plausible and would be serialised straight through to your caller - breaking the contract you published in Module 1.

Validating here means the failure becomes a 503 you control rather than a bad response the caller has to detect.

The mistake this prevents

The mistake is validating the request and trusting the response. The model is a boundary like any other, and its output drifts as versions change - usually into values that are reasonable and outside your allowed set.

Takeaway

Validate model output against a schema before returning it. An out-of-range value otherwise passes straight through and breaks your published contract.