mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-08 22:21:35 +00:00
Merge pull request #39525 from BerriAI/litellm_fix_gpt_image_background_dropped
fix(images): forward gpt-image supported params like background to OpenAI and Azure
This commit is contained in:
commit
4b1e24eae9
6 changed files with 90 additions and 7 deletions
|
|
@ -82,8 +82,8 @@ class GPTImageGenerationConfig(BaseImageGenerationConfig):
|
|||
)
|
||||
|
||||
# set optional params
|
||||
image_response.size = optional_params.get("size", "1024x1024") # default is always 1024x1024
|
||||
image_response.quality = optional_params.get("quality", "high") # always hd for dall-e-3
|
||||
image_response.output_format = optional_params.get("response_format", "png") # always png for dall-e-3
|
||||
image_response.size = image_response.size or optional_params.get("size", "1024x1024")
|
||||
image_response.quality = image_response.quality or optional_params.get("quality", "high")
|
||||
image_response.output_format = image_response.output_format or optional_params.get("output_format", "png")
|
||||
|
||||
return image_response
|
||||
|
|
|
|||
|
|
@ -2543,6 +2543,7 @@ class ImageResponse(OpenAIImageResponse, BaseLiteLLMOpenAIResponseObject):
|
|||
)
|
||||
super().__init__(created=created, data=_data, usage=_usage)
|
||||
|
||||
self.background = kwargs.get("background", None)
|
||||
self.quality = kwargs.get("quality", None)
|
||||
self.output_format = kwargs.get("output_format", None)
|
||||
self.size = kwargs.get("size", None)
|
||||
|
|
|
|||
|
|
@ -3338,6 +3338,9 @@ def get_optional_params_image_gen(
|
|||
continue
|
||||
passed_params[k] = v
|
||||
|
||||
provider_supported_params: Final[tuple[str, ...]] = (
|
||||
tuple(provider_config.get_supported_openai_params(model=model or "")) if provider_config is not None else ()
|
||||
)
|
||||
default_params: Final = {
|
||||
"n": None,
|
||||
"quality": None,
|
||||
|
|
@ -3348,6 +3351,7 @@ def get_optional_params_image_gen(
|
|||
"imageConfig": None,
|
||||
"tools": None,
|
||||
"web_search_options": None,
|
||||
**{k: None for k in provider_supported_params},
|
||||
}
|
||||
|
||||
non_default_params: Final = _get_non_default_params(
|
||||
|
|
@ -3407,10 +3411,9 @@ def get_optional_params_image_gen(
|
|||
if size is not None:
|
||||
optional_params["aspectRatio"] = _map_openai_size_to_vertex_ai_aspect_ratio(size)
|
||||
|
||||
openai_params: list[str] = list(default_params.keys())
|
||||
if provider_config is not None:
|
||||
supported_params = provider_config.get_supported_openai_params(model=model or "")
|
||||
openai_params = list(supported_params)
|
||||
openai_params: Final[list[str]] = (
|
||||
list(provider_supported_params) if provider_config is not None else list(default_params.keys())
|
||||
)
|
||||
|
||||
optional_params = add_provider_specific_params_to_optional_params(
|
||||
optional_params=optional_params,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
from unittest.mock import MagicMock
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from litellm.llms.azure.image_generation.gpt_transformation import AzureGPTImageGenerationConfig
|
||||
from litellm.llms.openai.image_generation.gpt_transformation import GPTImageGenerationConfig
|
||||
from litellm.types.utils import ImageResponse
|
||||
|
||||
|
||||
@pytest.mark.parametrize("config", [GPTImageGenerationConfig(), AzureGPTImageGenerationConfig()])
|
||||
def test_transform_image_generation_response_keeps_provider_echo(config):
|
||||
raw_response = httpx.Response(
|
||||
status_code=200,
|
||||
json={
|
||||
"created": 1788457009,
|
||||
"data": [{"b64_json": "/9j/4AAQSkZJRg=="}],
|
||||
"output_format": "jpeg",
|
||||
"background": "opaque",
|
||||
"quality": "low",
|
||||
"size": "1024x1024",
|
||||
},
|
||||
request=httpx.Request("POST", "https://api.openai.com/v1/images/generations"),
|
||||
)
|
||||
|
||||
image_response = config.transform_image_generation_response(
|
||||
model="gpt-image-2",
|
||||
raw_response=raw_response,
|
||||
model_response=ImageResponse(),
|
||||
logging_obj=MagicMock(),
|
||||
request_data={"prompt": "a red apple", "output_format": "jpeg"},
|
||||
optional_params={"output_format": "jpeg"},
|
||||
litellm_params={},
|
||||
encoding=None,
|
||||
)
|
||||
|
||||
assert image_response.output_format == "jpeg"
|
||||
assert image_response.quality == "low"
|
||||
assert image_response.background == "opaque"
|
||||
|
|
@ -330,6 +330,37 @@ def test_get_optional_params_image_gen():
|
|||
assert optional_params["n"] == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize("custom_llm_provider", ["openai", "azure"])
|
||||
def test_get_optional_params_image_gen_keeps_gpt_image_supported_params(custom_llm_provider):
|
||||
"""https://github.com/BerriAI/litellm/issues/38649"""
|
||||
from litellm.types.utils import LlmProviders
|
||||
|
||||
provider_config = ProviderConfigManager.get_provider_image_generation_config(
|
||||
model="gpt-image-2", provider=LlmProviders(custom_llm_provider)
|
||||
)
|
||||
optional_params = get_optional_params_image_gen(
|
||||
model="gpt-image-2",
|
||||
n=1,
|
||||
size="1024x1024",
|
||||
custom_llm_provider=custom_llm_provider,
|
||||
provider_config=provider_config,
|
||||
background="transparent",
|
||||
output_format="png",
|
||||
moderation="low",
|
||||
output_compression=50,
|
||||
unknown_param="kept-in-extra-body",
|
||||
)
|
||||
assert optional_params == {
|
||||
"n": 1,
|
||||
"size": "1024x1024",
|
||||
"background": "transparent",
|
||||
"output_format": "png",
|
||||
"moderation": "low",
|
||||
"output_compression": 50,
|
||||
"extra_body": {"unknown_param": "kept-in-extra-body"},
|
||||
}
|
||||
|
||||
|
||||
def test_get_optional_params_image_gen_vertex_ai_size():
|
||||
"""Test that Vertex AI image generation properly handles size parameter and maps it to aspectRatio"""
|
||||
# Test with various size parameters
|
||||
|
|
|
|||
|
|
@ -759,3 +759,12 @@ def test_delta_function_tool_call_unchanged_by_custom_support():
|
|||
delta = Delta(tool_calls=[{"index": 0, "id": "c2", "type": "function", "function": {"name": "g", "arguments": ""}}])
|
||||
assert isinstance(delta.tool_calls[0], ChatCompletionDeltaToolCall)
|
||||
assert "custom" not in delta.model_dump()["tool_calls"][0]
|
||||
|
||||
|
||||
def test_image_response_keeps_background():
|
||||
"""https://github.com/BerriAI/litellm/issues/38649"""
|
||||
from litellm.types.utils import ImageResponse
|
||||
|
||||
response = ImageResponse(created=1, data=[{"b64_json": "aGk="}], background="transparent", output_format="png")
|
||||
assert response.background == "transparent"
|
||||
assert response.model_dump()["background"] == "transparent"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue