Was this page helpful?
结构化输出限制 Claude 的响应遵循特定的模式,确保有效的、可解析的输出用于下游处理。结构化输出提供两个互补的功能:
output_config.format):获取 Claude 特定 JSON 格式的响应strict: true):保证工具名称和输入的模式验证您可以在同一请求中独立使用这些功能或一起使用。
结构化输出在 Claude API 上通常可用于 Claude Mythos Preview、Claude Opus 4.7、Claude Opus 4.6、Claude Sonnet 4.6、Claude Sonnet 4.5、Claude Opus 4.5 和 Claude Haiku 4.5。在 Amazon Bedrock 上,结构化输出通常可用于 Claude Mythos Preview、Claude Opus 4.6、Claude Sonnet 4.6、Claude Sonnet 4.5、Claude Opus 4.5 和 Claude Haiku 4.5;Claude Opus 4.7 可通过 Amazon Bedrock 研究预览版 获得。结构化输出在 Microsoft Foundry 上处于测试阶段。Google Cloud 的 Vertex AI 不支持 Claude Mythos Preview 的结构化输出。
This feature qualifies for Zero Data Retention (ZDR) with limited technical retention. See the Data retention section for details on what is retained and why.
从测试版迁移? output_format 参数已移至 output_config.format,不再需要测试版标头。旧的测试版标头 (structured-outputs-2025-11-13) 和 output_format 参数将在过渡期内继续工作。请参阅下面的代码示例了解更新的 API 形状。
没有结构化输出,Claude 可能会生成格式错误的 JSON 响应或无效的工具输入,这会破坏您的应用程序。即使进行了仔细的提示,您也可能遇到:
结构化输出通过受限解码保证模式兼容的响应:
JSON.parse() 错误JSON 输出控制 Claude 的响应格式,确保 Claude 返回与您的模式匹配的有效 JSON。在以下情况下使用 JSON 输出:
响应格式: 与您的模式匹配的有效 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_config.format 参数
在您的 API 请求中包含 output_config.format 参数,其中包含 type: "json_schema" 和您的模式定义。
解析响应
Claude 的响应是与您的模式匹配的有效 JSON,在 response.content[0].text 中返回。
SDK 提供了辅助工具,使得使用 JSON 输出变得更加容易,包括模式转换、自动验证以及与流行模式库的集成。
Python SDK 的 client.messages.parse() 仍然接受 output_format 作为便利参数,并在内部将其转换为 output_config.format。其他 SDK 需要直接使用 output_config。下面的示例展示了 SDK 辅助工具的语法。
与其编写原始 JSON 模式,你可以在你的语言中使用熟悉的模式定义工具:
client.messages.parse() 的 Pydantic 模型zodOutputFormat() 的 Zod 模式或使用 jsonSchemaOutputFormat() 的类型化 JSON Schema 字面量outputConfig(Class<T>) 进行自动模式推导的普通 Java 类output_config: {format: Model} 的 Anthropic::BaseModel 类StructuredOutputModel 的类,使用 outputConfig: ['format' => MyClass::class]output_config 传递的原始 JSON 模式每个 SDK 都提供了辅助工具,使得使用结构化输出变得更加容易。有关完整详情,请参阅各个 SDK 页面。
Python、TypeScript、Ruby 和 PHP SDK 自动转换具有不支持功能的模式:
minimum、maximum、minLength、maxLength)additionalProperties: false这意味着 Claude 接收一个简化的模式,但你的代码仍然通过验证强制所有约束。
示例: 具有 minimum: 100 的 Pydantic 字段在发送的模式中变成普通整数,但 SDK 将描述更新为"必须至少 100",并针对原始约束验证响应。
有关使用语法约束采样在工具输入上强制执行 JSON Schema 合规性的信息,请参阅严格工具使用。
JSON 输出和严格工具使用解决不同的问题并协同工作:
结合使用时,Claude 可以使用保证有效的参数调用工具,并返回结构化的 JSON 响应。这对于需要可靠工具调用和结构化最终输出的代理工作流很有用。
结构化输出使用带有编译语法工件的受限采样。这引入了一些需要注意的性能特征:
name 或 description 字段不会使缓存失效使用结构化输出时,Claude 会自动收到一个额外的系统提示,说明预期的输出格式。这意味着:
output_config.format 参数将使该对话线程的任何提示缓存失效结构化输出支持标准 JSON Schema,但有一些限制。JSON 输出和严格工具使用都共享这些限制。
Python、TypeScript、Ruby 和 PHP SDK 可以通过删除不支持的功能并向字段描述添加约束来自动转换模式。有关详细信息,请参阅 SDK 特定方法。
使用结构化输出时,对象中的属性保持从您的模式定义的顺序,但有一个重要的注意事项:必需属性首先出现,然后是可选属性。
例如,给定此模式:
{
"type": "object",
"properties": {
"notes": { "type": "string" },
"name": { "type": "string" },
"email": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name", "email"],
"additionalProperties": false
}输出将按以下顺序排列属性:
name(必需,按模式顺序)email(必需,按模式顺序)notes(可选,按模式顺序)age(可选,按模式顺序)这意味着输出可能如下所示:
{
"name": "John Smith",
"email": "[email protected]",
"notes": "Interested in enterprise plan",
"age": 35
}如果输出中的属性顺序对您的应用程序很重要,请将所有属性标记为必需,或在您的解析逻辑中考虑此重新排序。
虽然结构化输出在大多数情况下保证模式合规性,但在某些情况下输出可能不匹配您的模式:
拒绝(stop_reason: "refusal")
Claude 即使在使用结构化输出时也保持其安全性和有用性属性。如果 Claude 因安全原因拒绝请求:
stop_reason: "refusal"达到令牌限制(stop_reason: "max_tokens")
如果响应因达到 max_tokens 限制而被截断:
stop_reason: "max_tokens"max_tokens 值重试以获得完整的结构化输出结构化输出通过将您的 JSON 模式编译为限制 Claude 输出的语法来工作。更复杂的模式会产生更大的语法,编译时间更长。为了防止过度的编译时间,API 强制执行多个复杂性限制。
以下限制适用于所有具有 output_config.format 或 strict: true 的请求:
| 限制 | 值 | 描述 |
|---|---|---|
| 每个请求的严格工具 | 20 | 具有 strict: true 的最大工具数。非严格工具不计入此限制。 |
| 可选参数 | 24 | 所有严格工具模式和 JSON 输出模式中的总可选参数。未列在 required 中的每个参数都计入此限制。 |
| 具有联合类型的参数 | 16 | 在所有严格模式中使用 anyOf 或类型数组(例如 "type": ["string", "null"])的总参数。这些特别昂贵,因为它们会产生指数编译成本。 |
这些限制适用于单个请求中所有严格模式的组合总数。例如,如果您有 4 个严格工具,每个工具有 6 个可选参数,即使没有单个工具看起来很复杂,您也会达到 24 参数限制。
除了上述显式限制外,编译语法大小还有额外的内部限制。这些限制存在是因为模式复杂性不能简化为单一维度:可选参数、联合类型、嵌套对象和工具数量等功能以可能使编译语法不成比例地大的方式相互作用。
当超过这些限制时,您将收到 400 错误,消息为"Schema is too complex for compilation"。这些错误意味着您的模式的组合复杂性超过了可以有效编译的范围,即使每个上述单独的限制都得到满足。作为最后的防止措施,API 还强制执行编译超时 180 秒。通过所有显式检查但产生非常大的编译语法的模式可能会触发此超时。
如果您遇到复杂性限制,请按顺序尝试这些策略:
**仅将关键工具标记为严格。**如果您有许多工具,请为模式违规会导致真正问题的工具保留它,并依靠 Claude 对更简单工具的自然遵守。
**减少可选参数。**尽可能使参数 required。每个可选参数大约会使语法状态空间的一部分翻倍。如果参数总是有合理的默认值,请考虑使其成为必需的,并让 Claude 明确提供该默认值。
**简化嵌套结构。**具有可选字段的深层嵌套对象会加剧复杂性。尽可能展平结构。
**分成多个请求。**如果您有许多严格工具,请考虑将它们分散到单独的请求或子代理中。
对于有效模式的持续问题,请联系支持并提供您的模式定义。
使用结构化输出时,提示和响应使用 ZDR 处理。但是,JSON 模式本身会临时缓存长达 24 小时(自上次使用以来)以用于优化目的。API 响应之外不保留任何提示或响应数据。
结构化输出符合 HIPAA 资格,但JSON 模式定义中不得包含 PHI。API 将 JSON 模式编译为与消息内容分开缓存的语法,这些缓存的模式不会获得与提示和响应相同的 PHI 保护。不要在模式属性名称、enum 值、const 值或 pattern 正则表达式中包含 PHI。PHI 应仅出现在消息内容(提示和响应)中,其中受 HIPAA 保护措施保护。
有关所有功能的 ZDR 和 HIPAA 资格,请参阅 API 和数据保留。
适用于:
output_config.format)和严格工具使用(strict: true)不兼容:
output_config.format 的引用,返回 400 错误。语法范围:语法仅适用于 Claude 的直接输出,不适用于工具使用调用、工具结果或思考标签(使用扩展思考时)。语法状态在各部分之间重置,允许 Claude 自由思考,同时仍在最终响应中生成结构化输出。
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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_config={
"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,
},
}
},
)
print(response.content[0].text)from pydantic import BaseModel
from anthropic import Anthropic
class ContactInfo(BaseModel):
name: str
email: str
plan_interest: str
demo_requested: bool
client = Anthropic()
response = client.messages.parse(
model="claude-opus-4-7",
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=ContactInfo,
)
print(response.parsed_output)response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Help me plan a trip to Paris departing May 15, 2026",
}
],
# JSON outputs: structured response format
output_config={
"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,
},
}
],
)
print(response)