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 use get_file_check_sum

Ishaan Jaff67059266ca1f
diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py
index 8148147ef95..a9e53531670 100644
--- a/litellm/litellm_core_utils/core_helpers.py
+++ b/litellm/litellm_core_utils/core_helpers.py
@@ -1,6 +1,9 @@
# What is this?
## Helper utilities
-from typing import List, Literal, Optional, Tuple
+import os
+from typing import BinaryIO, List, Literal, Optional, Tuple
+
+from litellm._logging import verbose_logger
def map_finish_reason(
@@ -83,3 +86,20 @@ def _get_parent_otel_span_from_kwargs(kwargs: Optional[dict] = None):
return kwargs["litellm_parent_otel_span"]
except:
return None
+
+
+def get_file_check_sum(_file: BinaryIO):
+ """
+ Helper to safely get file checksum - used as a cache key
+ """
+ try:
+ file_descriptor = _file.fileno()
+ file_stat = os.fstat(file_descriptor)
+ file_size = str(file_stat.st_size)
+ file_checksum = _file.name + file_size
+ return file_checksum
+ except Exception as e:
+ verbose_logger.error(f"Error getting file_checksum: {(str(e))}")
+ file_checksum = _file.name
+ return file_checksum
+ return file_checksum
diff --git a/litellm/utils.py b/litellm/utils.py
index a20e9617272..35596c784cf 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -55,7 +55,10 @@ import litellm._service_logger # for storing API inputs, outputs, and metadata
import litellm.litellm_core_utils
import litellm.litellm_core_utils.json_validation_rule
from litellm.caching import DualCache
-from litellm.litellm_core_utils.core_helpers import map_finish_reason
+from litellm.litellm_core_utils.core_helpers import (
+ get_file_check_sum,
+ map_finish_reason,
+)
from litellm.litellm_core_utils.exception_mapping_utils import get_error_message
from litellm.litellm_core_utils.llm_request_utils import _ensure_extra_body_is_safe
from litellm.litellm_core_utils.redact_messages import (
@@ -557,12 +560,8 @@ def function_setup(
or call_type == CallTypes.transcription.value
):
_file_name: BinaryIO = args[1] if len(args) > 1 else kwargs["file"]
- file_name = getattr(_file_name, "name", "audio_file")
- file_descriptor = _file_name.fileno()
- file_stat = os.fstat(file_descriptor)
- file_size = str(file_stat.st_size)
-
- file_checksum = _file_name.name + file_size
+ file_checksum = get_file_check_sum(_file=_file_name)
+ file_name = _file_name.name
if "metadata" in kwargs:
kwargs["metadata"]["file_checksum"] = file_checksum
else: