mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-23 00:41:40 +00:00
fix(azure): strip litellm format field from file and image content parts
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
2e06d195b2
commit
f925c1d1e6
3 changed files with 75 additions and 0 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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"""
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue