本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
從模型取得經過驗證的 JSON 結果
結構化輸出是 Amazon Bedrock 上的功能,可確保模型回應符合使用者定義的 JSON 結構描述和工具定義,減少在生產 AI 部署中自訂剖析和驗證機制的需求。
優勢
結構化輸出可解決生產 AI 應用程式的關鍵挑戰:
-
確保結構描述合規 – 從提示型方法中消除錯誤率和重試迴圈
-
降低開發複雜性 – 無需自訂剖析和驗證邏輯
-
降低營運成本 – 減少失敗的請求和重試次數
-
生產可靠性 – 可放心部署需要可預測、機器可讀取輸出的 AI 應用程式
運作方式
結構化輸出會限制模型回應以遵循特定結構描述,確保下游處理的有效、可剖析輸出。您可以透過兩種互補機制使用結構化輸出:
JSON 結構描述輸出格式
對於具有 Anthropic Claude 模型的 InvokeModel API,請使用 output_config.format請求欄位。對於開放權重模型,請使用 response_format 請求欄位。針對 Converse APIs,請使用 outputConfig.textFormat 請求欄位。模型的回應將符合指定的 JSON 結構描述。
嚴格的工具使用
將 strict: true旗標新增至工具定義,以啟用工具名稱和輸入的結構描述驗證。模型的工具呼叫接著會遵循定義的工具輸入結構描述。
這些機制可以獨立使用,也可以在相同的請求中一起使用。如需詳細資訊,請參閱 Bedrock API 文件。
請求工作流程
以下說明 Amazon Bedrock 如何處理具有結構化輸出的請求:
-
初始請求 – 您可以透過
outputConfig.textFormat、 或response_format參數包含 JSON 結構描述output_config.format,或在推論請求中包含具有strict: true旗標的工具定義。 -
結構描述驗證 – Amazon Bedrock 會根據支援的 JSON 結構描述草稿 2020 年 12 月子集來驗證 JSON 結構描述格式。如果結構描述包含不支援的功能,Amazon Bedrock 會立即傳回 400 錯誤。
-
第一次編譯 – 對於新的結構描述,Amazon Bedrock 會編譯文法,最多可能需要幾分鐘的時間。
-
快取 – 成功編譯的文法會在第一次存取後快取 24 小時。快取文法會使用 AWS 受管金鑰加密。
-
後續請求 – 來自相同帳戶的相同結構描述使用快取文法,導致推論延遲與具有最少額外負荷的標準請求相當。
-
回應 – 您收到具有嚴格結構描述合規的標準推論回應。
支援的 APIs或功能
您可以在下列 Amazon Bedrock 功能中使用結構化輸出:
Converse 和 ConverseStream APIs – 使用結構化輸出搭配 Converse 和 ConverseStream APIs 進行對話推論。
InvokeModel 和 InvokeModelWithResponseStream APIs – 將結構化輸出與 InvokeModel 和 InvokeModelWithResponseStream APIs搭配使用,以進行一次性推論。
跨區域推論 – 在跨區域推論中使用結構化輸出,無需任何額外設定。
批次推論 – 在批次推論中使用結構化輸出,無需任何額外設定。
注意
結構化輸出與 Anthropic 模型的引數不相容。如果您在使用結構化輸出時啟用引文,模型將傳回 400 錯誤。
支援的模型
特定 Amazon Bedrock 無伺服器模型的所有商業 AWS 區域通常都提供結構化輸出。如需支援的模型清單,請參閱依功能提供的模型支援。
- Anthropic
-
Claude Haiku 4.5 (
anthropic.claude-haiku-4-5-20251001-v1:0)Claude Sonnet 4.5 (
anthropic.claude-sonnet-4-5-20250929-v1:0)Claude Opus 4.5 (
anthropic.claude-opus-4-5-20251101-v1:0)Claude Opus 4.6 (
anthropic.claude-opus-4-6-v1)
- Qwen
-
Qwen3 235B A22B 2507 (
qwen.qwen3-235b-a22b-2507-v1:0)Qwen3 32B (密集) (
qwen.qwen3-32b-v1:0)Qwen3-Coder-30B-A3B-Instruct (
qwen.qwen3-coder-30b-a3b-v1:0)Qwen3 Coder 480B A35B 指示 (
qwen.qwen3-coder-480b-a35b-v1:0)Qwen3 Next 80B A3B (
qwen.qwen3-next-80b-a3b)Qwen3 VL 235B A22B (
qwen.qwen3-vl-235b-a22b)
- OpenAI
-
gpt-oss-120b (
openai.gpt-oss-120b-1:0)gpt-oss-20b (
openai.gpt-oss-20b-1:0)GPT OSS 保護 120B (
openai.gpt-oss-safeguard-120b)GPT OSS 保護 20B (
openai.gpt-oss-safeguard-20b)
- DeepSeek
-
DeepSeek-V3.1 (
deepseek.v3-v1:0)
-
Gemma 3 12B IT (
google.gemma-3-12b-it)Gemma 3 27B PT (
google.gemma-3-27b-it)
- MiniMax
-
MiniMax M2 (
minimax.minimax-m2)
- Mistral AI
-
Magistral Small 2509 (
mistral.magistral-small-2509)最小 3B (
mistral.ministral-3-3b-instruct)Ministral 3 8B (
mistral.ministral-3-8b-instruct)最小 14B 3.0 (
mistral.ministral-3-14b-instruct)Mistral Large 3 (
mistral.mistral-large-3-675b-instruct)Voxtral Mini 3B 2507 (
mistral.voxtral-mini-3b-2507)Voxtral Small 24B 2507 (
mistral.voxtral-small-24b-2507)
- 月亮 AI
-
Kimi K2 思維 (
moonshot.kimi-k2-thinking)
- NVIDIA
-
NVIDIA Nemotron Nano 12B v2 VL BF16 (
nvidia.nemotron-nano-12b-v2)NVIDIA Nemotron Nano 9B v2 (
nvidia.nemotron-nano-9b-v2)
請求範例
JSON 結構描述輸出格式
下列範例示範如何使用 JSON 結構描述輸出格式搭配結構化輸出。
Converse API
{ "messages": [ { "role": "user", "content": [ { "text": "Given the following unstructured data, extract it into the provided structure." }, { "text": "..." } ] } ], "outputConfig": { "textFormat": { "type": "json_schema", "structure": { "jsonSchema": { "schema": "{\"type\": \"object\", \"properties\": {\"title\": {\"type\": \"string\", \"description\": \"title\"}, \"summary\": {\"type\": \"string\", \"description\": \"summary\"}, \"next_steps\": {\"type\": \"string\", \"description\": \"next steps\"}}, \"required\": [\"title\", \"summary\", \"next_steps\"], \"additionalProperties\": false}", "name": "data_extraction", "description": "Extract structured data from unstructured text" } } } } }
InvokeModel (Anthropic Claude)
{ "anthropic_version": "bedrock-2023-05-31", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Given the following unstructured data, extract it into the provided structure." }, { "type": "text", "text": "..." } ] } ], "max_tokens": 3000, "temperature": 1.0, "output_config": { "format": { "type": "json_schema", "schema": { "type": "object", "properties": { "title": { "type": "string", "description": "title" }, "summary": { "type": "string", "description": "summary" }, "next_steps": { "type": "string", "description": "next steps" } }, "required": [ "title", "summary", "next_steps" ], "additionalProperties": false } } } }
InvokeModel (Open-weight 模型)
{ "messages": [ { "role": "user", "content": "Given the following unstructured data, extract it into the provided structure." }, { "role": "user", "content": "..." } ], "inferenceConfig": { "maxTokens": 3000, "temperature": 1.0 }, "response_format": { "json_schema": { "name": "summarizer", "schema": { "type": "object", "properties": { "title": { "type": "string", "description": "title" }, "summary": { "type": "string", "description": "summary" }, "next_steps": { "type": "string", "description": "next steps" } }, "required": [ "title", "summary", "next_steps" ], "additionalProperties": false } }, "type": "json_schema" } }
嚴格的工具使用
下列範例示範如何將嚴格欄位與工具搭配使用。
Converse API
{ "messages": [ { "role": "user", "content": [ { "text": "What's the weather like in New York?" } ] } ], "toolConfig": { "tools": [ { "toolSpec": { "name": "get_weather", "description": "Get the current weather for a specified location", "strict": true, "inputSchema": { "json": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "fahrenheit", "celsius" ], "description": "The temperature unit to use" } }, "required": [ "location", "unit" ] } } } } ] } }
InvokeModel (Anthropic Claude)
{ "anthropic_version": "bedrock-2023-05-31", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What's the weather like in San Francisco?" } ] } ], "max_tokens": 3000, "temperature": 1.0, "tools": [ { "name": "get_weather", "description": "Get the current weather for a specified 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": [ "fahrenheit", "celsius" ], "description": "The temperature unit to use" } }, "required": [ "location", "unit" ], "additionalProperties": false } } ] }
InvokeModel (Open-weight 模型)
{ "messages": [ { "role": "user", "content": "What's the weather like in San Francisco?" } ], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a specified location", "strict": true, "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "fahrenheit", "celsius" ], "description": "The temperature unit to use" } }, "required": [ "location", "unit" ] } } } ], "tool_choice": "auto", "max_tokens": 2000, "temperature": 1.0 }