From dafadf3705617eb36ba6cc5fc30f5d3e456da106 Mon Sep 17 00:00:00 2001 From: Lakshya Dhariwal Date: Sat, 17 Oct 2026 14:54:45 +0530 Subject: [PATCH] fix(caching): skip proxy-only Anthropic import in cache-hit defer check Base SDK installs lack the proxy-only extras (fastapi), so the lazy import of the Anthropic Messages replay class crashes sync cache hits. Guard it; a base install can never hold that iterator. --- litellm/caching/caching_handler.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/litellm/caching/caching_handler.py b/litellm/caching/caching_handler.py index 063faa70f2f..ab823176abe 100644 --- a/litellm/caching/caching_handler.py +++ b/litellm/caching/caching_handler.py @@ -182,14 +182,22 @@ def _should_defer_streaming_cache_hit_callbacks(*, cached_result: object) -> boo spend and callback records. A plain (non-stream) replay logs here, since nothing else will. """ - from litellm.llms.anthropic.pass_through.messages.response_cache import ( - CachedAnthropicMessagesStreamIterator, - ) from litellm.responses.streaming_iterator import BaseResponsesAPIStreamingIterator - return isinstance( - cached_result, (CustomStreamWrapper, BaseResponsesAPIStreamingIterator, CachedAnthropicMessagesStreamIterator) - ) + stream_replay_classes: tuple[type, ...] = (CustomStreamWrapper, BaseResponsesAPIStreamingIterator) + try: + from litellm.llms.anthropic.pass_through.messages.response_cache import ( + CachedAnthropicMessagesStreamIterator, + ) + except ModuleNotFoundError: + # The Anthropic Messages replay is only reachable through proxy-only + # extras (fastapi). A base SDK install can never hold one, so the + # isinstance check can safely go ahead without it. + pass + else: + stream_replay_classes = (*stream_replay_classes, CachedAnthropicMessagesStreamIterator) + + return isinstance(cached_result, stream_replay_classes) def _prompt_tokens_details_as_mapping(details: "PromptTokensDetailsWrapper") -> Mapping[str, object]: