Skip to content

Commit c5f9551

Browse files
committed
Add support for Anthropic prompt caching
1 parent 415ced0 commit c5f9551

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

shiny/ui/_chat_normalize.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,28 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
185185
# The actual MessageStreamEvent is a generic, so isinstance() can't
186186
# be used to check the type. Instead, we manually construct the relevant
187187
# union of relevant classes...
188-
return (
188+
if (
189189
isinstance(chunk, RawContentBlockDeltaEvent)
190190
or isinstance(chunk, RawContentBlockStartEvent)
191191
or isinstance(chunk, RawContentBlockStopEvent)
192192
or isinstance(chunk, RawMessageDeltaEvent)
193193
or isinstance(chunk, RawMessageStartEvent)
194194
or isinstance(chunk, RawMessageStopEvent)
195+
):
196+
return True
197+
198+
# Older versions of the anthropic library don't have the beta prompt caching
199+
# types, so we need to check for them separately. If this import fails or
200+
# errors, then we'll end up in the Exception handler and return False.
201+
from anthropic.types.beta.prompt_caching import (
202+
RawPromptCachingBetaMessageStartEvent,
195203
)
204+
205+
if isinstance(chunk, RawPromptCachingBetaMessageStartEvent):
206+
return True
207+
208+
return False
209+
196210
except Exception:
197211
return False
198212

0 commit comments

Comments
 (0)