Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(caching): carry has_buffered_provider_output through the anthropic stream cache writer

mateo-berri8d877980e0e7
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py b/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py
index 9ac5187681b..86dfe8ff451 100644
--- a/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py
+++ b/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py
@@ -46,6 +46,10 @@ class AnthropicMessagesStreamCacheWriter:
stream._hidden_params if isinstance(stream, AnthropicMessagesStreamingResponse) else _EMPTY_MAPPING
)
+ @property
+ def has_buffered_provider_output(self) -> bool:
+ return getattr(self.stream, "has_buffered_provider_output", False) is True
+
def __aiter__(self) -> "AnthropicMessagesStreamCacheWriter":
return self
diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_response_cache.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_response_cache.py
index fe0bcfa4f30..22d14614108 100644
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_response_cache.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_response_cache.py
@@ -4,9 +4,15 @@ from typing import Any, AsyncIterator, Dict, List
import pytest
+import datetime
+
import litellm
from litellm.caching.caching import Cache, LiteLLMCacheType
+from litellm.caching.caching_handler import LLMCachingHandler
from litellm.llms.anthropic.experimental_pass_through.messages import handler
+from litellm.llms.anthropic.experimental_pass_through.messages.response_cache import (
+ AnthropicMessagesStreamCacheWriter,
+)
STREAM_EVENTS: List[bytes] = [
b'event: message_start\ndata: {"type": "message_start", "message": {"id": "msg_stream_1", "type": "message", '
@@ -262,3 +268,25 @@ async def test_cached_stream_replay_logs_once_when_polled_after_exhaustion():
await asyncio.sleep(0)
mock_route.assert_called_once()
+class _HeldBackStream:
+ has_buffered_provider_output = True
+
+ def __aiter__(self) -> "_HeldBackStream":
+ return self
+
+ async def __anext__(self) -> bytes:
+ raise StopAsyncIteration
+
+
+def test_cache_writer_forwards_has_buffered_provider_output(request_kwargs):
+ caching_handler = LLMCachingHandler(
+ original_function=handler.anthropic_messages,
+ request_kwargs=dict(request_kwargs),
+ start_time=datetime.datetime.now(),
+ )
+ held_back = AnthropicMessagesStreamCacheWriter(stream=_HeldBackStream(), caching_handler=caching_handler)
+ assert held_back.has_buffered_provider_output is True
+ replayable = AnthropicMessagesStreamCacheWriter(
+ stream=_byte_stream(STREAM_EVENTS), caching_handler=caching_handler
+ )
+ assert replayable.has_buffered_provider_output is False