Generative AI is revolutionizing industries by streamlining operations and enabling innovation. Whereas textual chat interactions with GenAI stay common, real-world purposes usually depend upon structured knowledge for APIs, databases, data-driven workloads, and wealthy consumer interfaces. Structured knowledge may also improve conversational AI, enabling extra dependable and actionable outputs. A key problem is that LLMs (Massive Language Fashions) are inherently unpredictable, which makes it troublesome for them to provide constantly structured outputs like JSON. This problem arises as a result of their coaching knowledge primarily consists of unstructured textual content, equivalent to articles, books, and web sites, with comparatively few examples of structured codecs. Consequently, LLMs can wrestle with precision when producing JSON outputs, which is essential for seamless integration into present APIs and databases. Fashions range of their capability to help structured responses, together with recognizing knowledge sorts and managing advanced hierarchies successfully. These capabilities could make a distinction when choosing the proper mannequin.
This weblog demonstrates how Amazon Bedrock, a managed service for securely accessing high AI fashions, may help deal with these challenges by showcasing two different choices:
- Immediate Engineering: A simple strategy to shaping structured outputs utilizing well-crafted prompts.
- Software Use with the Bedrock Converse API: A complicated technique that permits higher management, consistency, and native JSON schema integration.
We are going to use a buyer evaluation evaluation instance to reveal how Bedrock generates structured outputs, equivalent to sentiment scores, with simplified Python code.
Constructing a immediate engineering resolution
This part will reveal tips on how to use immediate engineering successfully to generate structured outputs utilizing Amazon Bedrock. Immediate engineering entails crafting exact enter prompts to information giant language fashions (LLMs) in producing constant and structured responses. It’s a elementary method for creating Generative AI purposes, notably when structured outputs are required.Listed here are the 5 key steps we are going to comply with:
- Configure the Bedrock shopper and runtime parameters.
- Create a JSON schema for structured outputs.
- Craft a immediate and information the mannequin with clear directions and examples.
- Add a buyer evaluation as enter knowledge to analyse.
- Invoke Bedrock, name the mannequin, and course of the response.
Whereas we reveal buyer evaluation evaluation to generate a JSON output, these strategies may also be used with different codecs like XML or CSV.
Step 1: Configure Bedrock
To start, we’ll arrange some constants and initialize a Python Bedrock shopper connection object utilizing the Python Boto3 SDK for Bedrock runtime, which facilitates interplay with Bedrock:
The REGION specifies the AWS area for mannequin execution, whereas the MODEL_ID identifies the precise Bedrock mannequin. The TEMPERATURE fixed controls the output randomness, the place greater values improve creativity, and decrease values keep precision, equivalent to when producing structured output. MAX_TOKENS determines the output size, balancing cost-efficiency and knowledge completeness.
Step 2: Outline the Schema
Defining a schema is crucial for facilitating structured and predictable mannequin outputs, sustaining knowledge integrity, and enabling seamless API integration. With out a well-defined schema, fashions could generate inconsistent or incomplete responses, resulting in errors in downstream purposes. The JSON standard schema used within the code beneath serves as a blueprint for structured knowledge era, guiding the mannequin on tips on how to format its output with specific directions.
Let’s create a JSON schema for buyer opinions with three required fields: reviewId (string, max 50 chars), sentiment (quantity, -1 to 1), and abstract (string, max 200 chars).
Step 3: Craft the Immediate textual content
To generate constant, structured, and correct responses, prompts should be clear and well-structured, as LLMs depend on exact enter to provide dependable outputs. Poorly designed prompts can result in ambiguity, errors, or formatting points, disrupting structured workflows, so we comply with these finest practices:
- Clearly define the AI’s function and targets to keep away from ambiguity.
- Divide duties into smaller, manageable numbered steps for readability.
- Point out {that a} JSON schema can be supplied (see Step 5 beneath) to take care of a constant and legitimate construction.
- Use one-shot prompting with a pattern output to information the mannequin; add extra examples if wanted for consistency, however keep away from too many, as they might restrict the mannequin’s capability to deal with new inputs.
- Outline tips on how to deal with lacking or invalid knowledge.

Step 4: Combine Enter Information
For demonstration functions, we’ll embrace a evaluation textual content within the immediate as a Python variable:

Separating the enter knowledge with <enter> tags enhance readability and readability, making it easy to determine and reference. This hardcoded enter simulates real-world knowledge integration. For manufacturing use, you would possibly dynamically populate enter knowledge from APIs or consumer submissions.
Step 5: Name Bedrock
On this part, we assemble a Bedrock request by defining a physique object that features the JSON schema, immediate, and enter evaluation knowledge from earlier steps. This structured request makes certain the mannequin receives clear directions, adheres to a predefined schema, and processes pattern enter knowledge appropriately. As soon as the request is ready, we invoke Amazon Bedrock to generate a structured JSON response.

We reuse the MAX_TOKENS, TEMPERATURE, and MODEL_ID constants outlined in Step 1. The physique object has important inference configurations like anthropic_version for mannequin compatibility and the messages array, which features a single message to offer the mannequin with process directions, the schema, and the enter knowledge. The function defines the “speaker” within the interplay context, with consumer worth representing this system sending the request. Alternatively, we may simplify the enter by combining directions, schema, and knowledge into one textual content immediate, which is simple to handle however much less modular.
Lastly, we use the shopper.invoke_model technique to ship the request. After invoking, the mannequin processes the request, and the JSON knowledge should be correctly (not defined right here) extracted from the Bedrock response. For instance:

