diff --git a/litellm/types/litellm_params.py b/litellm/types/litellm_params.py index 2065093f295..a0c043de6a1 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 @@ -344,6 +345,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 c4727cbeff0..1a92d0b05ee 100644 --- a/tests/unit/caching/test_caching.py +++ b/tests/unit/caching/test_caching.py @@ -454,6 +454,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 e369b7e93ab..ab618332038 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 @@ -252,6 +253,7 @@ INTERNAL_STATE_NAMES: Final = ( "model_alias_map", "data_residency", "litellm_logging_obj", + "parent_otel_span", "preset_cache_key", "cache_key", "stream_response", @@ -527,6 +529,7 @@ TYPE_HINT_NAMESPACE: Final[Mapping[str, object]] = { "ModelResponse": ModelResponse, "ModelResponseStream": ModelResponseStream, "Logging": Logging, + "Span": Span, "SecretFields": SecretFields, "CompactionState": CompactionState, "RouterWeights": RouterWeights,