Skip to content

aws bedrock-runtime converse or invoke to get response in a given JSON schema format

1

is there any option to specify the target JSON schema (something like response_format) for the converse or invoke API call to the response directly in JSON format, without using the toolSpec approach sample in https://community.aws/content/2hWA16FSt2bIzKs0Z1fgJBwu589

asked a year ago1.8K views

1 Answer
2

Yes, there is an option to specify a target JSON schema for the response when using the Amazon Bedrock Converse API, but it's not exactly like a response_format parameter you might find in other APIs. Instead, you can use the toolConfig field in your Converse API request to define the desired JSON schema for the response.

The toolConfig field allows you to specify a tool that helps the model generate responses in a structured format. By defining a JSON schema within the tool configuration, you can guide the model to produce a response that matches your desired structure.

Here's how you can approach this:

  1. Define your desired JSON schema as part of a tool in the toolConfig field of your Converse API request.
  2. Set up the tool with a name, description, and the JSON schema that specifies the structure you want for the response.
  3. Include this tool configuration in your API call.

The model will then attempt to generate a response that conforms to the specified JSON schema. This approach allows you to receive structured data directly from the model without needing to parse unstructured text.

It's important to note that while this method can be effective, the accuracy and consistency of the JSON output will depend on the model's ability to understand and follow the schema. You may need to experiment with different schema designs and prompts to get the best results for your specific use case.

Remember that you'll need to process the tool use response from the model to extract the generated JSON. This typically involves parsing the model's response to find the tool use section and then extracting the JSON data from there.

While this approach using toolConfig is the recommended way to get structured JSON responses from the Bedrock Converse API, it's not a direct equivalent to a simple response_format parameter. It requires more setup but offers greater flexibility in defining complex output structures.
Sources
Community | Generating JSON with the Amazon Bedrock Converse API
Using the Converse API - Amazon Bedrock

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.