From c517b67baddf9fc41f3196289c6c6d6c6ada0e82 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Mon, 27 Apr 2026 14:39:24 -0400 Subject: [PATCH] fix: resolve pyright error when merging consecutive Anthropic messages MessageParam types content as `str | Iterable[...]`, and Iterable has no `.extend()`. After the str-to-list conversions, pyright re-reads the TypedDict field as the original wide type rather than carrying the narrowing forward. Cast to `list[Any]` to express the codebase's existing str-or-list assumption. Drops anthropic_adapter.py from 23 to 21 pyright errors. --- src/pipecat/adapters/services/anthropic_adapter.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/pipecat/adapters/services/anthropic_adapter.py b/src/pipecat/adapters/services/anthropic_adapter.py index 6832192dc..d0fdfca3b 100644 --- a/src/pipecat/adapters/services/anthropic_adapter.py +++ b/src/pipecat/adapters/services/anthropic_adapter.py @@ -9,7 +9,7 @@ import copy import json from dataclasses import dataclass -from typing import Any, TypedDict, TypeGuard, TypeVar +from typing import Any, TypedDict, TypeGuard, TypeVar, cast from anthropic import NOT_GIVEN, NotGiven from anthropic.types.message_param import MessageParam @@ -189,8 +189,13 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]): ] if isinstance(next_message["content"], str): next_message["content"] = [{"type": "text", "text": next_message["content"]}] - # Concatenate the content - current_message["content"].extend(next_message["content"]) + # Concatenate the content. MessageParam types content as + # `str | Iterable[...]`, but this codebase assumes it's + # either a str or a list. The str case is handled above, so + # we assume that both are lists here. + cast(list[Any], current_message["content"]).extend( + cast(list[Any], next_message["content"]) + ) # Remove the next message from the list messages.pop(i + 1) else: