Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions litellm/litellm_core_utils/prompt_templates/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved


def _azure_file_helper(content: ChatCompletionFileObject) -> None:
if isinstance(content.get("file"), dict):
content["file"].pop("format", None)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved


def convert_to_azure_openai_messages(
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

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"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,38 @@ def test_request_without_tools_is_unchanged(self):
)
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}},
],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Local Variable Rebinding

Reassigning messages violates the repository's no-rebinding directive. Bind the transformed result to a new Final variable before merging

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

}
]
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"
Loading