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]

feat: implement SafeAttributeModel for safe attribute access in models (#18321)

Loïc Rakotoson1645be3a2f86
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index e1f1c1c4b5b..e71fcac3897 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -63,6 +63,19 @@ def _generate_id(): # private helper function
return "chatcmpl-" + str(uuid.uuid4())
+
+class SafeAttributeModel:
+ """
+ A base model that provides safe attribute access.
+ """
+ def __delattr__(self, name):
+ try:
+ super().__delattr__(name)
+ except AttributeError:
+ # noop if attribute does not exist
+ pass
+
+
class LiteLLMCommonStrings(Enum):
redacted_by_litellm = "redacted by litellm. 'litellm.turn_off_message_logging=True'"
llm_provider_not_provided = "Unmapped LLM provider for this endpoint. You passed model={model}, custom_llm_provider={custom_llm_provider}. Check supported provider and route: https://docs.litellm.ai/docs/providers"
@@ -1021,7 +1034,7 @@ def add_provider_specific_fields(
setattr(object, "provider_specific_fields", provider_specific_fields)
-class Message(OpenAIObject):
+class Message(SafeAttributeModel, OpenAIObject):
content: Optional[str]
role: Literal["assistant", "user", "system", "tool", "function"]
tool_calls: Optional[List[ChatCompletionMessageToolCall]]
@@ -1141,7 +1154,7 @@ class Message(OpenAIObject):
return self.dict()
-class Delta(OpenAIObject):
+class Delta(SafeAttributeModel, OpenAIObject):
reasoning_content: Optional[str] = None
thinking_blocks: Optional[
List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]]
@@ -1238,7 +1251,7 @@ class Delta(OpenAIObject):
setattr(self, key, value)
-class Choices(OpenAIObject):
+class Choices(SafeAttributeModel, OpenAIObject):
finish_reason: str
index: int
message: Message
@@ -1331,6 +1344,7 @@ class CacheCreationTokenDetails(BaseModel):
class PromptTokensDetailsWrapper(
+ SafeAttributeModel,
PromptTokensDetails
): # extends with image generation fields (text_tokens, image_tokens)
text_tokens: Optional[int] = None
@@ -1378,7 +1392,7 @@ class ServerToolUse(BaseModel):
tool_search_requests: Optional[int] = None
-class Usage(CompletionUsage):
+class Usage(SafeAttributeModel, CompletionUsage):
_cache_creation_input_tokens: int = PrivateAttr(
0
) # hidden param for prompt caching. Might change, once openai introduces their equivalent.