Repository navigation
fix(azure): drop tool_choice when the request has no tools (internal copy of #41781) - #42031
Conversation
DEVX-829 Co-Authored-By: Claude Code <noreply@anthropic.com>
DEVX-829 Co-Authored-By: Claude Code <noreply@anthropic.com>
…_tool_choice # Conflicts: # tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 0ae5d7c. Configure here.
Internal copy of #41781 by @dsteeley into
main, opened so CircleCI runs on alitellm_head. Original head:dsteeley/litellm:fix/azure-tool-choice-without-tools@0ae5d7c2fa. Commits and authorship are unchangedTLDR
Problem this solves:
tool_choicearrives withouttoolsorfunctionstool_choicecannot use Azure models through the proxyHow it solves it:
tool_choicefrom the Azure request when no tools or functions are settool_choice, are sent unchangedUser Flow
Before: a developer whose client always sends
tool_choicegets a 400 from an Azure OpenAI model whenever the request carries no tools"model": "azure-gpt-4o","tool_choice": "none"(or"auto"), and notoolsorfunctionslitellm.BadRequestError: AzureException BadRequestError - Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified."tool_choice": {"type": "auto"}and no tools, and with"stream": truetool_choiceand it returns HTTP 200After: the same request returns a normal completion
"model": "azure-gpt-4o","tool_choice": "none"(or"auto"), and notoolsorfunctions"tool_choice": {"type": "auto"}and no tools, and the streaming variant, return HTTP 200 as welltool_choicestill returns HTTP 200, and a request withtoolsplus"tool_choice": "required"still returns the tool callRelevant issues
Follows the same fix already made for the OpenAI provider in #39147, commit
93a03a9ffd04Affected release
Linear ticket
Resolves LIT-8184
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Two proxies were booted from the same config against the same live Azure OpenAI resource, one from the merge base and one from this PR's tip, and the same five curls were run against each. A forwarding recorder in front of the Azure endpoint on both sides showed exactly one upstream call per request, and the only difference in what the proxy sent to Azure is that
tool_choiceis missing when the request has no toolsConfig (
api_baseandapi_keycome from the environment):Boot:
python litellm/proxy/proxy_cli.py --config qa.config.yaml --host 127.0.0.1 --port <port>(port 33238 for Before, 57939 for After)Before (d675c12)
Chat completion, gpt-4o, tool_choice=none, no tools
curl -s http://127.0.0.1:33238/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":"none"}'{"error":{"message":"litellm.BadRequestError: AzureException BadRequestError - Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.. Received Model Group=azure-gpt-4o\nAvailable Model Group Fallbacks=None","type":"invalid_request_error","param":"tool_choice","code":"400"}}Chat completion, gpt-5.6, tool_choice=auto, no tools
curl -s http://127.0.0.1:33238/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-5-6-luna","messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":"auto"}'{"error":{"message":"litellm.BadRequestError: AzureException BadRequestError - Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.. Received Model Group=azure-gpt-5-6-luna\nAvailable Model Group Fallbacks=None","type":"invalid_request_error","param":"tool_choice","code":"400"}}Messages API, gpt-4o, tool_choice auto, no tools
curl -s http://127.0.0.1:33238/v1/messages -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","max_tokens":50,"messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":{"type":"auto"}}'{"type":"error","error":{"type":"invalid_request_error","message":"litellm.BadRequestError: AzureException BadRequestError - Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.. Received Model Group=azure-gpt-4o\nAvailable Model Group Fallbacks=None"}}Chat completion, gpt-4o, tools plus tool_choice=required (unchanged)
curl -s http://127.0.0.1:33238/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","messages":[{"role":"user","content":"Weather in Paris?"}],"tools":[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}],"tool_choice":"required"}'{"id":"chatcmpl-EPy0SyZwPIPeWQGUywa1XsnmGureu","model":"azure-gpt-4o","choices":[{"message":{"role":"assistant","content":null,"tool_calls":[{"function":{"name":"get_weather","arguments":"{\"city\":\"Paris\"}"}}]},"finish_reason":"tool_calls"}]}Chat completion, gpt-4o, stream plus tool_choice=none, no tools
curl -s -N http://127.0.0.1:33238/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":"none","stream":true}'{"error":{"message":"litellm.BadRequestError: AzureException BadRequestError - Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.. Received Model Group=azure-gpt-4o\nAvailable Model Group Fallbacks=None","type":"invalid_request_error","param":"tool_choice","code":"400"}}After (0ae5d7c)
Chat completion, gpt-4o, tool_choice=none, no tools
curl -s http://127.0.0.1:57939/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":"none"}'{"id":"chatcmpl-EPy0UIooyfEZl8PiBSzuWcpemQu9E","model":"azure-gpt-4o","choices":[{"message":{"role":"assistant","content":"Hi there!","tool_calls":null},"finish_reason":"stop"}]}Chat completion, gpt-5.6, tool_choice=auto, no tools
curl -s http://127.0.0.1:57939/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-5-6-luna","messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":"auto"}'{"id":"chatcmpl-EPy0VjcL34dPZ0BNYRGVxXTuPrV8w","model":"azure-gpt-5-6-luna","choices":[{"message":{"role":"assistant","content":"Hi there!","tool_calls":null},"finish_reason":"stop"}]}Messages API, gpt-4o, tool_choice auto, no tools
curl -s http://127.0.0.1:57939/v1/messages -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","max_tokens":50,"messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":{"type":"auto"}}'{"id":"chatcmpl-EPy0WkHVo0ecH8tCT6Dyn4gX6yUac","model":"azure-gpt-4o","content":[{"type":"text","text":"Hi there!"}],"stop_reason":"end_turn"}Chat completion, gpt-4o, tools plus tool_choice=required (unchanged)
curl -s http://127.0.0.1:57939/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","messages":[{"role":"user","content":"Weather in Paris?"}],"tools":[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}],"tool_choice":"required"}'{"id":"chatcmpl-EPy0XxrQ8tuudRoKLl9GkWlX5xEhw","model":"azure-gpt-4o","choices":[{"message":{"role":"assistant","content":null,"tool_calls":[{"function":{"name":"get_weather","arguments":"{\"city\":\"Paris\"}"}}]},"finish_reason":"tool_calls"}]}Chat completion, gpt-4o, stream plus tool_choice=none, no tools
curl -s -N http://127.0.0.1:57939/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model":"azure-gpt-4o","messages":[{"role":"user","content":"Say hi in two words"}],"tool_choice":"none","stream":true}'[DONE]Type
🐛 Bug Fix
Caveats (if any)
Medium
tool_choicewithout toolstool_choiceon tool-less requests via that gatewayLow
tool_choicethrough the OpenAI base transform from fix(openai): drop tool_choice when request has no tools on chat completions #39147tool_choicewithout tools (checked live), so/v1/responsesneeds no changeintegration-extensionsis red at collection on every branch cut after refactor(mcp): upgrade SDK2 while preserving legacy gateway behavior #41718 (merged 18:53Z) and at this PR's merge base with no PR commits:mcp2.2.0 from the lockfile has nomcp.server.fastmcp, whichtests/integration/_support/mcp.pyandtests/mcp_tests/*still import. Nothing in this diff touches MCPintegration-costfailstest_cost_tracking.py:81(spend 0.0012456 vs 0.0021672) identically on main's own 18:09Z pipeline at 2886b8e, before this PR's merge base, and on all 9 other pipelines since. Nothing in this diff touches cost trackingproxy_e2e_anthropic_messages_testsfails the two Bedrocktest_bedrock_invoke_messages_with_all_beta_headerscases withinvalid beta flag, red on every main pipeline since 2026-09-19 00:09Z (tracked as LIT-8149). Nothing in this diff touches BedrockFinal Attestation
The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
0ae5d7c passes /live-pr-risk