mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-09 03:18:44 +00:00
fix(xai): read tool usage details from ResponseAPIUsage extras
Also inspect model_extra when attaching server_side_tool_usage_details for Responses cost tracking.
This commit is contained in:
parent
3aea951e6c
commit
ea98d8e116
1 changed files with 19 additions and 3 deletions
|
|
@ -76,6 +76,22 @@ class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig):
|
|||
self._attach_server_side_tool_usage_details_to_usage(response)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _server_side_tool_usage_details_from_usage(usage: Any) -> Any:
|
||||
if usage is None:
|
||||
return None
|
||||
if isinstance(usage, dict):
|
||||
return usage.get("server_side_tool_usage_details")
|
||||
details = getattr(usage, "server_side_tool_usage_details", None)
|
||||
if details is not None:
|
||||
return details
|
||||
model_extra = getattr(usage, "model_extra", None) or getattr(
|
||||
usage, "__pydantic_extra__", None
|
||||
)
|
||||
if isinstance(model_extra, dict):
|
||||
return model_extra.get("server_side_tool_usage_details")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _attach_server_side_tool_usage_details_to_usage(
|
||||
response: ResponsesAPIResponse,
|
||||
|
|
@ -83,9 +99,9 @@ class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig):
|
|||
if response.usage is None:
|
||||
return
|
||||
|
||||
details = getattr(response.usage, "server_side_tool_usage_details", None)
|
||||
if details is None and isinstance(response.usage, dict):
|
||||
details = response.usage.get("server_side_tool_usage_details")
|
||||
details = XAIResponsesAPIConfig._server_side_tool_usage_details_from_usage(
|
||||
response.usage
|
||||
)
|
||||
if details is None:
|
||||
return
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue