From 24e30b551f155309172d11e91c65113aa50b2215 Mon Sep 17 00:00:00 2001 From: Yuneng Jiang Date: Mon, 15 Jun 2026 19:03:23 -0700 Subject: [PATCH] fix(types): coerce dict server_tool_use to ServerToolUse in Usage init Prerequisite for #27346 (completion_cost AttributeError on streaming Anthropic web_search). #27346's per-chunk coercion is undone by the existing Usage(**returned_usage.model_dump()) reconstruction in calculate_usage, which round-trips server_tool_use back to a plain dict; without this Usage.__init__ coercion the cost path still does attribute access on a dict and raises. The same prerequisite was bundled into the stable/1.88.x backport of #27346 (24b9655cd4). Content-verified present on litellm_internal_staging via aggregator 32c88ca74f (Litellm oss staging 080626, #29932); this restores only the two-line coercion, not the rest of that aggregator. (cherry picked from commit 32c88ca74f29143aadc4cfaa708f89ce1086386c) --- litellm/types/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 291d3548c8a..0200d1a0831 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -1672,6 +1672,9 @@ class Usage(SafeAttributeModel, CompletionUsage): prompt_tokens_details=_prompt_tokens_details or None, ) + if isinstance(server_tool_use, dict): + server_tool_use = ServerToolUse(**server_tool_use) + if server_tool_use is not None: self.server_tool_use = server_tool_use else: # maintain openai compatibility in usage object if possible