fix: remove hardcoded gpt-image-2 size constraints

This commit is contained in:
CrystalVibe28 2026-05-15 18:49:36 +08:00
parent bc3f09f362
commit 765cc80adf
2 changed files with 18 additions and 51 deletions

View file

@ -1,5 +1,4 @@
import json
import re
import base64
from io import BufferedReader, BytesIO
from os import PathLike
@ -42,11 +41,6 @@ from ..common_utils import (
)
GPT_IMAGE_MODEL_PREFIX = "gpt-image-"
GPT_IMAGE_2_MODEL_PREFIX = "gpt-image-2"
GPT_IMAGE_2_MIN_PIXELS = 655_360
GPT_IMAGE_2_MAX_PIXELS = 8_294_400
GPT_IMAGE_2_MAX_EDGE = 3840
GPT_IMAGE_2_MAX_RATIO = 3.0
ALLOWED_OUTPUT_FORMATS = {"png", "jpeg", "webp"}
INTERNAL_OPTIONAL_PARAMS = {"chatgpt_responses_model"}
@ -231,46 +225,6 @@ class ChatGPTImageGenerationConfig(BaseImageGenerationConfig):
if output_format is not None and output_format not in ALLOWED_OUTPUT_FORMATS:
raise ValueError("output_format must be one of png, jpeg, or webp")
size = optional_params.get("size")
if size is not None and model.startswith(GPT_IMAGE_2_MODEL_PREFIX):
self._validate_gpt_image_2_size(size)
@staticmethod
def _parse_size(size: str) -> Optional[Tuple[int, int]]:
match = re.fullmatch(r"([1-9][0-9]*)x([1-9][0-9]*)", size)
if not match:
return None
return int(match.group(1)), int(match.group(2))
def _validate_gpt_image_2_size(self, size: str) -> None:
if size == "auto":
return
parsed = self._parse_size(size)
if parsed is None:
raise ValueError("size must be auto or WIDTHxHEIGHT, for example 1024x1024")
width, height = parsed
max_edge = max(width, height)
min_edge = min(width, height)
total_pixels = width * height
if max_edge > GPT_IMAGE_2_MAX_EDGE:
raise ValueError("gpt-image-2 size maximum edge length must be <= 3840px")
if width % 16 != 0 or height % 16 != 0:
raise ValueError(
"gpt-image-2 size width and height must be multiples of 16px"
)
if max_edge / min_edge > GPT_IMAGE_2_MAX_RATIO:
raise ValueError("gpt-image-2 size ratio must not exceed 3:1")
if (
total_pixels < GPT_IMAGE_2_MIN_PIXELS
or total_pixels > GPT_IMAGE_2_MAX_PIXELS
):
raise ValueError(
"gpt-image-2 total pixels must be between 655,360 and 8,294,400"
)
def transform_image_generation_response(
self,
model: str,

View file

@ -107,10 +107,6 @@ def test_chatgpt_image_generation_forwards_supported_generate_params(
"optional_params, error",
[
({"output_format": "jpg"}, "output_format must be one of png, jpeg, or webp"),
({"size": "1535x1024"}, "multiples of 16px"),
({"size": "4096x1024"}, "maximum edge length"),
({"size": "1024x256"}, "ratio must not exceed 3:1"),
({"size": "512x512"}, "total pixels must be between"),
],
)
def test_chatgpt_image_generation_validates_params(
@ -489,7 +485,7 @@ def test_chatgpt_image_generation_uses_optional_responses_model(monkeypatch, tmp
("dall-e-3", {}, "requires a GPT Image model"),
("gpt-image-1.5", {"size": "auto"}, None),
("gpt-image-2", {"size": "auto"}, None),
("gpt-image-2", {"size": "bad-size"}, "size must be auto or WIDTHxHEIGHT"),
("gpt-image-2", {"size": "bad-size"}, None),
],
)
def test_chatgpt_image_generation_validates_additional_param_paths(
@ -518,6 +514,23 @@ def test_chatgpt_image_generation_validates_additional_param_paths(
)
def test_chatgpt_image_generation_forwards_size_without_local_constraints(
monkeypatch, tmp_path
):
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path))
config = ChatGPTImageGenerationConfig()
request = config.transform_image_generation_request(
model="gpt-image-2",
prompt="draw a cat",
optional_params={"size": "bad-size"},
litellm_params={},
headers={},
)
assert request["tools"][0]["size"] == "bad-size"
def test_chatgpt_image_generation_extracts_b64_from_deep_nested_payload(
monkeypatch, tmp_path
):