Structured output 允许 agents 以特定且可预测的格式返回数据。你无需解析自然语言响应,而是可以获得应用可直接使用的结构化数据,例如 JSON objects、Pydantic models 或 dataclasses。
本页介绍如何通过 create_agent 在 agents 中使用 structured output。如需直接在模型上使用 structured output(在 agents 之外),请参阅 Models - Structured output
LangChain 的 create_agent 会自动处理 structured output。用户设置所需的 structured output schema,当模型生成结构化数据时,数据会被捕获、验证,并在 agent state 的 'structured_response' key 中返回。
def create_agent(
    ...
    response_format: Union[
        ToolStrategy[StructuredResponseT],
        ProviderStrategy[StructuredResponseT],
        type[StructuredResponseT],
        None,
    ]
)

Response format

使用 response_format 控制 agent 如何返回结构化数据:
  • ToolStrategy[StructuredResponseT]:使用 tool calling 生成 structured output
  • ProviderStrategy[StructuredResponseT]:使用 provider-native structured output
  • type[StructuredResponseT]:Schema 类型,根据模型能力自动选择最佳策略
  • None:未显式请求 structured output
直接提供 schema 类型时,LangChain 会自动选择:
  • 如果所选模型和 provider 支持原生 structured output(例如 OpenAIAnthropic (Claude)xAI (Grok)),则选择 ProviderStrategy
  • 对所有其他模型选择 ToolStrategy
如果使用 langchain>=1.1,对原生 structured output 功能的支持会从模型的 profile data 中动态读取。如果数据不可用,请使用其他条件或手动指定:
custom_profile = {
    "structured_output": True,
    # ...
}
model = init_chat_model("...", profile=custom_profile)
如果指定了 tools,模型必须支持同时使用 tools 和 structured output。
结构化响应会在 agent 最终 state 的 structured_response key 中返回。

Provider strategy

一些 model providers 通过其 APIs 原生支持 structured output,例如 OpenAI、xAI (Grok)、Gemini、Anthropic (Claude)。如果可用,这是最可靠的方法。 如需使用该策略,请配置 ProviderStrategy
class ProviderStrategy(Generic[SchemaT]):
    schema: type[SchemaT]
    strict: bool | None = None
strict 参数需要 langchain>=1.2
schema
required
定义 structured output 格式的 schema。支持:
  • Pydantic models:带字段验证的 BaseModel 子类。返回经过验证的 Pydantic instance。
  • Dataclasses:带类型注解的 Python dataclasses。返回 dict。
  • TypedDict:Typed dictionary classes。返回 dict。
  • JSON Schema:带 JSON schema specification 的字典。返回 dict。
strict
可选 boolean 参数,用于启用严格 schema 遵循。部分 providers 支持,例如 OpenAIxAI。默认为 None(禁用)。
当你将 schema 类型直接传给 create_agent.response_format,且模型支持原生 structured output 时,LangChain 会自动使用 ProviderStrategy
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")

agent = create_agent(
    model="gpt-5.4",
    response_format=ContactInfo  # Auto-selects ProviderStrategy
)

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

print(result["structured_response"])
# ContactInfo(name='John Doe', email='john@example.com', phone='(555) 123-4567')
Provider-native structured output 由模型 provider 强制执行 schema,因此可靠性高且验证严格。可用时请优先使用。
如果 provider 对你选择的模型原生支持 structured output,写 response_format=ProductReview 与写 response_format=ProviderStrategy(ProductReview) 在功能上等价。无论哪种写法,如果不支持 structured output,agent 都会 fallback 到 tool calling strategy。

Tool calling strategy

对于不支持原生 structured output 的模型,LangChain 使用 tool calling 实现相同结果。这适用于所有支持 tool calling 的模型(大多数现代模型)。 如需使用该策略,请配置 ToolStrategy
class ToolStrategy(Generic[SchemaT]):
    schema: type[SchemaT]
    tool_message_content: str | None
    handle_errors: Union[
        bool,
        str,
        type[Exception],
        tuple[type[Exception], ...],
        Callable[[Exception], str],
    ]
schema
required
定义 structured output 格式的 schema。支持:
  • Pydantic models:带字段验证的 BaseModel 子类。返回经过验证的 Pydantic instance。
  • Dataclasses:带类型注解的 Python dataclasses。返回 dict。
  • TypedDict:Typed dictionary classes。返回 dict。
  • JSON Schema:带 JSON schema specification 的字典。返回 dict。
  • Union types:多个 schema 选项。模型会根据上下文选择最合适的 schema。
