fix: resolve pyright errors in Anthropic _from_standard_message
The function takes an OpenAI ChatCompletionMessageParam (a union of TypedDicts) and returns an Anthropic MessageParam (a different TypedDict). It does the conversion via dict-level mutations that don't type-check against either side's TypedDict schema. Work with the deepcopied message as a plain dict and cast to MessageParam at the return sites — matching the boundary-cast convention noted in llm_context.py. Drops anthropic_adapter.py from 20 to 2 pyright errors.
This commit is contained in:
@@ -289,20 +289,26 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]):
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
message = copy.deepcopy(message)
|
# ChatCompletionMessageParam (input) and MessageParam (output) are
|
||||||
if message["role"] == "tool":
|
# different TypedDicts — work with the message as a plain dict for the
|
||||||
return {
|
# transformations below and cast back to MessageParam at return sites.
|
||||||
"role": "user",
|
msg = cast(dict[str, Any], copy.deepcopy(message))
|
||||||
"content": [
|
if msg["role"] == "tool":
|
||||||
{
|
return cast(
|
||||||
"type": "tool_result",
|
MessageParam,
|
||||||
"tool_use_id": message["tool_call_id"],
|
{
|
||||||
"content": message["content"],
|
"role": "user",
|
||||||
},
|
"content": [
|
||||||
],
|
{
|
||||||
}
|
"type": "tool_result",
|
||||||
if message.get("tool_calls"):
|
"tool_use_id": msg["tool_call_id"],
|
||||||
tc = message["tool_calls"]
|
"content": msg["content"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if msg.get("tool_calls"):
|
||||||
|
tc = msg["tool_calls"]
|
||||||
ret = {"role": "assistant", "content": []}
|
ret = {"role": "assistant", "content": []}
|
||||||
for tool_call in tc:
|
for tool_call in tc:
|
||||||
function = tool_call["function"]
|
function = tool_call["function"]
|
||||||
@@ -314,8 +320,8 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]):
|
|||||||
"input": arguments,
|
"input": arguments,
|
||||||
}
|
}
|
||||||
ret["content"].append(new_tool_use)
|
ret["content"].append(new_tool_use)
|
||||||
return ret
|
return cast(MessageParam, ret)
|
||||||
content = message.get("content")
|
content = msg.get("content")
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
# fix empty text
|
# fix empty text
|
||||||
if content == "":
|
if content == "":
|
||||||
@@ -363,7 +369,7 @@ class AnthropicLLMAdapter(BaseLLMAdapter[AnthropicLLMInvocationParams]):
|
|||||||
image_item = content.pop(img_idx)
|
image_item = content.pop(img_idx)
|
||||||
content.insert(first_txt_idx, image_item)
|
content.insert(first_txt_idx, image_item)
|
||||||
|
|
||||||
return message
|
return cast(MessageParam, msg)
|
||||||
|
|
||||||
def _with_cache_control_markers(self, messages: list[MessageParam]) -> list[MessageParam]:
|
def _with_cache_control_markers(self, messages: list[MessageParam]) -> list[MessageParam]:
|
||||||
"""Add cache control markers to messages for prompt caching.
|
"""Add cache control markers to messages for prompt caching.
|
||||||
|
|||||||
Reference in New Issue
Block a user