From bad8ba5ad166544ec94dd5bb2fe55588c80e9449 Mon Sep 17 00:00:00 2001 From: Aikins Laryea Date: Fri, 20 Nov 2026 20:49:49 +0000 Subject: [PATCH] test(sdk-litellm): follow the router core of litellm 1.110 LiteLLM 1.110 runs the Router in a Rust core. The core writes its own model_group_size, attempted_retries, and max_retries into the metadata of each chat call, over a proxy hook's values, and it takes stream only as a keyword. Two tests assumed the older Router and failed at the highest dependency bound. The proxy metadata test now expects the core's values when the core is present, and keeps the hook assertions for older releases. The Router stream test passes stream positionally only where the Router takes it. --- sdks/python-litellm/tests/test_litellm.py | 44 ++++++++++++++++++----- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/sdks/python-litellm/tests/test_litellm.py b/sdks/python-litellm/tests/test_litellm.py index c575801..d25d8ab 100644 --- a/sdks/python-litellm/tests/test_litellm.py +++ b/sdks/python-litellm/tests/test_litellm.py @@ -2,6 +2,7 @@ import asyncio import gc +import importlib import inspect import json import math @@ -34,6 +35,23 @@ from . import conftest from .conftest import STREAM_LIMIT + +# From 1.110, LiteLLM's Router walks retries and fallbacks in a Rust core that calls LiteLLM +# through router_attempt. The core writes its own retry keys into the metadata of each call, and +# takes positionally only the parameters that POSITIONAL lists. +def _router_attempt() -> Any: + try: + return importlib.import_module("litellm.router_attempt") + except ImportError: + return None + + +ROUTER_ATTEMPT = _router_attempt() +ROUTER_CORE = ROUTER_ATTEMPT is not None +ROUTER_TAKES_POSITIONAL_STREAM = ( + ROUTER_ATTEMPT is None or "stream" in ROUTER_ATTEMPT.POSITIONAL["acompletion"] +) + llm = cast(Any, litellm) # A JSON string of this many characters alone passes the bound of a non-stream capture. OVER_RECORD = "x" * telemetry_dev.MAX_RECORD_BYTES @@ -247,9 +265,7 @@ def test_completion_metadata_param_maps_to_td_metadata( ("/v1/responses", {"input": "Say hi", "metadata": {"tenant": "acme"}}, True, {}), ("/v1/responses", {"input": "Say hi"}, True, {}), # Without a litellm_metadata argument the pre-auth tag merge writes the header's tags - # into the body's metadata before the proxy copies it. On chat the Router writes its - # retry keys into litellm_metadata, so a hook's values under these names in metadata - # reach the span unchanged. + # into the body's metadata before the proxy copies it. ( "/v1/chat/completions", {"messages": CHAT_MESSAGES, "metadata": {"tenant": "acme"}}, @@ -364,10 +380,17 @@ async def test_litellm_proxy_request_exports_only_requester_and_router_metadata( assert client_keys | {"model_group", "deployment"} <= exported <= client_keys | router_keys assert a["td.metadata.model_group"] == "gpt-4o-mini" assert a["td.metadata.deployment"] == "openai/gpt-4o-mini" - if hook_routing_values: + if hook_routing_values and not ROUTER_CORE: + # On chat this Router writes its retry keys into litellm_metadata and leaves the hook's + # values in metadata. assert a["td.metadata.max_retries"] == 3 assert "td.metadata.model_group_size" not in a assert "td.metadata.attempted_retries" not in a + elif hook_routing_values: + # The Router core writes its own values over the hook's. + assert a["td.metadata.model_group_size"] == 1 + assert a["td.metadata.attempted_retries"] == 0 + assert a["td.metadata.max_retries"] == 2 leaked = [ secret for secret in ( @@ -4061,7 +4084,7 @@ def test_router_completion_traced(memory: SimpleNamespace) -> None: assert parent.span_id == root_context.span_id -async def test_wrap_router_acompletion_positional_stream_is_instrumented( +async def test_wrap_router_acompletion_stream_is_instrumented( memory: SimpleNamespace, ) -> None: router = wrap_router( @@ -4075,9 +4098,14 @@ async def test_wrap_router_acompletion_positional_stream_is_instrumented( ) ) - stream = await router.acompletion( - "gpt-4o-mini", CHAT_MESSAGES, True, mock_response="Router async stream" - ) + if ROUTER_TAKES_POSITIONAL_STREAM: + stream = await router.acompletion( + "gpt-4o-mini", CHAT_MESSAGES, True, mock_response="Router async stream" + ) + else: + stream = await router.acompletion( + "gpt-4o-mini", CHAT_MESSAGES, stream=True, mock_response="Router async stream" + ) chunks = [chunk async for chunk in stream] assert (