tool_message_content
生成 structured output 时返回的 tool message 的自定义内容。 如果未提供,默认使用显示结构化响应数据的 message。
handle_errors
Structured output 验证失败时的错误处理策略。默认为 True
  • True:使用默认错误模板捕获所有错误
  • str:使用此自定义 message 捕获所有错误
  • type[Exception]:仅捕获此异常类型,并使用默认 message
  • tuple[type[Exception], ...]:仅捕获这些异常类型,并使用默认 message
  • Callable[[Exception], str]:返回错误 message 的自定义函数
  • False:不重试,让异常继续抛出
from pydantic import BaseModel, Field
from typing import Literal
from langchain.agents import create_agent
from langchain.agents.structured_output import ToolStrategy


class ProductReview(BaseModel):
    """Analysis of a product review."""
    rating: int | None = Field(description="The rating of the product", ge=1, le=5)
    sentiment: Literal["positive", "negative"] = Field(description="The sentiment of the review")
    key_points: list[str] = Field(description="The key points of the review. Lowercase, 1-3 words each.")

agent = create_agent(
    model="gpt-5.4",
    tools=tools,
    response_format=ToolStrategy(ProductReview)
)

result = agent.invoke({
    "messages": [{"role": "user", "content": "Analyze this review: 'Great product: 5 out of 5 stars. Fast shipping, but expensive'"}]
})
result["structured_response"]
# ProductReview(rating=5, sentiment='positive', key_points=['fast shipping', 'expensive'])

Custom tool message content

tool_message_content 参数允许你自定义生成 structured output 时出现在对话历史中的 message:
from pydantic import BaseModel, Field
from typing import Literal
from langchain.agents import create_agent
from langchain.agents.structured_output import ToolStrategy


class MeetingAction(BaseModel):
    """Action items extracted from a meeting transcript."""
    task: str = Field(description="The specific task to be completed")
    assignee: str = Field(description="Person responsible for the task")
    priority: Literal["low", "medium", "high"] = Field(description="Priority level")

agent = create_agent(
    model="gpt-5.4",
    tools=[],
    response_format=ToolStrategy(
        schema=MeetingAction,
        tool_message_content="Action item captured and added to meeting notes!"
    )
)

agent.invoke({
    "messages": [{"role": "user", "content": "From our meeting: Sarah needs to update the project timeline as soon as possible"}]
})
================================ Human Message =================================

From our meeting: Sarah needs to update the project timeline as soon as possible
================================== Ai Message ==================================
Tool Calls:
  MeetingAction (call_1)
 Call ID: call_1
  Args:
    task: Update the project timeline
    assignee: Sarah
    priority: high
================================= Tool Message =================================
Name: MeetingAction

Action item captured and added to meeting notes!
如果没有 tool_message_content,最终 ToolMessage 会是:
================================= Tool Message =================================
Name: MeetingAction

Returning structured response: {'task': 'update the project timeline', 'assignee': 'Sarah', 'priority': 'high'}

Error handling

Models 通过 tool calling 生成 structured output 时可能出错。LangChain 提供智能重试机制来自动处理这些错误。

Multiple structured outputs error

当模型错误地调用多个 structured output tools 时,agent 会在 ToolMessage 中提供错误反馈,并提示模型重试:
from pydantic import BaseModel, Field
from typing import Union
from langchain.agents import create_agent
from langchain.agents.structured_output import ToolStrategy


class ContactInfo(BaseModel):
    name: str = Field(description="Person's name")
    email: str = Field(description="Email address")

class EventDetails(BaseModel):
    event_name: str = Field(description="Name of the event")
    date: str = Field(description="Event date")

agent = create_agent(
    model="gpt-5.4",
    tools=[],
    response_format=ToolStrategy(Union[ContactInfo, EventDetails])  # Default: handle_errors=True
)

agent.invoke({
    "messages": [{"role": "user", "content": "Extract info: John Doe (john@email.com) is organizing Tech Conference on March 15th"}]
})
================================ Human Message =================================

Extract info: John Doe (john@email.com) is organizing Tech Conference on March 15th
None
================================== Ai Message ==================================
Tool Calls:
  ContactInfo (call_1)
 Call ID: call_1
  Args:
    name: John Doe
    email: john@email.com
  EventDetails (call_2)
 Call ID: call_2
  Args:
    event_name: Tech Conference
    date: March 15th
================================= Tool Message =================================
Name: ContactInfo

Error: Model incorrectly returned multiple structured responses (ContactInfo, EventDetails) when only one is expected.
 Please fix your mistakes.
================================= Tool Message =================================
Name: EventDetails

Error: Model incorrectly returned multiple structured responses (ContactInfo, EventDetails) when only one is expected.
 Please fix your mistakes.
