fix: coerce server_tool_use dict to ServerToolUse in stream_chunk_builder

When streaming Anthropic responses with web_search_options, stream_chunk_builder
reconstructs usage.server_tool_use as a dict rather than a ServerToolUse pydantic
object. This causes AttributeError when completion_cost() accesses
.web_search_requests on the dict.

Apply the same dict-to-object coercion pattern already used for prompt_tokens_details.

Fixes #26153
This commit is contained in:
Tai An 2026-04-21 06:13:45 -07:00
parent 26fcbc93e5
commit c7522a29d1

View file

@ -587,7 +587,10 @@ class ChunkProcessor:
hasattr(usage_chunk, "server_tool_use")
and usage_chunk.server_tool_use is not None
):
server_tool_use = usage_chunk.server_tool_use
_stu = usage_chunk.server_tool_use
server_tool_use = (
ServerToolUse(**_stu) if isinstance(_stu, dict) else _stu
)
if (
usage_chunk_dict["prompt_tokens_details"] is not None
and getattr(