Software Use with the Amazon Bedrock Converse API
Within the earlier chapter, we explored an answer utilizing Bedrock Immediate Engineering. Now, let’s have a look at another strategy for producing structured responses with Bedrock.
We are going to lengthen the earlier resolution through the use of the Amazon Bedrock Converse API, a constant interface designed to facilitate multi-turn conversations with Generative AI fashions. The API abstracts model-specific configurations, together with inference parameters, simplifying integration.
A key function of the Converse API is Software Use (also referred to as Perform Calling), which permits the mannequin to execute exterior instruments, equivalent to calling an exterior API. This technique helps commonplace JSON schema integration straight into instrument definitions, facilitating output alignment with predefined codecs. Not all Bedrock fashions help Software Use, so be sure you verify which fashions are suitable with these function.
Constructing on the beforehand outlined knowledge, the next code offers an easy instance of Software Use tailor-made to our curstomer evaluation use case:

On this code the tool_list defines a customized buyer evaluation evaluation instrument with its enter schema and function, whereas the messages present the sooner outlined directions and enter knowledge. In contrast to within the earlier immediate engineering instance we used the sooner outlined JSON schema within the definition of a instrument. Lastly, the shopper.converse name combines these elements, specifying the instrument to make use of and inference configurations, leading to outputs tailor-made to the given schema and process. After exploring Immediate Engineering and Software Use in Bedrock options for structured response era, let’s now consider how totally different basis fashions carry out throughout these approaches.
Check Outcomes: Claude Fashions on Amazon Bedrock
Understanding the capabilities of basis fashions in structured response era is crucial for sustaining reliability, optimizing efficiency, and constructing scalable, future-proof Generative AI purposes with Amazon Bedrock. To judge how effectively fashions deal with structured outputs, we carried out in depth testing of Anthropic’s Claude fashions, evaluating prompt-based and tool-based approaches throughout 1,000 iterations per mannequin. Every iteration processed 100 randomly generated gadgets, offering broad take a look at protection throughout totally different enter variations.The examples proven earlier on this weblog are deliberately simplified for demonstration functions, the place Bedrock carried out seamlessly with no points. To higher assess the fashions beneath real-world challenges, we used a extra advanced schema that featured nested constructions, arrays, and numerous knowledge sorts to determine edge circumstances and potential points. The outputs had been validated for adherence to the JSON format and schema, sustaining consistency and accuracy. The next diagram summarizes the outcomes, exhibiting the variety of profitable, legitimate JSON responses for every mannequin throughout the 2 demonstrated approaches: Immediate Engineering and Software Use.

The outcomes demonstrated that each one fashions achieved over 93% success throughout each approaches, with Software Use strategies constantly outperforming prompt-based ones. Whereas the analysis was carried out utilizing a extremely advanced JSON schema, easier schemas end in considerably fewer points, usually almost none. Future updates to the fashions are anticipated to additional improve efficiency.
Closing Ideas
In conclusion, we demonstrated two strategies for producing structured responses with Amazon Bedrock: Immediate Engineering and Software Use with the Converse API. Immediate Engineering is versatile, works with Bedrock fashions (together with these with out Software Use help), and handles numerous schema sorts (e.g., Open API schemas), making it an excellent place to begin. Nevertheless, it may be fragile, requiring precise prompts and fighting advanced wants. However, Software Use affords larger reliability, constant outcomes, seamless API integration, and runtime validation of JSON schema for enhanced management.
For simplicity, we didn’t reveal a couple of areas on this weblog. Different methods for producing structured responses embrace utilizing fashions with built-in help for configurable response codecs, equivalent to JSON, when invoking fashions, or leveraging constraint decoding methods with third-party libraries like LMQL. Moreover, producing structured knowledge with GenAI may be difficult on account of points like invalid JSON, lacking fields, or formatting errors. To keep up knowledge integrity and deal with surprising outputs or API failures, efficient error dealing with, thorough testing, and validation are important.
To strive the Bedrock methods demonstrated on this weblog, comply with the steps to Run instance Amazon Bedrock API requests by way of the AWS SDK for Python (Boto3). With pay-as-you-go pricing, you’re solely charged for API calls, so little to no cleanup is required after testing. For extra particulars on finest practices, check with the Bedrock immediate engineering tips and model-specific documentation, equivalent to Anthropic’s best practices.
Structured knowledge is vital to leveraging Generative AI in real-world eventualities like APIs, data-driven workloads, and wealthy consumer interfaces past text-based chat. Begin utilizing Amazon Bedrock immediately to unlock its potential for dependable structured responses.
Concerning the authors
Adam Nemeth is a Senior Options Architect at AWS, the place he helps world monetary prospects embrace cloud computing by way of architectural steerage and technical help. With over 24 years of IT experience, Adam beforehand labored at UBS earlier than becoming a member of AWS. He lives in Switzerland along with his spouse and their three kids.
Dominic Searle is a Senior Options Architect at Amazon Net Companies, the place he has had the pleasure of working with World Monetary Companies prospects as they discover how Generative AI may be built-in into their expertise methods. Offering technical steerage, he enjoys serving to prospects successfully leverage AWS Companies to unravel actual enterprise issues.


