From c63d2c4397b8662da50160f33ffacdeddc464055 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:11:17 -0800 Subject: [PATCH] test(azure_ai): record the Azure inference request with respx instead of appending to a list --- .../count_tokens/test_token_counter.py | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/tests/unit/llms/azure_ai/anthropic/count_tokens/test_token_counter.py b/tests/unit/llms/azure_ai/anthropic/count_tokens/test_token_counter.py index 1120cfda85a..faa33e559ca 100644 --- a/tests/unit/llms/azure_ai/anthropic/count_tokens/test_token_counter.py +++ b/tests/unit/llms/azure_ai/anthropic/count_tokens/test_token_counter.py @@ -12,7 +12,6 @@ from pydantic import ValidationError import litellm from litellm import anthropic_beta_headers_manager from litellm.llms.azure_ai.anthropic.count_tokens.token_counter import AzureAIAnthropicTokenCounter -from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler from litellm.types.utils import CountContextManagement, TokenCountResponse API_BASE: Final = "https://my-resource.services.ai.azure.com" @@ -275,26 +274,22 @@ async def test_count_sends_the_tool_change_beta_when_the_history_adds_a_tool(mon async def _betas_azure_inference_sends(context_management: Mapping[str, object]) -> frozenset[str]: - received: Final[list[httpx.Request]] = [] - - def foundry_answers(request: httpx.Request) -> httpx.Response: - received.append(request) - return httpx.Response( - 200, - json={ - "id": "msg_1", - "type": "message", - "role": "assistant", - "model": "claude-sonnet-5-5", - "content": [{"type": "text", "text": "ok"}], - "stop_reason": "end_turn", - "stop_sequence": None, - "usage": {"input_tokens": 12, "output_tokens": 2}, - }, + with respx.mock: + route: Final = respx.post(f"{API_BASE}/anthropic/v1/messages").mock( + return_value=httpx.Response( + 200, + json={ + "id": "msg_1", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-5-5", + "content": [{"type": "text", "text": "ok"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 12, "output_tokens": 2}, + }, + ) ) - - upstream: Final = AsyncHTTPHandler(transport=httpx.MockTransport(foundry_answers)) - try: await litellm.anthropic.messages.acreate( max_tokens=64, messages=[{"role": "user", "content": "Summarize the thread."}], @@ -302,11 +297,8 @@ async def _betas_azure_inference_sends(context_management: Mapping[str, object]) api_key="azure-key", api_base=API_BASE, context_management=context_management, - client=upstream, ) - finally: - await upstream.close() - return frozenset(filter(None, received[0].headers.get("anthropic-beta", "").split(","))) + return frozenset(filter(None, route.calls.last.request.headers.get("anthropic-beta", "").split(","))) @pytest.mark.asyncio