Structured Output — LangChain Agents (cross-section)

Full summary: Structured Output (LangChain Agents)

A LangChain reference covering how agents return typed, validated data using create_agent’s response_format parameter. Two strategies: ProviderStrategy (native API-level schema enforcement for OpenAI, Anthropic/Claude, xAI/Grok, Gemini) and ToolStrategy (tool-calling-based, works with any tool-capable model). Auto-selection is based on model capability profile. The structured response appears in structured_response in the agent’s final state.

Agent-Development Angle

Structured output is a core implementation pattern for building agents that return predictable, machine-readable responses — critical for chaining agents, populating downstream systems, and enforcing output contracts in multi-agent pipelines. Key contributions to this section:

  • Two-Strategy Pattern: ProviderStrategy for supported providers, ToolStrategy as universal fallback — understanding which to use and when is an agent development concern
  • Auto-Selection from Model Profile: response_format=ContactInfo auto-selects the best strategy; custom profiles override capability detection for non-standard models
  • Validation Error Recovery: ToolStrategy automatically retries on schema validation failure with targeted error feedback — building reliable structured output into agent logic without manual try/except plumbing
  • Union Type Support: Union[SchemaA, SchemaB] enables agents to select among multiple output schemas based on context — useful for multi-intent extraction tasks
  • Pydantic Integration: BaseModel subclasses are the recommended schema definition; field validators enforce business rules (range checks, regex) at the output layer

Connections

  • Retry Pattern — ToolStrategy’s handle_errors retry mechanism is the retry pattern applied at the structured output layer
  • Circuit Breaker Pattern — structured output validation and circuit-breaking address complementary reliability concerns in agent development