结构化输出限制 Claude 的响应遵循特定的模式,确保有效的、可解析的输出用于下游处理。有两个互补的功能可用:
output_format):获取 Claude 特定 JSON 格式的响应strict: true):保证工具名称和输入的模式验证这些功能可以独立使用,也可以在同一请求中一起使用。
结构化输出目前在 Claude API 中作为公开测试版功能提供,支持 Claude Sonnet 4.5、Claude Opus 4.1、Claude Opus 4.5 和 Claude Haiku 4.5。
要使用此功能,请设置 beta 标头 structured-outputs-2025-11-13。
使用此 表单 分享反馈。
没有结构化输出,Claude 可能会生成格式错误的 JSON 响应或无效的工具输入,从而破坏您的应用程序。即使进行仔细的提示,您也可能遇到:
结构化输出通过约束解码保证模式兼容的响应:
JSON.parse()JSON 输出控制 Claude 的响应格式,确保 Claude 返回与您的模式匹配的有效 JSON。当您需要以下情况时,使用 JSON 输出:
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: structured-outputs-2025-11-13" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Extract the key information from this email: John Smith ([email protected]) is interested in our Enterprise plan and wants to schedule a demo for next Tuesday at 2pm."
}
],
"output_format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"},
"plan_interest": {"type": "string"},
"demo_requested": {"type": "boolean"}
},
"required": ["name", "email", "plan_interest", "demo_requested"],
"additionalProperties": false
}
}
}'响应格式: 与您的模式匹配的有效 JSON,位于 response.content[0].text 中
{
"name": "John Smith",
"email": "[email protected]",
"plan_interest": "Enterprise",
"demo_requested": true
}定义您的 JSON 模式
创建一个 JSON 模式,描述您希望 Claude 遵循的结构。该模式使用标准 JSON Schema 格式,但有一些限制(请参阅 JSON Schema 限制)。
添加 output_format 参数
在您的 API 请求中包含 output_format 参数,其中 type: "json_schema" 和您的模式定义。
包含 beta 标头
将 anthropic-beta: structured-outputs-2025-11-13 标头添加到您的请求中。
解析响应
Claude 的响应将是与您的模式匹配的有效 JSON,在 response.content[0].text 中返回。
Python 和 TypeScript SDK 提供了帮助程序,使得使用 JSON 输出变得更容易,包括模式转换、自动验证和与流行模式库的集成。
对于 Python 和 TypeScript 开发人员,您可以使用熟悉的模式定义工具(如 Pydantic 和 Zod),而不是编写原始 JSON 模式。
from pydantic import BaseModel
from anthropic import Anthropic, transform_schema
class ContactInfo(BaseModel):
name: str
email: str
plan_interest: str
demo_requested: bool
client = Anthropic()
# With .create() - requires transform_schema()
response = client.beta.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
betas=["structured-outputs-2025-11-13"],
messages=[
{
"role": "user",
"content": "Extract the key information from this email: John Smith ([email protected]) is interested in our Enterprise plan and wants to schedule a demo for next Tuesday at 2pm."
}
],
output_format={
"type": "json_schema",
"schema": transform_schema(ContactInfo),
}
)
print(response.content[0].text)
# With .parse() - can pass Pydantic model directly
response = client.beta.messages.parse(
model="claude-sonnet-4-5",
max_tokens=1024,
betas=["structured-outputs-2025-11-13"],
messages=[
{
"role": "user",
"content": "Extract the key information from this email: John Smith ([email protected]) is interested in our Enterprise plan and wants to schedule a demo for next Tuesday at 2pm."
}
],
output_format=ContactInfo,
)
print(response.parsed_output)Python: client.beta.messages.parse() (推荐)
parse() 方法自动转换您的 Pydantic 模型,验证响应,并返回 parsed_output 属性。
parse() 方法在 client.beta.messages 上可用,而不是在 client.messages 上。
Python: transform_schema() 帮助程序
用于在发送前手动转换模式,或当您想修改 Pydantic 生成的模式时。与 client.beta.messages.parse() 不同,后者自动转换提供的模式,这给您转换后的模式,以便您可以进一步自定义它。
Python 和 TypeScript SDK 都自动转换具有不支持功能的模式:
minimum、maximum、minLength、maxLength)additionalProperties: false这意味着 Claude 接收简化的模式,但您的代码仍然通过验证强制执行所有约束。
示例: 具有 minimum: 100 的 Pydantic 字段在发送的模式中变成普通整数,但描述更新为"必须至少 100",SDK 验证响应是否符合原始约束。
严格工具使用验证工具参数,确保 Claude 使用正确类型的参数调用您的函数。当您需要以下情况时,使用严格工具使用:
构建可靠的代理系统需要保证模式一致性。没有严格模式,Claude 可能会返回不兼容的类型("2" 而不是 2)或缺少必需字段,破坏您的函数并导致运行时错误。
严格工具使用保证类型安全的参数:
例如,假设预订系统需要 passengers: int。没有严格模式,Claude 可能会提供 passengers: "two" 或 passengers: "2"。使用 strict: true,响应将始终包含 passengers: 2。
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: structured-outputs-2025-11-13" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "What is the weather in San Francisco?"}
],
"tools": [{
"name": "get_weather",
"description": "Get the current weather in a given location",
"strict": true,
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"],
"additionalProperties": false
}
}]
}'响应格式: 在 response.content[x].input 中具有经过验证的输入的工具使用块
{
"type": "tool_use",
"name": "get_weather",
"input": {
"location": "San Francisco, CA"
}
}保证:
input 严格遵循 input_schemaname 始终有效(来自提供的工具或服务器工具)定义您的工具模式
为您的工具的 input_schema 创建一个 JSON 模式。该模式使用标准 JSON Schema 格式,但有一些限制(请参阅 JSON Schema 限制)。
添加 strict: true
在您的工具定义中设置 "strict": true 作为顶级属性,与 name、description 和 input_schema 一起。
包含 beta 标头
将 anthropic-beta: structured-outputs-2025-11-13 标头添加到您的请求中。
处理工具调用
当 Claude 使用该工具时,工具使用块中的 input 字段将严格遵循您的 input_schema,name 将始终有效。
JSON 输出和严格工具使用解决不同的问题,可以一起使用:
结合使用时,Claude 可以使用保证有效的参数调用工具,并返回结构化的 JSON 响应。这对于需要可靠工具调用和结构化最终输出的代理工作流很有用。
response = client.beta.messages.create(
model="claude-sonnet-4-5",
betas=["structured-outputs-2025-11-13"],
max_tokens=1024,
messages=[{"role": "user", "content": "Help me plan a trip to Paris for next month"}],
# JSON outputs: structured response format
output_format={
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"summary": {"type": "string"},
"next_steps": {"type": "array", "items": {"type": "string"}}
},
"required": ["summary", "next_steps"],
"additionalProperties": False
}
},
# Strict tool use: guaranteed tool parameters
tools=[{
"name": "search_flights",
"strict": True,
"input_schema": {
"type": "object",
"properties": {
"destination": {"type": "string"},
"date": {"type": "string", "format": "date"}
},
"required": ["destination", "date"],
"additionalProperties": False
}
}]
)结构化输出使用具有编译语法工件的约束采样。这引入了一些需要注意的性能特征:
name 或 description 字段不会使缓存失效使用结构化输出时,Claude 自动接收额外的系统提示,解释预期的输出格式。这意味着:
output_format 参数将使该对话线程的任何 提示缓存 失效结构化输出支持标准 JSON Schema,但有一些限制。JSON 输出和严格工具使用都共享这些限制。
Python 和 TypeScript SDK 可以通过删除不支持的功能并将约束添加到字段描述来自动转换模式。有关详细信息,请参阅 SDK 特定方法。
虽然结构化输出在大多数情况下保证模式合规性,但在某些情况下输出可能不匹配您的模式:
拒绝 (stop_reason: "refusal")
Claude 即使在使用结构化输出时也保持其安全性和有用性属性。如果 Claude 出于安全原因拒绝请求:
stop_reason: "refusal"达到令牌限制 (stop_reason: "max_tokens")
如果响应因达到 max_tokens 限制而被截断:
stop_reason: "max_tokens"max_tokens 值重试以获取完整的结构化输出如果您的模式使用不支持的功能或过于复杂,您将收到 400 错误:
"模式中的递归定义过多"
"模式过于复杂"
strict: true 的工具数量对于有效模式的持续问题,请 联系支持 并提供您的模式定义。
适用于:
不兼容:
语法范围:语法仅适用于 Claude 的直接输出,不适用于工具使用调用、工具结果或思考标签(使用 扩展思考 时)。语法状态在各部分之间重置,允许 Claude 自由思考,同时仍在最终响应中生成结构化输出。