从模型中获取经过验证的 JSON 结果 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

从模型中获取经过验证的 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 如何处理带有结构化输出的请求:

  1. 初始请求 — 您可以通过outputConfig.textFormat、或response_format参数包含一个 JSON 架构output_config.format,或者在推理请求中加入带有strict: true标志的工具定义。

  2. 架构验证 — Amazon Bedrock 根据支持的 2020-12 年 JSON 架构草案子集验证 JSON 架构格式。如果架构包含不支持的功能,Amazon Bedrock 会立即返回 400 错误。

  3. 首次编译 — 对于新的架构,Amazon Bedrock 会编译语法,这可能需要几分钟。

  4. 缓存 — 成功编译的语法将在首次访问后缓存 24 小时。缓存的语法使用 AWS 托管的密钥进行加密。

  5. 后续请求 — 来自同一账户的相同架构使用缓存的语法,因此推理延迟与标准请求相当,开销最小。

  6. 响应-您会收到具有严格架构合规性的标准推理响应。

支持的 APIs 或功能

您可以在以下 Amazon Bedrock 功能中使用结构化输出:

Converse ConverseStream APIs and — 使用结构化输出和 Converse ConverseStream APIs 进行对话推理。

InvokeModel 和 InvokeModelWithResponseStream APIs — 使用带 InvokeModel 和的结构化输出 InvokeModelWithResponseStream APIs 进行单圈推断。

跨区域推理 — 在跨区域推理中使用结构化输出,无需任何其他设置。

Batch Inf erence — 在批量推理中使用结构化输出,无需任何其他设置。

注意

结构化输出与 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 Instruct () qwen.qwen3-coder-480b-a35b-v1:0

  • Qwen3 Next 80B A3B () qwen.qwen3-next-80b-a3b

  • Qwen3 VL 235B A2B () 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 Safegure 120B () openai.gpt-oss-safeguard-120b

  • GPT OSS Safeguard 20B () openai.gpt-oss-safeguard-20b

DeepSeek
  • DeepSeek-V3.1 () deepseek.v3-v1:0

Google
  • 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

  • Ministral 3B () mistral.ministral-3-3b-instruct

  • Ministral 3 8B () mistral.ministral-3-8b-instruct

  • Ministral 14B 3.0 () mistral.ministral-3-14b-instruct

  • Mistral 大号 (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

月球射击人工智能
  • Kimi K2 Thinking () 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 (人类 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 (开放式重量款)

{ "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 (人类 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 (开放式重量款)

{ "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 }