The Need for Structured Outputs
We’re faced with a common challenge in AI systems: how do we get our agents to output information in a consistent, predictable format that other systems can understand? Think about it like this - our finance bot and summary bot are doing great work analyzing transactions, but if we want to connect them to other business systems, we need something more reliable than hoping they’ll format data consistently each time. This is where structured outputs come in - they provide a way to define the exact structure your agent’s responses should follow. An Analogy for Structured Outputs: Continuing our hospital emergency room analogy:- Think of structured outputs like standardized medical forms used in the hospital
- Without standardized forms, each doctor might document patient information differently (freeform text)
- With standardized forms, all required fields must be completed and in the correct format
- Technicians know exactly where to find vitals, lab results, and medications without guessing
- Downstream systems (insurance, billing, pharmacy) can reliably process these forms
- The forms act as guardrails, ensuring all necessary information is captured and consistently structured
From Concept to Implementation
Implementing structured outputs in AG2 is straightforward using Pydantic models and theresponse_format
parameter:
Not all model providers support structured outputs. For a current list of supported providers, see here.
- The agent will always return responses that match the
ResponseModel
structure - All required fields will be present and correctly typed
- You no longer need formatting instructions in the system message
- The responses can be easily parsed and processed by other systems
Enhancing Our Financial Compliance System with Structured Outputs
Now, let’s enhance our financial compliance system by adding structured outputs to our summary agent:Defining the Structured Output Model
First, let’s define our structured output model for transaction audit logs:Creating the Summary Agent with Structured Output
Next, let’s update our summary agent to use this structured output model:Updating the Termination Condition
We’ll also update our termination condition to recognize the structured output:Using the Structured Output
After the Group Chat completes, we can easily get the final message and parse it as JSON. For example:Understanding the Enhanced Workflow
With structured outputs added to our financial compliance system, our workflow becomes much more reliable:Complete Code Example
Here’s the complete, ready-to-run code for our financial compliance system with structured outputs: ???+ info “Complete Code Example”Example Output
When you run this code, you’ll receive a perfectly structured JSON output (shown here with pretty-printing):Key Benefits of Structured Outputs
Structured outputs offer several compelling benefits:- Consistency: Every response has the same structure, making integration reliable
- Validation: Built-in type checking ensures data integrity
- Simplicity: Reduced need for complex formatting instructions in system messages
- Integration: Easy to parse and use in downstream systems
- Maintainability: Schema changes can be managed centrally
Next Steps
You’ve now learned how to create a complete financial compliance system incorporating the basic concepts of AG2:- LLM Configuration: Powering our agents with the right language models
- ConversableAgent: Creating smart, interactive agents for different tasks
- Human in the Loop: Adding human oversight for suspicious transactions
- Agent Orchestration: Coordinating multiple specialized agents with the Group Chat pattern
- Tools: Extending agent capabilities with duplicate payment detection
- Structured Outputs: Ensuring consistent, validated summary reports