mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-08 03:08:45 +00:00
fix(xai): type tool usage details helpers without Any
This commit is contained in:
parent
4a536098e1
commit
21f742041a
2 changed files with 18 additions and 12 deletions
|
|
@ -5,7 +5,7 @@ Helper util for handling XAI-specific cost calculation
|
|||
"""
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
from typing import TYPE_CHECKING, Final
|
||||
|
||||
from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
|
||||
from litellm.types.utils import PromptTokensDetailsWrapper, Usage
|
||||
|
|
@ -17,7 +17,7 @@ if TYPE_CHECKING:
|
|||
_DEFAULT_WEB_SEARCH_COST_PER_CALL = 5.0 / 1000.0
|
||||
|
||||
|
||||
def apply_server_side_tool_usage_details_to_usage(usage: Usage, details: Mapping[str, Any] | None) -> None:
|
||||
def apply_server_side_tool_usage_details_to_usage(usage: Usage, details: Mapping[str, object] | None) -> None:
|
||||
"""
|
||||
Attach server_side_tool_usage_details and mirror web_search_calls onto
|
||||
prompt_tokens_details.web_search_requests for built-in tool cost gating.
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from collections.abc import Mapping
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
|
||||
import httpx
|
||||
|
|
@ -14,6 +15,7 @@ from litellm.llms.xai.cost_calculator import (
|
|||
from litellm.responses.utils import ResponseAPILoggingUtils
|
||||
from litellm.secret_managers.main import get_secret_str
|
||||
from litellm.types.llms.openai import (
|
||||
ResponseAPIUsage,
|
||||
ResponseCompletedEvent,
|
||||
ResponseFailedEvent,
|
||||
ResponseIncompleteEvent,
|
||||
|
|
@ -107,18 +109,22 @@ class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig):
|
|||
return event
|
||||
|
||||
@staticmethod
|
||||
def _server_side_tool_usage_details_from_usage(usage: Any) -> Any:
|
||||
def _server_side_tool_usage_details_from_usage(
|
||||
usage: Usage | ResponseAPIUsage | Mapping[str, object] | None,
|
||||
) -> Mapping[str, object] | None:
|
||||
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
|
||||
if isinstance(usage, Mapping):
|
||||
details = usage.get("server_side_tool_usage_details")
|
||||
else:
|
||||
details = getattr(usage, "server_side_tool_usage_details", None)
|
||||
if details is None:
|
||||
model_extra = getattr(usage, "model_extra", None) or getattr(usage, "__pydantic_extra__", None)
|
||||
if isinstance(model_extra, Mapping):
|
||||
details = model_extra.get("server_side_tool_usage_details")
|
||||
if not isinstance(details, Mapping):
|
||||
return None
|
||||
return details
|
||||
|
||||
@staticmethod
|
||||
def _attach_server_side_tool_usage_details_to_usage(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue