-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix LiteLLM thinking models with tool calling across providers #812
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix LiteLLM thinking models with tool calling across providers #812
Conversation
- Introduced a new configuration file for permissions in `.claude/settings.local.json`. - Enhanced `LitellmModel` to properly handle assistant messages with tool calls when reasoning is enabled, addressing issue openai#765. - Added a new comprehensive test suite for LiteLLM thinking models to ensure the fix works across various models and scenarios. - Tests include reproducing the original error, verifying successful tool calls, and checking the fix's applicability to different thinking models.
Enhances the fix for issue openai#765 to work universally with all LiteLLM thinking models that support function calling. Verified working: - Anthropic Claude Sonnet 4 (partial fix - progress from "found text" to "found tool_use") - OpenAI o4-mini (complete success - full tool calling with reasoning) The fix now automatically applies when ModelSettings(reasoning=...) is used with any LiteLLM model, making it future-proof for new thinking models that support both reasoning and function calling.
Remove .claude/settings.local.json from git tracking and add it to .gitignore to prevent local Claude Code settings from being committed to the repository.
… maintainability - Cleaned up whitespace and formatting in the test suite for LiteLLM thinking models. - Ensured consistent use of commas and spacing in function calls and assertions. - Verified that the fix for issue openai#765 applies universally across all supported models. - Enhanced documentation within the tests to clarify the purpose and expected outcomes.
Update on Fix StatusThis PR successfully addresses part of issue #765, but there's a limitation that requires upstream work in LiteLLM. What this PR fixes ✅
Current limitation with Anthropic 🟡Testing shows we've made progress with Anthropic models:
Root cause analysisThrough debug logging, I discovered that LiteLLM isn't preserving thinking blocks from previous assistant messages when reconstructing conversation history for Anthropic's API. When thinking is enabled, Anthropic requires:
LiteLLM currently converts our Upstream work neededFull Anthropic support would require LiteLLM to:
This PR provides a valuable partial fix that works for some models (OpenAI o4-mini) and improves the situation for others (Anthropic). |
Related LiteLLM Issue FoundI found a related bug in LiteLLM that confirms the upstream issues with Anthropic thinking models: BerriAI/litellm#11302 This issue shows that LiteLLM has problems handling reasoning/thinking content from Anthropic's API:
This aligns with our findings where LiteLLM:
Our fix addresses what we can at the openai-agents-python level, but full Anthropic thinking model support will require these upstream issues in LiteLLM to be resolved. |
Closing This PRAfter further investigation, I believe this fix belongs in LiteLLM rather than the openai-agents-python SDK. Here's why: Root Cause AnalysisThe issue is that LiteLLM doesn't properly preserve thinking blocks when reconstructing conversation history for Anthropic's API. This is a LiteLLM formatting issue, not an agents SDK problem. Why This Fix Doesn't Belong Here
The Right SolutionThe fix should be implemented in LiteLLM to:
Related Issues
Next StepsI'll update the original issue #765 with this analysis and recommend users:
Thanks for the discussion that helped clarify this should be fixed upstream! |
Summary
Fixes issue #765 by implementing a universal solution for LiteLLM thinking models that support function calling.
Changes
LitellmModel._fix_thinking_model_messages()
to work with any thinking model whenreasoning
is enabledVerified Working
Test Coverage
Impact
The fix automatically applies when
ModelSettings(reasoning=...)
is used with any LiteLLM model, making it future-proof for new thinking models that support both reasoning and function calling.