Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(backends/anyllm): map Anthropic tool_choice none to OpenAI none (#3963)

Rajat Nagdad722a6414c38
diff --git a/headroom/backends/anyllm.py b/headroom/backends/anyllm.py
index a4d2ad7ba..1d7d2e475 100644
--- a/headroom/backends/anyllm.py
+++ b/headroom/backends/anyllm.py
@@ -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"
diff --git a/tests/test_backend_anyllm.py b/tests/test_backend_anyllm.py
index 15b6e8145..c5a153594 100644
--- a/tests/test_backend_anyllm.py
+++ b/tests/test_backend_anyllm.py
@@ -340,6 +340,52 @@ async def test_send_message_converts_anthropic_tools_and_tool_choice(
assert sent["tool_choice"] == "required"
+ ("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
+
+
+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,