Pydantic Validation
Python's type-safe data validation and serialization library.
Definition
Pydantic is a Python library that enforces type annotations at runtime. It validates incoming data, coerces types, and serializes to JSON — all driven by Python type hints. FastAPI uses Pydantic models for request and response schemas, providing automatic validation, OpenAPI documentation, and editor autocomplete.
Why it matters for AI APIs
Pydantic v2 (with Rust-based validation) is dramatically faster than v1 and has become the standard for Python API development. Using Pydantic for LLM structured output — parsing GPT responses into typed Python objects — eliminates a class of runtime errors common in AI applications.
In FastAPI AI Kit
FastAPI AI Kit uses Pydantic v2 throughout: request/response models, config validation, LLM structured output parsing, and background job payloads. The `llm.extract(text, output_schema=MySchema)` method uses Pydantic models as the output contract.
