From 70aeb5c7c2b05d6982ab2cff22b0744796085327 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Mon, 27 Apr 2026 14:14:57 -0400 Subject: [PATCH] fix: resolve pyright errors in Anthropic get_messages_for_logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Content items in MessageParam have a heterogeneous union type (Pydantic ContentBlock variants and TypedDict *BlockParam variants), neither of which supports the dict-style access and mutation this sanitizer does. Treat the deepcopied message as a plain dict and guard each content item with isinstance(item, dict) — matches the runtime shape produced by _from_standard_message and avoids crashing if a non-dict ever flows through the LLMSpecificMessage path. Drops anthropic_adapter.py from 115 to 23 pyright errors. --- .../adapters/services/anthropic_adapter.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/pipecat/adapters/services/anthropic_adapter.py b/src/pipecat/adapters/services/anthropic_adapter.py index 0fc1636f7..6832192dc 100644 --- a/src/pipecat/adapters/services/anthropic_adapter.py +++ b/src/pipecat/adapters/services/anthropic_adapter.py @@ -121,16 +121,20 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]): messages = self._from_universal_context_messages(self.get_messages(context)).messages # Sanitize messages for logging - messages_for_logging = [] + messages_for_logging: list[dict[str, Any]] = [] for message in messages: - msg = copy.deepcopy(message) - if "content" in msg: - if isinstance(msg["content"], list): - for item in msg["content"]: - if item["type"] == "image": - item["source"]["data"] = "..." - if item["type"] == "thinking" and item.get("signature"): - item["signature"] = "..." + msg: dict[str, Any] = copy.deepcopy(dict(message)) + content = msg.get("content") + if isinstance(content, list): + for item in content: + if not isinstance(item, dict): + continue + if item.get("type") == "image": + source = item.get("source") + if isinstance(source, dict): + source["data"] = "..." + if item.get("type") == "thinking" and item.get("signature"): + item["signature"] = "..." messages_for_logging.append(msg) return messages_for_logging