結構化輸出限制 Claude 的回應遵循特定的架構,確保有效、可解析的輸出以供下游處理。使用 JSON 輸出(output_format)進行結構化資料回應,或使用嚴格工具使用(strict: true)以保證工具名稱和輸入的架構驗證。
結構化輸出目前在 Claude API 中作為公開測試版功能提供,適用於 Claude Sonnet 4.5 和 Claude Opus 4.1。
若要使用此功能,請設定 beta 標頭 structured-outputs-2025-11-13。
使用此表單分享回饋。
沒有結構化輸出,Claude 可能會生成格式不正確的 JSON 回應或無效的工具輸入,導致應用程式中斷。即使進行仔細的提示,您也可能遇到:
結構化輸出通過受限解碼保證架構相容的回應:
JSON.parse() 錯誤為您的使用案例選擇正確的模式:
| 何時使用 JSON 輸出 | 何時使用嚴格工具使用 |
|---|---|
| 您需要 Claude 的回應採用特定格式 | 您需要驗證的參數和工具名稱以進行工具呼叫 |
| 從影像或文字中提取資料 | 建立代理工作流程 |
| 生成結構化報告 | 確保類型安全的函數呼叫 |
| 格式化 API 回應 | 具有許多和/或巢狀屬性的複雜工具 |
建立可靠的代理系統需要保證架構相容性。無效的工具參數會破壞您的函數和工作流程。Claude 可能會返回不相容的類型("2" 而不是 2)或缺少欄位,導致執行時錯誤。
嚴格工具使用保證類型安全的參數:
例如,假設預訂系統需要 passengers: int。在沒有嚴格模式的情況下,Claude 可能會提供 passengers: "two" 或 passengers: "2"。使用 strict: true,您可以保證 passengers: 2。
Python 和 TypeScript SDK 提供了幫助程式,使使用 JSON 輸出變得更容易,包括架構轉換、自動驗證和與流行架構庫的整合。
對於 Python 和 TypeScript 開發人員,您可以使用熟悉的架構定義工具(如 Pydantic 和 Zod),而不是編寫原始 JSON 架構。
僅限 JSON 輸出
SDK 幫助程式(Pydantic、Zod、parse())僅適用於 JSON 輸出(output_format)。
這些幫助程式轉換並驗證 Claude 對您的輸出。嚴格工具使用驗證 Claude 對您的工具的輸入,這使用工具定義中現有的 input_schema 欄位。
對於嚴格工具使用,在工具定義中直接定義您的 input_schema,使用 strict: true。
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 會根據原始限制驗證回應。
結構化輸出使用具有編譯文法工件的受限採樣。這引入了一些需要注意的效能特性:
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 自由思考,同時仍在最終回應中產生結構化輸出。
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)