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(azure): strip litellm format field from file and image content parts

Devin AIf925c1d1e6b3
diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py
index 21ae8b001dd..f9b8922d019 100644
--- a/litellm/litellm_core_utils/prompt_templates/factory.py
+++ b/litellm/litellm_core_utils/prompt_templates/factory.py
@@ -1067,6 +1067,13 @@ def _azure_tool_call_invoke_helper(
def _azure_image_url_helper(content: ChatCompletionImageObject):
if isinstance(content["image_url"], str):
content["image_url"] = {"url": content["image_url"]}
+ elif isinstance(content["image_url"], dict):
+ content["image_url"].pop("format", None)
+
+
+def _azure_file_helper(content: ChatCompletionFileObject) -> None:
+ if isinstance(content.get("file"), dict):
+ content["file"].pop("format", None)
def convert_to_azure_openai_messages(
@@ -1082,6 +1089,8 @@ def convert_to_azure_openai_messages(
for content in m.get("content", []):
if isinstance(content, dict) and content.get("type") == "image_url":
_azure_image_url_helper(content)
+ elif isinstance(content, dict) and content.get("type") == "file":
+ _azure_file_helper(content)
return messages
diff --git a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
index 66d10fd1407..bbfafb41243 100644
--- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
+++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
@@ -297,6 +297,37 @@ def test_convert_to_azure_openai_messages():
assert content == expected_content
+def test_convert_to_azure_openai_messages_strips_litellm_format_from_file_and_image():
+ """Managed file ids write file.format = MIME type, which Azure rejects"""
+
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_azure_openai_messages,
+ )
+ from litellm.types.llms.openai import AllMessageValues
+
+ input: list[AllMessageValues] = [
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "file",
+ "file": {"file_id": "assistant-xyz", "format": "application/pdf"},
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": "https://x/y.png", "format": "image/png"},
+ },
+ ],
+ }
+ ]
+
+ output = convert_to_azure_openai_messages(input)
+
+ content = output[0].get("content")
+ assert content[0]["file"] == {"file_id": "assistant-xyz"}
+ assert content[1]["image_url"] == {"url": "https://x/y.png"}
+
+
def test_bedrock_validate_format_image_or_video():
"""Test the _validate_format method for images, videos, and documents"""
diff --git a/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py b/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
index bc6cb0c0fed..b5c72d5bb06 100644
--- a/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
+++ b/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py
@@ -307,3 +307,38 @@ class TestAzureToolSchemaCombinatorFlattening:
)
assert "tools" not in request
assert request["temperature"] == 0.2
+
+
+def test_transform_request_strips_litellm_format_from_managed_file_id():
+ """update_messages_with_model_file_ids writes file.format = MIME type, which Azure rejects"""
+ import base64
+
+ from litellm.litellm_core_utils.prompt_templates.common_utils import (
+ update_messages_with_model_file_ids,
+ )
+
+ managed_file_id: Final = base64.b64encode(
+ b"litellm_proxy:application/pdf;unified_id,abc123;llm_output_file_id,assistant-xyz;target_model_names,azure-gpt"
+ ).decode()
+ messages = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Summarize this file"},
+ {"type": "file", "file": {"file_id": managed_file_id}},
+ ],
+ }
+ ]
+ messages = update_messages_with_model_file_ids(messages, None, {})
+
+ request = AzureOpenAIConfig().transform_request(
+ model="gpt-5.4",
+ messages=messages,
+ optional_params={},
+ litellm_params={},
+ headers={},
+ )
+
+ file_part = request["messages"][0]["content"][1]["file"]
+ assert "format" not in file_part
+ assert file_part["file_id"] == "assistant-xyz"