refactor(backends): reuse the LiteLLM tool converters in the any-llm backend

_convert_anthropic_tool and _convert_tool_choice were defined in both backends with identical bodies (only the docstrings differed), so a fix to one copy could miss the other. anyllm.py now imports them from litellm.py. headroom/backends/__init__.py already imports the litellm backend, so this adds no import cost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-10-09 16:46:30 -07:00
co-authored by Claude Opus 5.5
parent a9815bbb5f
commit 25190e9117
2 changed files with 4 additions and 46 deletions
+1 -45
View File
@@ -16,6 +16,7 @@ from headroom.proxy.public_errors import client_message
from headroom.utils import format_exception_message
from .base import Backend, BackendResponse, StreamEvent
from .litellm import _convert_anthropic_tool, _convert_tool_choice
logger = logging.getLogger(__name__)
@@ -28,51 +29,6 @@ except ImportError:
AnyLLM = None # type: ignore
def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:
"""Convert an Anthropic tool definition to the OpenAI function shape.
any-llm speaks OpenAI, so an Anthropic ``{name, description, input_schema}``
tool must become ``{type: function, function: {name, description,
parameters}}`` before it is forwarded, or the provider ignores/rejects the
tools array and the model never calls a tool. Mirrors the LiteLLM backend's
converter so both OpenAI-compatible backends send the same shape.
"""
func: dict[str, Any] = {"name": tool.get("name", "")}
if "description" in tool:
func["description"] = tool["description"]
if "input_schema" in tool:
func["parameters"] = tool["input_schema"]
return {"type": "function", "function": func}
def _convert_tool_choice(choice: Any) -> Any:
"""Convert an Anthropic ``tool_choice`` to the OpenAI shape (mirrors LiteLLM).
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
if isinstance(choice, dict):
choice_type = choice.get("type", "auto")
if choice_type == "auto":
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"
class AnyLLMBackend(Backend):
"""Backend using any-llm for multi-provider support."""
+3 -1
View File
@@ -649,6 +649,8 @@ def _anthropic_usage_from_litellm(litellm_usage: Any) -> dict[str, Any]:
def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:
"""Convert Anthropic tool format to OpenAI function format.
Shared with the any-llm backend, so both OpenAI-compatible backends send the same shape.
Anthropic: {"name": "...", "description": "...", "input_schema": {...}}
OpenAI: {"type": "function", "function": {"name": "...", "description": "...", "parameters": {...}}}
"""
@@ -661,7 +663,7 @@ 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.
"""Convert Anthropic tool_choice to OpenAI format (shared with the any-llm backend).
Anthropic: {"type": "auto"}, {"type": "any"}, {"type": "none"}, {"type": "tool", "name": "..."}
OpenAI: "auto", "required", "none", {"type": "function", "function": {"name": "..."}}