結構化輸出限制 Claude 的回應遵循特定的架構,確保有效、可解析的輸出以供下游處理。有兩個互補的功能可用:
output_format):以特定的 JSON 格式獲取 Claude 的回應strict: true):保證工具名稱和輸入的架構驗證這些功能可以獨立使用,也可以在同一個請求中一起使用。
結構化輸出目前在 Claude API 中作為公開測試版功能提供,適用於 Claude Sonnet 4.5、Claude Opus 4.1、Claude Opus 4.5 和 Claude Haiku 4.5。
若要使用此功能,請設定 測試版標頭 structured-outputs-2025-11-13。
使用此 表單 分享回饋。
沒有結構化輸出,Claude 可能會生成格式不正確的 JSON 回應或無效的工具輸入,導致應用程式中斷。即使進行仔細的提示,您也可能遇到:
結構化輸出通過受限解碼保證架構相容的回應:
JSON.parse() 錯誤JSON 輸出控制 Claude 的回應格式,確保 Claude 返回與您的架構相符的有效 JSON。當您需要以下情況時,請使用 JSON 輸出:
回應格式: 在 response.content[0].text 中與您的架構相符的有效 JSON
{
"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" 和您的架構定義。
包含測試版標頭
將 anthropic-beta: structured-outputs-2025-11-13 標頭新增到您的請求中。
解析回應
Claude 的回應將是與您的架構相符的有效 JSON,在 response.content[0].text 中返回。
Python 和 TypeScript SDK 提供了幫助程式,使得使用 JSON 輸出變得更容易,包括架構轉換、自動驗證和與流行架構庫的整合。
對於 Python 和 TypeScript 開發人員,您可以使用熟悉的架構定義工具(如 Pydantic 和 Zod),而不是編寫原始 JSON 架構。
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。
回應格式: 在 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 一起。
包含測試版標頭
將 anthropic-beta: structured-outputs-2025-11-13 標頭新增到您的請求中。
處理工具呼叫
當 Claude 使用工具時,工具使用區塊中的 input 欄位將嚴格遵循您的 input_schema,name 將始終有效。
JSON 輸出和嚴格工具使用解決不同的問題,可以一起使用:
結合使用時,Claude 可以使用保證有效的參數呼叫工具,並返回結構化的 JSON 回應。這對於需要可靠工具呼叫和結構化最終輸出的代理工作流程很有用。
結構化輸出使用具有編譯文法成品的受限採樣。這引入了一些需要注意的效能特性:
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 的工具數量如有有效架構的持續問題,請 聯絡支援 並提供您的架構定義。
適用於:
output_format)和嚴格工具使用(strict: true)不相容於:
文法範圍:文法僅適用於 Claude 的直接輸出,不適用於工具使用呼叫、工具結果或思考標籤(使用 延伸思考 時)。文法狀態在各部分之間重置,允許 Claude 自由思考,同時仍在最終回應中產生結構化輸出。
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
}
}
}'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)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 = 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
}
}]
)