From 9ee123bf3364284b9e751feabedad74dd8a09c52 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Mon, 27 Apr 2026 15:49:39 -0400 Subject: [PATCH] fix: resolve final pyright error in Anthropic cache control marker Same MessageParam content-typing issue as the consecutive-message merge fix: pyright doesn't carry the str-to-list narrowing forward, and Iterable has no `[-1]` access. Cast to `list[Any]` and document the chain of assumptions (list, non-empty, dict-typed last item) and where each is upheld upstream. This brings anthropic_adapter.py to 0 pyright errors (down from 115). --- src/pipecat/adapters/services/anthropic_adapter.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pipecat/adapters/services/anthropic_adapter.py b/src/pipecat/adapters/services/anthropic_adapter.py index 1632662e5..4446ed9ca 100644 --- a/src/pipecat/adapters/services/anthropic_adapter.py +++ b/src/pipecat/adapters/services/anthropic_adapter.py @@ -384,7 +384,16 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]): def add_cache_control_marker(message: MessageParam): if isinstance(message["content"], str): message["content"] = [{"type": "text", "text": message["content"]}] - message["content"][-1]["cache_control"] = {"type": "ephemeral"} + # Assumptions on the next line: + # - content is a list (str case handled above; this codebase only + # ever constructs content as a str or a list) + # - the list is non-empty (guaranteed by the empty-content + # replacement in `_from_universal_context_messages`) + # - the last item is a dict. The standard-message path enforces + # this via TypedDicts (which are dicts at runtime); the + # LLMSpecificMessage passthrough doesn't, but in practice + # callers use dicts. + cast(list[Any], message["content"])[-1]["cache_control"] = {"type": "ephemeral"} try: # Add cache control markers to the most recent two user messages.