From 07e048bd70f0b3ecd930ba640930bfe749341ac6 Mon Sep 17 00:00:00 2001 From: Alexandre Baudoin Date: Tue, 6 Oct 2026 09:40:25 +0200 Subject: [PATCH] fix(caching): skip caching FakeAnthropicMessagesStreamIterator (not JSON serializable) --- litellm/caching/caching_handler.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/litellm/caching/caching_handler.py b/litellm/caching/caching_handler.py index f04ff8b6e78..076eec79c14 100644 --- a/litellm/caching/caching_handler.py +++ b/litellm/caching/caching_handler.py @@ -1101,6 +1101,18 @@ class LLMCachingHandler: ) ) else: + from litellm.llms.anthropic.pass_through.messages.fake_stream_iterator import ( + FakeAnthropicMessagesStreamIterator, + ) + + if isinstance(result, FakeAnthropicMessagesStreamIterator): + # Synthetic streaming wrapper (e.g. websearch interception + # short-circuit) around an already-served response. It is + # not JSON-serializable and was never meant to be cached + # as-is - caching it crashes the Redis/async cache backend + # with "Object of type FakeAnthropicMessagesStreamIterator + # is not JSON serializable" on every streaming request. + return create_cache_write_task(lambda: cache.async_add_cache(result, **new_kwargs)) def sync_set_cache(