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.
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
import copy
|
import copy
|
||||||
import json
|
import json
|
||||||
from dataclasses import dataclass
|
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 import NOT_GIVEN, NotGiven
|
||||||
from anthropic.types.message_param import MessageParam
|
from anthropic.types.message_param import MessageParam
|
||||||
@@ -189,8 +189,13 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]):
|
|||||||
]
|
]
|
||||||
if isinstance(next_message["content"], str):
|
if isinstance(next_message["content"], str):
|
||||||
next_message["content"] = [{"type": "text", "text": next_message["content"]}]
|
next_message["content"] = [{"type": "text", "text": next_message["content"]}]
|
||||||
# Concatenate the content
|
# Concatenate the content. MessageParam types content as
|
||||||
current_message["content"].extend(next_message["content"])
|
# `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
|
# Remove the next message from the list
|
||||||
messages.pop(i + 1)
|
messages.pop(i + 1)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user