Skip to content

fix(azure): drop tool_choice when the request has no tools (internal copy of #41781) - #42031

Merged
mateo-berri merged 5 commits into
mainfrom
litellm_internal_copy_41781
Sep 19, 2026
Merged

mateo-berri merged 5 commits into
mainfrom
litellm_internal_copy_41781

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Internal copy of #41781 by @dsteeley into main, opened so CircleCI runs on a litellm_ head. Original head: dsteeley/litellm:fix/azure-tool-choice-without-tools @ 0ae5d7c2fa. Commits and authorship are unchanged

TLDR

Problem this solves:

  • Azure returns 400 when tool_choice arrives without tools or functions
  • Clients that always send tool_choice cannot use Azure models through the proxy

How it solves it:

User Flow

Before: a developer whose client always sends tool_choice gets a 400 from an Azure OpenAI model whenever the request carries no tools

  1. They send POST https://litellm-domain/v1/chat/completions with "model": "azure-gpt-4o", "tool_choice": "none" (or "auto"), and no tools or functions
  2. The response is HTTP 400 with litellm.BadRequestError: AzureException BadRequestError - Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.
  3. The same happens on POST https://litellm-domain/v1/messages with "tool_choice": {"type": "auto"} and no tools, and with "stream": true
  4. They send the same request again without tool_choice and it returns HTTP 200

After: the same request returns a normal completion

  1. They send the same POST https://litellm-domain/v1/chat/completions with "model": "azure-gpt-4o", "tool_choice": "none" (or "auto"), and no tools or functions
  2. The response is HTTP 200 with the assistant's message
  3. POST https://litellm-domain/v1/messages with "tool_choice": {"type": "auto"} and no tools, and the streaming variant, return HTTP 200 as well
  4. The same request without tool_choice still returns HTTP 200, and a request with tools plus "tool_choice": "required" still returns the tool call

Relevant issues

Follows the same fix already made for the OpenAI provider in #39147, commit 93a03a9ffd04

Affected release

Linear ticket

Resolves LIT-8184

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to 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_choice is missing when the request has no tools

Config (api_base and api_key come from the environment):

model_list:
  - model_name: azure-gpt-5-6-luna
    litellm_params:
      model: azure/gpt-5.6-luna
      api_base: os.environ/AZURE_FOUNDRY_API_BASE
      api_key: os.environ/AZURE_FOUNDRY_API_KEY
      api_version: "2025-04-01-preview"
  - model_name: azure-gpt-4o
    litellm_params:
      model: azure/gpt-4o
      api_base: os.environ/AZURE_FOUNDRY_API_BASE
      api_key: os.environ/AZURE_FOUNDRY_API_KEY
      api_version: "2025-04-01-preview"

general_settings:
  master_key: sk-1234

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

  1. 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"}'
  2. HTTP 400
{"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

  1. 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"}'
  2. HTTP 400
{"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

  1. 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"}}'
  2. HTTP 400
{"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)

  1. 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"}'
  2. HTTP 200, the tool call comes back
{"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

  1. 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}'
  2. HTTP 400, no chunks
{"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

  1. 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"}'
  2. HTTP 200
{"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

  1. 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"}'
  2. HTTP 200
{"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

  1. 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"}}'
  2. HTTP 200
{"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)

  1. 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"}'
  2. HTTP 200, the same tool call comes back
{"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

  1. 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}'
  2. HTTP 200, five chunks then [DONE]
data: {"id":"chatcmpl-EPy0YpUS4yU1dMr7ulOuCsLClbbdd","model":"azure-gpt-4o","object":"chat.completion.chunk","choices":[{"content_filter_results":{},"index":0,"delta":{"content":"","role":"assistant"}}],"service_tier":"default","obfuscation":"jsGwlfFA6IjTNv"}
data: {"id":"chatcmpl-EPy0YpUS4yU1dMr7ulOuCsLClbbdd","model":"azure-gpt-4o","object":"chat.completion.chunk","choices":[{"content_filter_results":{"protected_material_text":{"detected":false,"filtered":false}},"index":0,"delta":{"content":"Hi"}}],"service_tier":"default","obfuscation":"bgU8yebUAGFeO7"}
data: {"id":"chatcmpl-EPy0YpUS4yU1dMr7ulOuCsLClbbdd","model":"azure-gpt-4o","object":"chat.completion.chunk","choices":[{"content_filter_results":{"protected_material_text":{"detected":false,"filtered":false}},"index":0,"delta":{"content":" there"}}],"service_tier":"default","obfuscation":"Jvhwa1Cq2A"}
data: {"id":"chatcmpl-EPy0YpUS4yU1dMr7ulOuCsLClbbdd","model":"azure-gpt-4o","object":"chat.completion.chunk","choices":[{"content_filter_results":{"protected_material_text":{"detected":false,"filtered":false}},"index":0,"delta":{"content":"!"}}],"service_tier":"default","obfuscation":"ubkti5gKT35u3Hh"}
data: {"id":"chatcmpl-EPy0YpUS4yU1dMr7ulOuCsLClbbdd","model":"azure-gpt-4o","object":"chat.completion.chunk","choices":[{"finish_reason":"stop","index":0,"delta":{}}],"service_tier":"default","obfuscation":"NbB3ujnkW5"}
data: [DONE]

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Pre-existing, not touched here: Azure through Cloudflare AI Gateway still forwards tool_choice without tools
    • That path builds the request body directly and skips the transform this PR changes
    • Workaround until a follow-up: do not send tool_choice on tool-less requests via that gateway

Low

  • Azure o-series deployments already drop a lone tool_choice through the OpenAI base transform from fix(openai): drop tool_choice when request has no tools on chat completions #39147
  • Azure's Responses API accepts tool_choice without tools (checked live), so /v1/responses needs no change
  • Proof was driven with curl against the proxy; the report named no specific client application
  • CircleCI integration-extensions is 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: mcp 2.2.0 from the lockfile has no mcp.server.fastmcp, which tests/integration/_support/mcp.py and tests/mcp_tests/* still import. Nothing in this diff touches MCP
  • CircleCI integration-cost fails test_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 tracking
  • CircleCI proxy_e2e_anthropic_messages_tests fails the two Bedrock test_bedrock_invoke_messages_with_all_beta_headers cases with invalid beta flag, red on every main pipeline since 2026-09-19 00:09Z (tracked as LIT-8149). Nothing in this diff touches Bedrock

Final 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

dsteeley and others added 5 commits September 17, 2026 07:07
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
@devin-ai-integration

devin-ai-integration Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge because all examined Azure chat paths remove standalone tool_choice while preserving valid tool-backed requests

Summary

This PR updates Azure chat request transformation to omit tool_choice when neither modern tools nor legacy functions are present. It preserves the caller's parameter mapping, applies to synchronous, asynchronous, and GPT-5 transformation paths, and adds regression coverage for absent, empty, and populated tool configurations

Reviews (1) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..."

@codspeed

codspeed Bot commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_internal_copy_41781 (0ae5d7c) with main (bdbb4e4)

Open in CodSpeed

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mateo-berri
mateo-berri merged commit 56408b3 into main Sep 19, 2026
240 of 244 checks passed
@mateo-berri
mateo-berri deleted the litellm_internal_copy_41781 branch September 19, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants