---
title: "Structured outputs with Pydantic AI"
description: "Pydantic AI gets reliable structured LLM output by declaring the expected shape as a Pydantic model."
url: "https://swe.quest/articles/structured-outputs-with-pydantic-ai-4bafdb166b5253d6"
canonical: "https://engineering.freeagent.com/2026/03/24/structured-outputs-with-pydantic-ai/"
source: "FreeAgent Engineering"
published: "2026-03-24"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["llms","python"]
---

# Structured outputs with Pydantic AI

A post on [FreeAgent Engineering](https://swe.quest/sources/freeagent.md), published 2026-03-24, 5 min read, How-to. Original: [https://engineering.freeagent.com/2026/03/24/structured-outputs-with-pydantic-ai/](https://engineering.freeagent.com/2026/03/24/structured-outputs-with-pydantic-ai/)

## Summary

Pydantic AI gets reliable structured LLM output by declaring the expected shape as a Pydantic model. The model's response is validated against that class, and the request is retried when validation fails, so there is no need to coax JSON out of the prompt. Type hints, dataclasses and Pydantic runtime validation lead up to the Agent API with output\_type, and Field descriptions pass attribute hints to the model. On AWS Bedrock, the structured output is implemented as a dummy tool call whose arguments match the schema. The abstraction means provider changes, such as Bedrock's newer output-config approach, need no code changes.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Python type hints ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- basic LLM API usage ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))

## Keywords

Pydantic AI, Pydantic, AWS Bedrock, structured outputs, mypy

## Related posts

- [Hugging Face x LangChain : A new partner package](https://swe.quest/articles/hugging-face-x-langchain-a-new-partner-package-4a05b92542fbd2d8.md): Hugging Face · 2024-05-14 · Medium · 4 min read · LLMs, Python, Inference & serving, RAG & retrieval
  langchain-huggingface is a new Python partner package jointly maintained by Hugging Face and LangChain, replacing community-written integrations that had drifted out of date.
- [Tool Use, Unified](https://swe.quest/articles/tool-use-unified-117f148d6f2c2374.md): Hugging Face · 2024-08-12 · Medium · 11 min read · LLMs, AI agents, Python, API design
  Hugging Face Transformers extends Jinja chat templates to cover tool use, giving a single portable API for passing tools to Mistral, Cohere, NousResearch and Llama models.
- [CodeAgents + Structure: A Better Way to Execute Actions](https://swe.quest/articles/codeagents-structure-a-better-way-to-execute-actions-5f9ad7c5f62e107c.md): Hugging Face · 2025-05-28 · Medium · 8 min read · AI agents, LLMs, Evaluation, Python
  Forcing code-writing agents to emit their reasoning and Python code as a structured JSON blob (thoughts + code) beats both markdown-parsed CodeAgents and JSON tool-calling agents by 2-7 percentage points on GAIA, MATH, SimpleQA and Frames for capable models.
- [Deploy Mistral Large to Azure and create a conversation with Python and LangChain](https://swe.quest/articles/deploy-mistral-large-to-azure-and-create-a-conversation-with-python-and-9f252b9b3fd4d808.md): Neon · 2024-02-27 · Medium · 4 min read · LLMs, Python, Cloud platforms
  Mistral Large, Mistral AI's flagship model with a 32k-token context window and leading MBPP and GSM8K scores, can be deployed through Azure AI Studio as a Pay-As-You-Go project in the France Central region.
- [Run Gemma 3 with Docker Model Runner: Fully Local GenAI Developer Experience](https://swe.quest/articles/run-gemma-3-with-docker-model-runner-fully-local-genai-developer-experience-c005339b3915484f.md): Docker · 2025-04-09 · Medium · 7 min read · LLMs, Inference & serving, Containers, JavaScript
  Docker Model Runner (in Docker Desktop for macOS 4.40.0+) exposes an OpenAI-compatible API on localhost, so Gemma 3 can be pulled with \`docker model pull ai/gemma3\` and called through the standard OpenAI SDK by pointing baseURL at http://localhost:12434/engines/v1.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
