test(anthropic): capture tool ids on the forwarded HTTP body

CI lint failed TQ008 on the two handler tests that patched
anthropic_messages_handler. They now inject AsyncHTTPHandler with
MockTransport and assert the id on the wire.
This commit is contained in:
Chaitanya Laxman 2026-09-07 20:37:18 +04:00
parent 2a1b0e400b
commit 7a10559ea8

View file

@ -220,11 +220,19 @@ async def test_anthropic_messages_sanitizes_tool_use_ids_before_dispatch():
assert msgs[0]["content"][0]["id"] == "functions.Bash:0"
@pytest.mark.asyncio
async def test_anthropic_messages_keeps_tool_use_ids_for_non_anthropic_api_base():
from litellm.llms.anthropic.experimental_pass_through.messages import handler
_ANTHROPIC_MESSAGES_OK = {
"id": "msg_test",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4-5-20250929",
"content": [{"type": "text", "text": "ok"}],
"stop_reason": "end_turn",
"usage": {"input_tokens": 1, "output_tokens": 1},
}
msgs = [
def _tool_use_replay_messages():
return [
{
"role": "assistant",
"content": [
@ -237,29 +245,38 @@ async def test_anthropic_messages_keeps_tool_use_ids_for_non_anthropic_api_base(
],
}
]
def _capturing_anthropic_client():
captured = {}
def fake_handler(*args, **kwargs):
captured["messages"] = kwargs.get("messages")
return "stub"
def capture_upstream(request: httpx.Request) -> httpx.Response:
captured["body"] = json.loads(request.content)
return httpx.Response(200, json=_ANTHROPIC_MESSAGES_OK, request=request)
fake_loop = MagicMock()
fake_loop.run_in_executor = lambda _e, func: _async_return(func())
upstream = AsyncHTTPHandler()
upstream.client = httpx.AsyncClient(transport=httpx.MockTransport(capture_upstream))
return captured, upstream
with (
patch.object(handler, "anthropic_messages_handler", side_effect=fake_handler),
patch("asyncio.get_event_loop", return_value=fake_loop),
):
await handler.anthropic_messages(
max_tokens=100,
messages=msgs,
model="anthropic/claude-sonnet-4-5-20250929",
custom_llm_provider="anthropic",
api_key="k",
api_base="http://127.0.0.1:8000/v1",
)
assert captured["messages"][0]["content"][0]["id"] == "functions.Bash:0"
@pytest.mark.asyncio
async def test_anthropic_messages_keeps_tool_use_ids_for_non_anthropic_api_base():
from litellm.llms.anthropic.experimental_pass_through.messages import handler
msgs = _tool_use_replay_messages()
captured, upstream = _capturing_anthropic_client()
await handler.anthropic_messages(
max_tokens=100,
messages=msgs,
model="anthropic/claude-sonnet-4-5-20250929",
custom_llm_provider="anthropic",
api_key="k",
api_base="http://127.0.0.1:8000/v1",
client=upstream,
)
assert captured["body"]["messages"][0]["content"][0]["id"] == "functions.Bash:0"
assert msgs[0]["content"][0]["id"] == "functions.Bash:0"
@ -267,41 +284,19 @@ async def test_anthropic_messages_keeps_tool_use_ids_for_non_anthropic_api_base(
async def test_anthropic_messages_sanitizes_azure_ai_model_prefix_without_provider():
from litellm.llms.anthropic.experimental_pass_through.messages import handler
msgs = [
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "functions.Bash:0",
"name": "Bash",
"input": {},
}
],
}
]
captured = {}
msgs = _tool_use_replay_messages()
captured, upstream = _capturing_anthropic_client()
def fake_handler(*args, **kwargs):
captured["messages"] = kwargs.get("messages")
return "stub"
await handler.anthropic_messages(
max_tokens=100,
messages=msgs,
model="azure_ai/claude-sonnet-4-5",
api_key="k",
api_base="https://myres.services.ai.azure.com/anthropic",
client=upstream,
)
fake_loop = MagicMock()
fake_loop.run_in_executor = lambda _e, func: _async_return(func())
with (
patch.object(handler, "anthropic_messages_handler", side_effect=fake_handler),
patch("asyncio.get_event_loop", return_value=fake_loop),
):
await handler.anthropic_messages(
max_tokens=100,
messages=msgs,
model="azure_ai/claude-sonnet-4-5",
api_key="k",
api_base="https://myres.services.ai.azure.com/anthropic",
)
assert captured["messages"][0]["content"][0]["id"] == "functions_Bash_0"
assert captured["body"]["messages"][0]["content"][0]["id"] == "functions_Bash_0"
assert msgs[0]["content"][0]["id"] == "functions.Bash:0"