Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(xai): replace setattr with assignments for B010

Yang Yangfc102b5f1a56
diff --git a/litellm/llms/xai/cost_calculator.py b/litellm/llms/xai/cost_calculator.py
index 6479757aded..dd77b8d5d09 100644
--- a/litellm/llms/xai/cost_calculator.py
+++ b/litellm/llms/xai/cost_calculator.py
@@ -24,7 +24,7 @@ def apply_server_side_tool_usage_details_to_usage(usage: Usage, details: Mapping
"""
if details is None:
return
- setattr(usage, "server_side_tool_usage_details", details)
+ usage.server_side_tool_usage_details = details # pyright: ignore[reportAttributeAccessIssue] # extra # rebind-ok: extras
try:
web_search_calls: Final = int(details.get("web_search_calls") or 0)
except (TypeError, ValueError):
@@ -32,8 +32,8 @@ def apply_server_side_tool_usage_details_to_usage(usage: Usage, details: Mapping
if web_search_calls <= 0:
return
prompt_tokens_details: Final = usage.prompt_tokens_details or PromptTokensDetailsWrapper()
- setattr(prompt_tokens_details, "web_search_requests", web_search_calls)
- setattr(usage, "prompt_tokens_details", prompt_tokens_details)
+ prompt_tokens_details.web_search_requests = web_search_calls
+ usage.prompt_tokens_details = prompt_tokens_details # rebind-ok: write details onto caller usage
def cost_per_token(model: str, usage: Usage) -> tuple[float, float]:
diff --git a/litellm/llms/xai/responses/transformation.py b/litellm/llms/xai/responses/transformation.py
index 4c11f456b31..c1ebe1705d7 100644
--- a/litellm/llms/xai/responses/transformation.py
+++ b/litellm/llms/xai/responses/transformation.py
@@ -145,7 +145,7 @@ class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig):
chat_usage: Final = ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage(response.usage)
apply_server_side_tool_usage_details_to_usage(chat_usage, details)
- setattr(response, "usage", chat_usage)
+ response.usage = chat_usage # pyright: ignore[reportAttributeAccessIssue] # extra # rebind-ok: chat Usage
def _transform_web_search_tool(self, tool: dict[str, Any]) -> XAIWebSearchTool | dict[str, Any]:
"""