mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
fix: handle unsupported server_tool_use types with warnings
Address code review feedback: instead of silently dropping data when server_tool_use is an unsupported type, now: - Issue a warning when receiving unexpected types - Attempt graceful conversion for Pydantic models (via model_dump) - Attempt graceful conversion for Mapping types (via dict()) - Warn again if conversion fails This prevents silent data loss while maintaining backward compatibility. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
270220dac9
commit
b76fdd40c2
1 changed files with 29 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
import time
|
||||
import warnings
|
||||
from enum import Enum
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Mapping, Optional, Union
|
||||
|
||||
|
|
@ -1528,6 +1529,34 @@ class Usage(SafeAttributeModel, CompletionUsage):
|
|||
_server_tool_use = ServerToolUse(**server_tool_use)
|
||||
elif isinstance(server_tool_use, ServerToolUse):
|
||||
_server_tool_use = server_tool_use
|
||||
else:
|
||||
# Warn about unsupported type and try to handle it gracefully
|
||||
warnings.warn(
|
||||
f"server_tool_use received unsupported type {type(server_tool_use).__name__}. "
|
||||
f"Expected dict or ServerToolUse. Attempting to convert to dict.",
|
||||
UserWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
# Try to convert to dict if it has model_dump (Pydantic models)
|
||||
if hasattr(server_tool_use, "model_dump"):
|
||||
try:
|
||||
_server_tool_use = ServerToolUse(**server_tool_use.model_dump())
|
||||
except Exception as e:
|
||||
warnings.warn(
|
||||
f"Failed to convert server_tool_use to ServerToolUse: {e}",
|
||||
UserWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
# Try dict() conversion for mapping types
|
||||
elif isinstance(server_tool_use, Mapping):
|
||||
try:
|
||||
_server_tool_use = ServerToolUse(**dict(server_tool_use))
|
||||
except Exception as e:
|
||||
warnings.warn(
|
||||
f"Failed to convert server_tool_use to ServerToolUse: {e}",
|
||||
UserWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
prompt_tokens=prompt_tokens or 0,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue