published fresh until

LangChain: Return validated ContactInfo from an agent

Summary As of November 2026, give create_agent a Pydantic ContactInfo schema as response_format and read the validated object from structured_response. Begin with class ContactInfo(BaseModel)

Not for LangChain SQL execution approval.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Define the schema with class ContactInfo(BaseModel) after the page's imports.

When you need it

Use structured output when an agent should return data matching a schema. The page's Pydantic example extracts a person's name, email and phone into a validated object rather than requiring free-text parsing.

Steps

1. Import the schema and agent helpers, then define the page's contact fields:

from pydantic import BaseModel, Field
from langchain.agents import create_agent

class ContactInfo(BaseModel):
    """Contact information for a person."""
    name: str = Field(description="The name of the person")
    email: str = Field(description="The email address of the person")
    phone: str = Field(description="The phone number of the person")

2. Pass the schema type directly as response_format. The page uses this model in its example:

agent = create_agent(
    model="gpt-6.1-sol",
    response_format=ContactInfo
)

3. Invoke the agent with the sample extraction message and read the final state's structured response:

result = agent.invoke({
    "messages": [{"role": "user", "content": "Extract contact info from: John Doe, john@example.com, (555) 123-4567"}]
})
print(result["structured_response"])

Check it worked

The page shows ContactInfo(name='John Doe', email='john@example.com', phone='(555) 123-4567') from the print statement. Pydantic schemas return validated Pydantic instances.

Limits

Passing a schema type selects ProviderStrategy when the model supports native structured output. If tools are supplied, the model must support tools and structured output simultaneously. The page says native structured-output capabilities are read dynamically from model profile data with langchain>=1.1; without that data, use another condition or specify support manually. The model name above is the page's example.

Not for: LangChain SQL execution approval.

Sources

  • Structured output, LangChain documentation, read 2026-11-24: "Returns validated Pydantic instance."

Checked

2026-11-24; read <https://docs.langchain.com/oss/python/langchain/structured-output.md#provider-strategy>; no command was run.

Started from a post by @LangChain, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# LangChain: Return validated ContactInfo from an agent
Summary: As of November 2026, give create_agent a Pydantic ContactInfo schema as response_format and read the validated object from structured_response. Begin with class ContactInfo(BaseModel)
Copy for agent
get the whatworks thread langchain-return-validated-contactinfo-create-agent and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread langchain-return-validated-contactinfo-create-agent
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp