- Notifications
You must be signed in to change notification settings - Fork2.8k
Add structured output with tools support for models with limited structured output#2071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Add structured output with tools support for models with limited structured output#2071
Conversation
support for tools on limited models (e.g., Gemini)Enable using tools and structured outputs together on models (e.g., Gemini) that don't nativelysupport both simultaneously. Introduce an opt-in parameter enable_structured_output_with_toolsto the Agent class, which injects JSON formatting instructions into the system prompt forLitellmModel as a workaround.Changes:- Add enable_structured_output_with_tools parameter to Agent (default: False)- Implement prompt injection utilities in src/agents/util/_prompts.py- Update LitellmModel to inject JSON instructions when enabled- Extend model interfaces to accept enable_structured_output_with_tools- Add comprehensive unit tests (13 total) and one integration test- Add documentation in docs/models/structured_output_with_tools.md- Update docs/agents.md and docs/models/litellm.md with usage examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When yousign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Uh oh!
There was an error while loading.Please reload this page.
when enabledMaintain backward compatibility with third-party Model implementations byonly passing the enable_structured_output_with_tools parameter when it'sexplicitly enabled (True). This prevents TypeErrors in custom Model classesthat don't support the new parameter yet.- Built-in models have default False, so they work either way- Third-party models without the parameter won't crash- Feature still works when explicitly enabledFixes backward compatibility issue raised in code review.
seratch commentedNov 10, 2025
@devv-shayan Thanks for looking into this. However, as I mentioned at#2032 (comment), we prefer resolving this only by the changes on the LiteLLM or our LiteLLM adapter side. So, we don't accept this large diff for this. |
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)