================================== Ai Message ==================================
Tool Calls:
  ContactInfo (call_3)
 Call ID: call_3
  Args:
    name: John Doe
    email: john@email.com
================================= Tool Message =================================
Name: ContactInfo

Returning structured response: {'name': 'John Doe', 'email': 'john@email.com'}

Schema validation error

当 structured output 与预期 schema 不匹配时,agent 会提供具体错误反馈:
from pydantic import BaseModel, Field
from langchain.agents import create_agent
from langchain.agents.structured_output import ToolStrategy


class ProductRating(BaseModel):
    rating: int | None = Field(description="Rating from 1-5", ge=1, le=5)
    comment: str = Field(description="Review comment")

agent = create_agent(
    model="gpt-5.4",
    tools=[],
    response_format=ToolStrategy(ProductRating),  # Default: handle_errors=True
    system_prompt="You are a helpful assistant that parses product reviews. Do not make any field or value up."
)

agent.invoke({
    "messages": [{"role": "user", "content": "Parse this: Amazing product, 10/10!"}]
})
================================ Human Message =================================

Parse this: Amazing product, 10/10!
================================== Ai Message ==================================
Tool Calls:
  ProductRating (call_1)
 Call ID: call_1
  Args:
    rating: 10
    comment: Amazing product
================================= Tool Message =================================
Name: ProductRating

Error: Failed to parse structured output for tool 'ProductRating': 1 validation error for ProductRating.rating
  Input should be less than or equal to 5 [type=less_than_equal, input_value=10, input_type=int].
 Please fix your mistakes.
================================== Ai Message ==================================
Tool Calls:
  ProductRating (call_2)
 Call ID: call_2
  Args:
    rating: 5
    comment: Amazing product
================================= Tool Message =================================
Name: ProductRating

Returning structured response: {'rating': 5, 'comment': 'Amazing product'}

Error handling strategies

可以使用 handle_errors 参数自定义错误处理方式: 自定义错误 message:
ToolStrategy(
    schema=ProductRating,
    handle_errors="Please provide a valid rating between 1-5 and include a comment."
)
如果 handle_errors 是字符串,agent 将始终使用固定 tool message 提示模型重试:
================================= Tool Message =================================
Name: ProductRating

Please provide a valid rating between 1-5 and include a comment.
仅处理特定异常:
ToolStrategy(
    schema=ProductRating,
    handle_errors=ValueError  # Only retry on ValueError, raise others
)
如果 handle_errors 是异常类型,agent 仅在抛出的异常属于指定类型时重试(使用默认错误 message)。在所有其他情况下,异常会被抛出。 处理多个异常类型:
ToolStrategy(
    schema=ProductRating,
    handle_errors=(ValueError, TypeError)  # Retry on ValueError and TypeError
)
如果 handle_errors 是异常 tuple,agent 仅在抛出的异常属于指定类型之一时重试(使用默认错误 message)。在所有其他情况下,异常会被抛出。 自定义错误处理函数:

from langchain.agents.structured_output import StructuredOutputValidationError
from langchain.agents.structured_output import MultipleStructuredOutputsError

def custom_error_handler(error: Exception) -> str:
    if isinstance(error, StructuredOutputValidationError):
        return "There was an issue with the format. Try again."
    elif isinstance(error, MultipleStructuredOutputsError):
        return "Multiple structured outputs were returned. Pick the most relevant one."
    else:
        return f"Error: {str(error)}"


agent = create_agent(
    model="gpt-5.4",
    tools=[],
    response_format=ToolStrategy(
                        schema=Union[ContactInfo, EventDetails],
                        handle_errors=custom_error_handler
                    )  # Default: handle_errors=True
)

result = agent.invoke({
    "messages": [{"role": "user", "content": "Extract info: John Doe (john@email.com) is organizing Tech Conference on March 15th"}]
})

for msg in result['messages']:
    # If message is actually a ToolMessage object (not a dict), check its class name
    if type(msg).__name__ == "ToolMessage":
        print(msg.content)
    # If message is a dictionary or you want a fallback
    elif isinstance(msg, dict) and msg.get('tool_call_id'):
        print(msg['content'])

遇到 StructuredOutputValidationError 时:
================================= Tool Message =================================
Name: ToolStrategy

There was an issue with the format. Try again.
遇到 MultipleStructuredOutputsError 时:
================================= Tool Message =================================
Name: ToolStrategy

Multiple structured outputs were returned. Pick the most relevant one.
遇到其他错误时:
================================= Tool Message =================================
Name: ToolStrategy

Error: <error message>
不处理错误:
response_format = ToolStrategy(
    schema=ProductRating,
    handle_errors=False  # All errors raised
)