Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

FreeAgent Engineering 5 min read How-to mediumAdept

Structured outputs with Pydantic AI

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.

Related posts

  • Hugging Face x LangChain : A new partner package

    Hugging Face mediumAdept

    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. It provides HuggingFacePipeline for running transformers models locally, HuggingFaceEndpoint for the serverless Inference API or deployed TGI endpoints via InferenceClient, and ChatHuggingFace, which turns message lists into model-specific prompts with tokenizer.apply_chat_template. For embeddings it offers HuggingFaceEmbeddings (local sentence-transformers) and HuggingFaceEndpointEmbeddings (Hub or TEI instances), each shown with short code examples.

  • Tool Use, Unified

    Hugging Face mediumAdept

    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. Tools are defined as JSON schema, or as Python functions whose type hints and docstrings are converted automatically via get_json_schema. Tool calls go into the chat as a tool_calls field on assistant messages, and results are added as separate tool-role messages. A full walkthrough with Hermes-2-Pro-Llama-3-8B shows the loop end to end. Parsing the model's emitted tool-call text is still model-specific and needs a little json.loads or regex work.

  • CodeAgents + Structure: A Better Way to Execute Actions

    Hugging Face mediumAdept

    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. An analysis of 15,724 traces shows first-step parsing errors cut success from 51.3% to 42.3% and raise average steps from 3.18 to 4.63, which structured generation eliminates. Smaller models such as Mistral-7B pay a "structure tax", producing broken Python when juggling JSON escaping and code at once, so the approach is recommended for 32B+ or frontier models. In smolagents it is enabled with use_structured_outputs_internally=True.

  • Deploy Mistral Large to Azure and create a conversation with Python and LangChain

    Neon mediumAdept

    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. The deployment yields an inference endpoint and API key that plug directly into LangChain's ChatMistralAI class. A short Python example builds a conversational LLMChain with a system prompt, ConversationBufferMemory and a chat-history placeholder, and shows the expected output.

  • Run Gemma 3 with Docker Model Runner: Fully Local GenAI Developer Experience

    Docker mediumAdept

    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. A sample Comment Processing System uses it to generate synthetic user feedback, categorize sentiment, cluster comments with mxbai-embed-large embeddings, extract feature ideas as JSON and draft support replies, all with no external API calls. Per-task temperature and token limits can be tuned freely because local inference carries no per-token cost or rate limit.

The quest for software engineering knowledge.

Look:
Layout: