Pydantic recipes
16 tested Pydantic integration recipes with FastAPI, httpx, respx, hypothesis. Exact pinned versions and a last-passed receipt, last run 2026-10-15.
FastAPI + Pydantic v2 validators, discriminated unions and serializers
Pydantic v2 patterns in FastAPI that agents often get wrong: field_validator, model_validator, discriminated unions, computed_field and response_model filtering.
FastAPI background tasks + dependency overrides
FastAPI BackgroundTasks for post-response work, with dependency_overrides to swap services in tests.
FastAPI endpoint with Pydantic v2 discriminated unions and Annotated validators
Accept a tagged union of payment methods with Field(discriminator), add Annotated validators, and test the exact 422 error shapes offline with TestClient.
httpx async API client + respx mocks
A typed async API client on httpx with retries on 5xx and Pydantic parsing, tested offline with respx route mocks.
Hypothesis property tests for Pydantic v2 models
Property-based tests with Hypothesis on Pydantic v2 models: JSON round trips, validator invariants, and a stateful RuleBasedStateMachine for a small inventory class.
LangChain (Python) LCEL chain with structured parsing, tested with a fake chat model
Compose ChatPromptTemplate, a chat model and a Pydantic output parser with LCEL in langchain-core, tested offline using GenericFakeChatModel.
Loading .env files with python-dotenv and pydantic-settings precedence rules
Typed app settings from .env files with pydantic-settings, covering source precedence, layered env files, extra-key rules and python-dotenv override behavior, tested offline with tmp_path files.
OpenAI chat.completions.parse with Pydantic output + respx mocks
Get typed output from OpenAI chat.completions.parse using a Pydantic response_format, handle refusals and schema mismatches, and test offline with respx returning canned JSON.
OpenAI Python SDK structured outputs, tested with respx
Call the OpenAI Python SDK and validate JSON output with Pydantic, testing success, malformed output and API errors offline by mocking HTTP with respx.
Pydantic Settings typed config from env and .env
Typed app configuration with pydantic-settings: env prefixes, nested settings, secrets and validation errors.
Pydantic v2 email and phone validation with email-validator and phonenumbers
Validate emails with EmailStr and normalize phone numbers to E164 with phonenumbers inside a Pydantic model, tested offline with no network lookups.
pydantic-ai Agent with typed output and deps-based tools, tested offline
A pydantic-ai agent with a typed Pydantic output and a tool that reads deps, tested fully offline with TestModel, FunctionModel and Agent.override.
pydantic-settings v2 config from env, YAML and defaults with SecretStr
Nested pydantic-settings models loaded with precedence init > env > YAML > defaults, with env_nested_delimiter, SecretStr masking, and offline tests using tmp_path and monkeypatch.
SQLAlchemy 2 ORM rows serialized with Pydantic v2 from_attributes
Map SQLAlchemy 2 models, expose nested relationships through Pydantic v2 schemas with from_attributes=True and a computed_field, and test them offline against in-memory SQLite.
Typed httpx client parsing into pydantic v2 models, tested with respx
An async httpx client that retries 5xx and transport errors, validates responses into pydantic v2 models, and is tested offline with respx route mocks.
Validating polars DataFrame rows with pydantic v2 TypeAdapter and collecting errors
Validate each polars row against a strict pydantic v2 model with a shared TypeAdapter, collect per-row errors instead of failing the batch, and catch polars and pydantic gotchas such as nulls, NaN, numeric strings and extra columns.
Used together with Pydantic
FastAPIhttpxrespxhypothesislangchain-corepython-dotenvPydantic Settingsopenaiemail-validatorphonenumberspydantic-aipyyamlSQLAlchemypolars