Add structured output with tools support for models with limited structured output #2071
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add structured output with tools support for models with limited structured output
Summary
Adds an opt-in structured output with tools feature to enable using tools and structured outputs simultaneously on models that don't natively support both (specifically Google Gemini via LiteLLM).
Problem: Models like Gemini return
BadRequestError: Function calling with a response mime type 'application/json' is unsupportedwhen trying to use tools with structured outputs (viaresponse_schema).Solution: New
enable_structured_output_with_toolsparameter on theAgentclass that:response_formatto avoid API errorsFalsefor backward compatibilityLitellmModel(OpenAI models ignore it as they have native support)Test plan
Unit tests (13 tests added):
tests/utils/test_prompts.py- Tests for prompt generation and injection logicIntegration test:
tests/test_gemini_local.py- Manual test script for Gemini (requires API key)Verification performed:
*20 failures are pre-existing Windows-specific SQLite file locking issues in temporary directory cleanup (not related to this PR). All 13 new tests pass.
Test coverage:
enable_structured_output_with_toolsparameterFalse)Issue number
#2032
Checks
make lintandmake formatDocumentation
Added comprehensive documentation:
docs/models/structured_output_with_tools.md- Complete guide with examplesdocs/agents.md- Added section on using structured outputs with toolsdocs/models/litellm.md- Added example for Gemini integrationmkdocs.yml- Added new doc page to navigationFiles Changed
Source code:
src/agents/agent.py- Addedenable_structured_output_with_toolsparametersrc/agents/util/_prompts.py- New utility for JSON prompt generationsrc/agents/models/interface.py- Added parameter toModelinterfacesrc/agents/models/openai_chatcompletions.py- Pass-through (ignores parameter)src/agents/models/openai_responses.py- Pass-through (ignores parameter)src/agents/extensions/models/litellm_model.py- Active implementationsrc/agents/run.py- Passes parameter to model callsTests:
tests/utils/test_prompts.py- 13 new unit teststests/test_gemini_local.py- Integration testDocumentation:
docs/models/structured_output_with_tools.md- New comprehensive guidedocs/agents.md- Added usage sectiondocs/models/litellm.md- Added Gemini examplemkdocs.yml- Updated navigationImplementation Details
Design decisions:
enable_structured_output_with_tools=Falseensures no impact on existing codeLitellmModelactively uses this; OpenAI models ignore itHow it works:
enable_structured_output_with_tools=Trueon AgentLitellmModelchecks if both tools and output_schema are presentresponse_formatto avoid API errorsExample Usage
Backward Compatibility
✅ Fully backward compatible - All changes are additive:
False)