From 02fd30121a995552dd857ee1b81bf602b112a94a Mon Sep 17 00:00:00 2001 From: Prasanna Jain Date: Wed, 28 Oct 2026 01:25:01 -0400 Subject: [PATCH] fix(caching): keep parent_otel_span out of the response cache key --- litellm/types/litellm_params.py | 2 ++ tests/unit/caching/test_caching.py | 10 ++++++++++ tests/unit/types/test_litellm_params.py | 3 +++ 3 files changed, 15 insertions(+) diff --git a/litellm/types/litellm_params.py b/litellm/types/litellm_params.py index 717b74e5741..88130646318 100644 --- a/litellm/types/litellm_params.py +++ b/litellm/types/litellm_params.py @@ -14,6 +14,7 @@ if TYPE_CHECKING: import httpx from aiohttp import ClientSession from openai import AsyncAzureOpenAI, AsyncOpenAI, AzureOpenAI, OpenAI + from opentelemetry.trace import Span from litellm.litellm_core_utils.litellm_logging import Logging from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler @@ -341,6 +342,7 @@ class CallState: model_alias_map: Mapping[str, str] | None = None data_residency: str | None = None litellm_logging_obj: "Logging | None" = None + parent_otel_span: "Span | None" = None preset_cache_key: str | None = None cache_key: str | None = None stream_response: "Mapping[str, ModelResponse] | None" = None diff --git a/tests/unit/caching/test_caching.py b/tests/unit/caching/test_caching.py index 91083921130..418e75deb08 100644 --- a/tests/unit/caching/test_caching.py +++ b/tests/unit/caching/test_caching.py @@ -445,6 +445,16 @@ def test_provider_specific_cache_key_ignores_litellm_owned_kwargs(monkeypatch: p assert cache.get_cache_key(**{**request, "top_k": 6}) != base_key +def test_provider_specific_cache_key_ignores_parent_otel_span(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(litellm, "enable_caching_on_provider_specific_optional_params", True) + cache: Final = Cache(type=LiteLLMCacheType.LOCAL) + request: Final = {"model": "gpt-4.1-mini", "messages": [{"role": "user", "content": "hi"}]} + + assert cache.get_cache_key(**request, parent_otel_span=object()) == cache.get_cache_key( + **request, parent_otel_span=object() + ) + + def test_cache_key_tells_apart_requests_that_differ_only_in_continuation_token() -> None: cache: Final = Cache(type=LiteLLMCacheType.LOCAL) request: Final = { diff --git a/tests/unit/types/test_litellm_params.py b/tests/unit/types/test_litellm_params.py index 740342f54d9..c0122ae9057 100644 --- a/tests/unit/types/test_litellm_params.py +++ b/tests/unit/types/test_litellm_params.py @@ -9,6 +9,7 @@ import httpx import pytest from aiohttp import ClientSession from openai import AsyncAzureOpenAI, AsyncOpenAI, AzureOpenAI, OpenAI +from opentelemetry.trace import Span from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError import litellm @@ -249,6 +250,7 @@ INTERNAL_STATE_NAMES: Final = ( "model_alias_map", "data_residency", "litellm_logging_obj", + "parent_otel_span", "preset_cache_key", "cache_key", "stream_response", @@ -524,6 +526,7 @@ TYPE_HINT_NAMESPACE: Final[Mapping[str, object]] = { "ModelResponse": ModelResponse, "ModelResponseStream": ModelResponseStream, "Logging": Logging, + "Span": Span, "SecretFields": SecretFields, "CompactionState": CompactionState, "RouterWeights": RouterWeights,