fix(backends/litellm): map Anthropic tool_choice none to OpenAI none (#2689)

## Description

`_convert_tool_choice` in the LiteLLM backend converts an Anthropic
`tool_choice` into the OpenAI shape. It handled `{"type": "auto"}`,
`{"type": "any"}` and `{"type": "tool", "name": ...}`, but there was no
branch for Anthropic's `{"type": "none"}`, so it fell through to the
`return "auto"` default at the end of the function.

That inverts the instruction. A client that sends `tool_choice: {"type":
"none"}` is saying "do not use any tool this turn" (tools stay defined,
but the model must not call one). When such a request is routed through
the LiteLLM backend (Bedrock, Vertex, OpenRouter, and the other
providers this backend serves), the choice was rewritten to `"auto"`,
which tells the model it may call a tool. So the model could invoke a
tool the client explicitly forbade.

The string form already passed through unchanged
(`_convert_tool_choice("none") == "none"`, covered by an existing test);
only the Anthropic dict form, which is what Anthropic clients actually
send, was mishandled. OpenAI's equivalent is the string `"none"`, so
this maps `{"type": "none"}` to `"none"`.

Closes #

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `headroom/backends/litellm.py` (`_convert_tool_choice`): added a
`{"type": "none"}` to `"none"` branch and updated the docstring shapes.
- `tests/test_backend_bugs.py`: added
`test_none_dict_not_inverted_to_auto` asserting the dict form maps to
`"none"` (it previously returned `"auto"`).

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

```text
# Fail-before (source fix stashed, new test kept):
tests/test_backend_bugs.py::TestConvertToolChoice::test_none_dict_not_inverted_to_auto FAILED
  assert _convert_tool_choice({"type": "none"}) == "none"
  AssertionError: assert 'auto' == 'none'

# Pass-after (fix applied):
tests/test_backend_bugs.py ...........................................
43 passed in 4.83s

# uvx ruff@0.15.17 check  -> All checks passed!
# uvx mypy@1.20.2 headroom/backends/litellm.py -> Success: no issues found in 1 source file
```

## Real Behavior Proof

- Environment: Windows 11, Python 3.12.11, litellm installed in the
project venv, pytest 9.1.1.
- Exact command / steps: fail-before with `git stash push
headroom/backends/litellm.py` and `python -m pytest
tests/test_backend_bugs.py -k TestConvertToolChoice` (the new assertion
fails, `_convert_tool_choice({"type": "none"})` returns `"auto"`), then
pass-after with `git stash pop` and rerunning the file (43 passed).
- Observed result: `_convert_tool_choice({"type": "none"})` now returns
`"none"` instead of `"auto"`, so an Anthropic client that forbids tool
use keeps that instruction when the request is routed through the
LiteLLM backend. The existing auto / any / tool / string cases are
unchanged.
- Not tested: a live end-to-end call to a provider with a real "none"
tool_choice (no provider credentials in this environment). The
conversion is a pure function verified directly, and the downstream
provider is responsible for honoring OpenAI's `"none"`.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`: it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Additional Notes

Docs and manual testing are N/A: this is a pure conversion-correctness
fix with no API surface change. The fallback `return "auto"` is kept for
genuinely unknown future types; only the known "none" type is now mapped
explicitly.

Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
Abhay Singh
2026-10-02 11:07:19 -05:00
committed by GitHub
co-authored by JD Davis
parent 95ae69a85f
commit 57a57088de
2 changed files with 15 additions and 2 deletions
+9 -2
View File
@@ -606,8 +606,8 @@ def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:
def _convert_tool_choice(choice: Any) -> Any:
"""Convert Anthropic tool_choice to OpenAI format.
Anthropic: {"type": "auto"}, {"type": "any"}, {"type": "tool", "name": "..."}
OpenAI: "auto", "required", {"type": "function", "function": {"name": "..."}}
Anthropic: {"type": "auto"}, {"type": "any"}, {"type": "none"}, {"type": "tool", "name": "..."}
OpenAI: "auto", "required", "none", {"type": "function", "function": {"name": "..."}}
"""
if isinstance(choice, str):
return choice
@@ -617,6 +617,13 @@ def _convert_tool_choice(choice: Any) -> Any:
return "auto"
if choice_type == "any":
return "required"
if choice_type == "none":
# Anthropic's {"type": "none"} means "do not use any tool this turn".
# Without this branch it fell through to the "auto" default below,
# inverting the instruction into "you may use tools" — the model
# could then call a tool the client explicitly forbade. OpenAI's
# equivalent is the string "none".
return "none"
if choice_type == "tool":
return {"type": "function", "function": {"name": choice.get("name", "")}}
return "auto"
+6
View File
@@ -81,6 +81,12 @@ class TestConvertToolChoice:
result = _convert_tool_choice({"type": "tool", "name": "get_weather"})
assert result == {"type": "function", "function": {"name": "get_weather"}}
def test_none_dict_not_inverted_to_auto(self):
# Anthropic sends {"type": "none"} to forbid tool use this turn. It must
# map to OpenAI's "none", not fall through to the "auto" default (which
# would let the model call a tool the client explicitly disallowed).
assert _convert_tool_choice({"type": "none"}) == "none"
def test_string_passthrough(self):
assert _convert_tool_choice("auto") == "auto"
assert _convert_tool_choice("none") == "none"