mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(backends/anyllm): map Anthropic tool_choice none to OpenAI none (#3963)
## Description
`AnyLLMBackend._convert_tool_choice` translates an Anthropic
`tool_choice` into the OpenAI shape that any-llm speaks. It handled
`auto`, `any`, and `tool`, but **not** `{"type": "none"}`, so that value
fell through to the final `return "auto"`.
`{"type": "none"}` means *"do not use any tool this turn."* Converting
it to `"auto"` inverts the instruction into *"you may use tools,"* so
the model can call a tool the client explicitly forbade. The litellm
backend already maps this case to OpenAI's `"none"` (see
`headroom/backends/litellm.py`); this brings the any-llm backend in
line. The any-llm docstring even claimed it "mirrors LiteLLM" while
omitting this branch.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
## Changes Made
- Add a `choice_type == "none"` branch to
`AnyLLMBackend._convert_tool_choice` returning OpenAI `"none"`.
- Update the docstring to list the `none` case.
- Tests: a parametrized unit test covering all four Anthropic
`tool_choice` types, plus an end-to-end `send_message` test asserting
`{"type": "none"}` reaches any-llm as `"none"`.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ pytest tests/test_backend_anyllm.py -k "tool_choice_none or covers_every" -q # before fix
FAILED tests/test_backend_anyllm.py::test_convert_tool_choice_covers_every_anthropic_type[anthropic_choice2-none]
FAILED tests/test_backend_anyllm.py::test_send_message_forwards_tool_choice_none
2 failed, 3 passed
$ pytest tests/test_backend_anyllm.py -q # after fix
26 passed
$ ruff check headroom/backends/anyllm.py tests/test_backend_anyllm.py
All checks passed!
```
## Real Behavior Proof
- Environment: macOS (arm64), Python 3.13, local editable install
(`maturin develop --release`), any-llm request path driven through
`AnyLLMBackend.send_message` with the outbound provider call captured.
- Exact command / steps: build a request with `tool_choice={"type":
"none"}` and one tool, run `AnyLLMBackend.send_message`, and print the
`tool_choice` actually forwarded to the any-llm provider, before and
after the patch.
- Observed result: The Anthropic tool_choice none request changed from
auto to none at the captured any-llm completion boundary, preserving the
caller prohibition on tool use, as captured below.
```text
=== BEFORE fix ===
client sent tool_choice: {'type': 'none'}
outbound tool_choice forwarded to any-llm provider: 'auto'
=== AFTER fix ===
client sent tool_choice: {'type': 'none'}
outbound tool_choice forwarded to any-llm provider: 'none'
```
Before the patch the forbidden-tools request reached the provider as
`auto` (tools allowed); after, it is correctly `none`.
- Not tested: a live round-trip against a real any-llm provider/model
(no provider credentials in this environment); the outbound request was
captured at the backend boundary instead.
## Runtime Rollout Safety
- Rollout-managed feature(s): none — pure request-conversion fix.
- Minimum rollout channel: n/a
- Stable/default behavior changed: yes, but only for
`tool_choice={"type": "none"}` on the any-llm backend, which was
previously handled incorrectly.
- Kill switch / disable path: n/a
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this commit.
## 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
- [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`
## Additional Notes
`mypy` not run locally (not set up in this environment); `ruff
check`/`ruff format` are clean. No new dependencies.
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3963"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
@@ -48,10 +48,10 @@ def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:
|
||||
def _convert_tool_choice(choice: Any) -> Any:
|
||||
"""Convert an Anthropic ``tool_choice`` to the OpenAI shape (mirrors LiteLLM).
|
||||
|
||||
Anthropic: ``{"type": "auto"}``, ``{"type": "any"}``, ``{"type": "tool",
|
||||
"name": ...}``. OpenAI: ``"auto"``, ``"required"``, ``{"type": "function",
|
||||
"function": {"name": ...}}``. Passing the raw Anthropic dict through makes
|
||||
the provider reject or ignore it.
|
||||
Anthropic: ``{"type": "auto"}``, ``{"type": "any"}``, ``{"type": "none"}``,
|
||||
``{"type": "tool", "name": ...}``. OpenAI: ``"auto"``, ``"required"``,
|
||||
``"none"``, ``{"type": "function", "function": {"name": ...}}``. Passing the
|
||||
raw Anthropic dict through makes the provider reject or ignore it.
|
||||
"""
|
||||
if isinstance(choice, str):
|
||||
return choice
|
||||
@@ -61,6 +61,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"
|
||||
|
||||
@@ -340,6 +340,52 @@ async def test_send_message_converts_anthropic_tools_and_tool_choice(
|
||||
assert sent["tool_choice"] == "required"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("anthropic_choice", "openai_choice"),
|
||||
[
|
||||
({"type": "auto"}, "auto"),
|
||||
({"type": "any"}, "required"),
|
||||
({"type": "none"}, "none"),
|
||||
({"type": "tool", "name": "t"}, {"type": "function", "function": {"name": "t"}}),
|
||||
],
|
||||
)
|
||||
def test_convert_tool_choice_covers_every_anthropic_type(
|
||||
anthropic_choice: dict[str, object], openai_choice: object
|
||||
) -> None:
|
||||
"""Every Anthropic tool_choice type maps to its OpenAI equivalent.
|
||||
|
||||
``{"type": "none"}`` ("do not use any tool this turn") previously fell
|
||||
through to the ``"auto"`` default, inverting the instruction into "you may
|
||||
use tools" so the model could call a tool the client explicitly forbade.
|
||||
"""
|
||||
assert anyllm._convert_tool_choice(anthropic_choice) == openai_choice
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_forwards_tool_choice_none(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""A request forbidding tools must reach any-llm as OpenAI ``"none"``.
|
||||
|
||||
Regression: ``{"type": "none"}`` was converted to ``"auto"``, letting the
|
||||
model call a tool the client disabled for the turn.
|
||||
"""
|
||||
backend, instance = make_backend(monkeypatch)
|
||||
instance.response = make_response(make_choice("ok", "stop"))
|
||||
|
||||
await backend.send_message(
|
||||
{
|
||||
"model": "claude",
|
||||
"messages": [{"role": "user", "content": "hi"}],
|
||||
"tools": [{"name": "t", "input_schema": {"type": "object"}}],
|
||||
"tool_choice": {"type": "none"},
|
||||
},
|
||||
{},
|
||||
)
|
||||
|
||||
assert instance.calls[0]["tool_choice"] == "none"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_message_converts_anthropic_tools_and_tool_choice(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
|
||||
Reference in New Issue
Block a user