What Are Structured Outputs?
Structured outputs force the model to return data in a specific format:Function Calling
Model calls predefined functions with typed parameters
Response Format
Model returns JSON matching a specified schema
strict: true to guarantee schema compliance.
Prerequisites
- Python 3.8+ or Node.js 18+
- OpenAI API key
- Helicone API key (sign up free)
Setup
- Python
- TypeScript
.env:What We’ll Build
A flight booking assistant that:- Extracts search parameters using function calling
- Searches a database with extracted parameters
- Formats results using structured outputs
- Tracks everything in Helicone
Implementation
Step 1: Define Data Models
- Python
- TypeScript
Step 2: Initialize OpenAI with Helicone
- Python
- TypeScript
Step 3: Use Function Calling to Extract Parameters
- Python
- TypeScript
Step 4: Search Flight Database
Simulate a database search:- Python
- TypeScript
Step 5: Format Response with Structured Outputs
- Python
- TypeScript
Step 6: Handle Refusals
Structured outputs can refuse unsafe requests:- Python
- TypeScript
Step 7: Track Refusals in Helicone
Filter for refused requests in your dashboard:- Go to Helicone Requests
- Add filter:
refusalexists - Review why requests were refused
- False positives (safe requests incorrectly refused)
- Patterns in refused content
- Opportunities to improve prompts
Complete Flight Assistant
Put everything together:- Python
- TypeScript
Monitoring in Helicone
View your structured outputs in the dashboard:Session View
Each query creates a session with:- Parameter extraction request
- Response formatting request
Filter by Refusals
To see refused requests:- Go to Requests
- Add filter:
refusalfield exists - Review and adjust prompts if needed
Track Tool Usage
Filter byStep property:
parameter-extraction- How often is extraction called?response-formatting- How many results are formatted?
Best Practices
Always Use strict: true
Always Use strict: true
Setting
strict: true guarantees schema compliance. Without it, the model may return invalid data.Handle Refusals Gracefully
Handle Refusals Gracefully
Always check
message.refusal and provide fallback responses. Track refusals in Helicone to identify patterns.Keep Schemas Simple
Keep Schemas Simple
Complex nested schemas are harder for models to follow. Start simple and add complexity only when needed.
Use Descriptive Field Names
Use Descriptive Field Names
Clear field names and descriptions help the model understand what you want. Use
description liberally.Test Edge Cases
Test Edge Cases
Test with unusual inputs, missing data, and ambiguous queries. Track failures in Helicone.
Common Issues
Schema Validation Errors
If you see validation errors:Refusals on Valid Requests
If safe requests are refused:- Review refusal reasons in Helicone
- Adjust system prompt to be clearer
- Add examples of acceptable requests
Inconsistent Outputs
If outputs vary despite schemas:- Check
strict: trueis set - Use temperature 0 for deterministic results
- Provide clearer field descriptions
Next Steps
Agent Tracing
Build agents with multiple tool calls
Vercel AI Gateway
Add model routing and complexity classification
Custom Properties
Track structured output usage with metadata
OpenAI Docs
Deep dive into OpenAI’s structured outputs
