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]

[Bug Fix] Fix: _transform_responses_api_content_to_chat_completion_content` doesn't support file content type (#11494)

Ishaan Jaff96cba0148be7
diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py
index ef9a635aec9..d5bf7c606b8 100644
--- a/litellm/responses/litellm_completion_transformation/transformation.py
+++ b/litellm/responses/litellm_completion_transformation/transformation.py
@@ -208,9 +208,9 @@ class LiteLLMCompletionResponsesConfig:
_messages = litellm_completion_request.get("messages") or []
session_messages = chat_completion_session.get("messages") or []
litellm_completion_request["messages"] = session_messages + _messages
- litellm_completion_request[
- "litellm_trace_id"
- ] = chat_completion_session.get("litellm_session_id")
+ litellm_completion_request["litellm_trace_id"] = (
+ chat_completion_session.get("litellm_session_id")
+ )
return litellm_completion_request
@staticmethod
@@ -389,6 +389,26 @@ class LiteLLMCompletionResponsesConfig:
return [tool_output_message]
+ @staticmethod
+ def _transform_input_file_item_to_file_item(item: Dict[str, Any]) -> Dict[str, Any]:
+ """
+ Transform a Responses API input_file item to a Chat Completion file item
+
+ Args:
+ item: Dictionary containing input_file type with file_id and/or file_data
+
+ Returns:
+ Dictionary with transformed file structure for Chat Completion
+ """
+ file_dict: Dict[str, Any] = {}
+ keys = ["file_id", "file_data"]
+ for key in keys:
+ if item.get(key):
+ file_dict[key] = item.get(key)
+
+ new_item: Dict[str, Any] = {"type": "file", "file": file_dict}
+ return new_item
+
@staticmethod
def _transform_responses_api_content_to_chat_completion_content(
content: Any,
@@ -405,14 +425,21 @@ class LiteLLMCompletionResponsesConfig:
if isinstance(item, str):
content_list.append(item)
elif isinstance(item, dict):
- content_list.append(
- {
- "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(
- item.get("type") or "text"
- ),
- "text": item.get("text"),
- }
- )
+ if item.get("type") == "input_file":
+ content_list.append(
+ LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
+ item
+ )
+ )
+ else:
+ content_list.append(
+ {
+ "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(
+ item.get("type") or "text"
+ ),
+ "text": item.get("text"),
+ }
+ )
return content_list
else:
raise ValueError(f"Invalid content type: {type(content)}")
diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py
new file mode 100644
index 00000000000..77ecb48382f
--- /dev/null
+++ b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py
@@ -0,0 +1,121 @@
+import base64
+import json
+import os
+import sys
+
+import pytest
+from fastapi.testclient import TestClient
+
+sys.path.insert(
+ 0, os.path.abspath("../../..")
+) # Adds the parent directory to the system path
+
+from litellm.responses.litellm_completion_transformation.transformation import (
+ LiteLLMCompletionResponsesConfig,
+)
+
+
+class TestLiteLLMCompletionResponsesConfig:
+ def test_transform_input_file_item_to_file_item_with_file_id(self):
+ """Test transformation of input_file item with file_id to Chat Completion file format"""
+ # Setup
+ input_item = {"type": "input_file", "file_id": "file-abc123xyz"}
+
+ # Execute
+ result = (
+ LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
+ input_item
+ )
+ )
+
+ # Assert
+ expected = {"type": "file", "file": {"file_id": "file-abc123xyz"}}
+ assert result == expected
+ assert result["type"] == "file"
+ assert result["file"]["file_id"] == "file-abc123xyz"
+
+ def test_transform_input_file_item_to_file_item_with_file_data(self):
+ """Test transformation of input_file item with file_data to Chat Completion file format"""
+ # Setup
+ file_data = "base64encodeddata"
+ input_item = {"type": "input_file", "file_data": file_data}
+
+ # Execute
+ result = (
+ LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
+ input_item
+ )
+ )
+
+ # Assert
+ expected = {"type": "file", "file": {"file_data": file_data}}
+ assert result == expected
+ assert result["type"] == "file"
+ assert result["file"]["file_data"] == file_data
+
+ def test_transform_input_file_item_to_file_item_with_both_fields(self):
+ """Test transformation of input_file item with both file_id and file_data"""
+ # Setup
+ input_item = {
+ "type": "input_file",
+ "file_id": "file-abc123xyz",
+ "file_data": "base64encodeddata",
+ }
+
+ # Execute
+ result = (
+ LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
+ input_item
+ )
+ )
+
+ # Assert
+ expected = {
+ "type": "file",
+ "file": {"file_id": "file-abc123xyz", "file_data": "base64encodeddata"},
+ }
+ assert result == expected
+ assert result["type"] == "file"
+ assert result["file"]["file_id"] == "file-abc123xyz"
+ assert result["file"]["file_data"] == "base64encodeddata"
+
+ def test_transform_input_file_item_to_file_item_empty_file_fields(self):
+ """Test transformation of input_file item with no file_id or file_data"""
+ # Setup
+ input_item = {"type": "input_file"}
+
+ # Execute
+ result = (
+ LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
+ input_item
+ )
+ )
+
+ # Assert
+ expected = {"type": "file", "file": {}}
+ assert result == expected
+ assert result["type"] == "file"
+ assert result["file"] == {}
+
+ def test_transform_input_file_item_to_file_item_ignores_other_fields(self):
+ """Test that transformation only includes file_id and file_data, ignoring other fields"""
+ # Setup
+ input_item = {
+ "type": "input_file",
+ "file_id": "file-abc123xyz",
+ "extra_field": "should_be_ignored",
+ "another_field": 123,
+ }
+
+ # Execute
+ result = (
+ LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
+ input_item
+ )
+ )
+
+ # Assert
+ expected = {"type": "file", "file": {"file_id": "file-abc123xyz"}}
+ assert result == expected
+ assert "extra_field" not in result["file"]
+ assert "another_field" not in result["file"]