litellm/tests/integration/providers/test_openai_image_edit_wire.py
SanthoshSachin 28a71ab46c
fix(images): send image_edit markup as a multipart file part (#53815)
* fix(images): send image_edit markup as a multipart file part

* fix(images): unpack FileTypes tuples for multipart uploads and add Final annotations

* fix(images): avoid Unknown tuple narrowing in _build_upload_part for basedpyright

* fix(images): send tuple mask uploads through the shared upload part helper

Mask uploads now go through _add_image_to_files like image and markup, so a (filename, content[, content_type[, headers]]) mask tuple is sent as that file instead of nested as the part's content

* fix(images): reject JSON image references for mask and markup, send the first markup of a list, and let Azure drop markup

A mask or markup given as a JSON image reference now gets a 400 from LiteLLM before any provider call, instead of a 500 from the multipart send

A markup list sends its first file, the same as mask

Azure answers Unknown parameter for markup, so Azure no longer lists it, and drop_params drops it

The FLUX 2 edit config now returns list[str] like its parent

* fix(hosted_vllm): exclude unsupported markup param from image_edit config

* fix(images): stop listing markup for Azure AI FLUX Kontext and name the resolved provider on image edit errors

The JSON image reference 400 used litellm_params built before the provider was resolved, so azure, azure_ai and litellm_proxy errors said openai. The hosted vLLM markup test now goes through litellm.image_edit, and the JSON reference test covers a list holding a reference

* fix(images): let the markup type hint take a list like image

* fix(images): read pathlib.Path image edit uploads, send base file names and reject string mask or markup

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-25 04:14:57 +00:00

576 lines
25 KiB
Python

import json
import struct
import zlib
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from email.message import Message
from email.parser import BytesParser
from email.policy import HTTP
from typing import Final, cast
import httpx
import openai
import pytest
from integration._support.client import JSON_OBJECT, Gateway, object_value, string_value
from integration._support.wire import Reply, Request, wire_server
from pydantic import BaseModel, JsonValue
_PNG_BYTES: Final = (
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00"
b"\x1f\x15\xc4\x89\x00\x00\x00\rIDAT\x08\xd7c\xf8\xcf\xc0\xf0\x1f\x00\x05\x00\x01\xff"
b"\x89\x99=\x1d\x00\x00\x00\x00IEND\xaeB`\x82"
)
_PROMPT: Final = "turn the red circle green"
_EDITED_IMAGE_B64: Final = "aW50ZWdyYXRpb24tZWRpdGVkLWltYWdl"
def _png_chunk(chunk_type: bytes, chunk_data: bytes) -> bytes:
checksum: Final = zlib.crc32(chunk_type + chunk_data) & 0xFFFFFFFF
return (
struct.pack(">I", len(chunk_data))
+ chunk_type
+ chunk_data
+ struct.pack(">I", checksum)
)
def _png_from_rgba_pixel(pixel: tuple[int, int, int, int]) -> bytes:
header: Final = struct.pack(">IIBBBBB", 1, 1, 8, 6, 0, 0, 0)
compressed_pixel: Final = zlib.compress(b"\x00" + bytes(pixel))
return (
b"\x89PNG\r\n\x1a\n"
+ _png_chunk(b"IHDR", header)
+ _png_chunk(b"IDAT", compressed_pixel)
+ _png_chunk(b"IEND", b"")
)
_SECOND_PNG: Final = _png_from_rgba_pixel((0, 0, 255, 255))
_MASK_PNG: Final = _png_from_rgba_pixel((0, 0, 0, 0))
class _Image(BaseModel):
b64_json: str
class _ImageResponse(BaseModel):
data: tuple[_Image, ...]
def _multipart_parts(request: Request) -> tuple[Message, ...]:
envelope: Final = f"content-type: {request.headers['content-type']}\r\n\r\n".encode() + request.body
parsed: Final = BytesParser(policy=HTTP).parsebytes(envelope)
assert parsed.is_multipart(), request.headers["content-type"]
return tuple(parsed.iter_parts())
@dataclass(frozen=True, slots=True)
class _FilePart:
name: str
filename: str
content_type: str
content: bytes
def _text_parts(parts: tuple[Message, ...]) -> tuple[tuple[str, str], ...]:
return tuple(
sorted(
(
cast(str, part.get_param("name", header="content-disposition")),
cast(bytes, part.get_payload(decode=True)).decode(),
)
for part in parts
if part.get_filename() is None
)
)
def _file_parts(parts: tuple[Message, ...]) -> tuple[_FilePart, ...]:
return tuple(
_FilePart(
name=cast(str, part.get_param("name", header="content-disposition")),
filename=filename,
content_type=part.get_content_type(),
content=cast(bytes, part.get_payload(decode=True)),
)
for part in parts
if (filename := part.get_filename()) is not None
)
def _edit(
gateway: Gateway,
fields: Mapping[str, str],
files: Sequence[tuple[str, tuple[str, bytes, str]]],
path: str = "/v1/images/edits",
) -> httpx.Response:
return gateway.client.post(
path,
data=dict(fields),
files=list(files),
headers={"Authorization": f"Bearer {gateway.key}"},
)
@pytest.mark.covers("other.provider_wire.openai.image_edit_forwards_provider_specific_form_fields")
def test_openai_compatible_image_edit_forwards_seed_form_field_to_backend(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("prompt", _PROMPT), ("seed", "42"))
assert _file_parts(parts) == (_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request_multipart(
"/v1/images/edits",
{"model": model, "prompt": _PROMPT, "seed": "42"},
{"image": ("red_circle.png", _PNG_BYTES, "image/png")},
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
@pytest.mark.parametrize(
"path_template",
("/v1/images/edits", "/images/edits", "/openai/deployments/{model}/images/edits"),
ids=("v1", "unversioned", "azure_deployment"),
)
def test_image_edit_bracketed_image_field_reaches_openai_as_one_file_part(
gateway: Gateway, path_template: str
) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("prompt", _PROMPT), ("seed", "42"))
assert _file_parts(parts) == (_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT, "seed": "42"},
(("image[]", ("red_circle.png", _PNG_BYTES, "image/png")),),
path_template.format(model=model),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
def test_image_edit_repeated_bracketed_image_parts_reach_openai_in_upload_order(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("n", "2"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("image[]", "image.png", "image/png", _SECOND_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT, "n": "2"},
(
("image[]", ("first.png", _PNG_BYTES, "image/png")),
("image[]", ("second.png", _SECOND_PNG, "image/png")),
),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
def test_image_edit_bracketed_mask_reaches_openai_as_mask_file_part(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("mask", "mask.png", "image/png", _MASK_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT},
(
("image[]", ("red_circle.png", _PNG_BYTES, "image/png")),
("mask[]", ("mask.png", _MASK_PNG, "image/png")),
),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
def test_image_edit_repeated_bracketed_mask_parts_reach_openai_as_first_mask_only(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("mask", "mask.png", "image/png", _MASK_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT},
(
("image[]", ("red_circle.png", _PNG_BYTES, "image/png")),
("mask[]", ("first-mask.png", _MASK_PNG, "image/png")),
("mask[]", ("second-mask.png", _SECOND_PNG, "image/png")),
),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
def test_image_edit_plain_mask_reaches_openai_as_mask_file_part(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("mask", "mask.png", "image/png", _MASK_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT},
(
("image", ("red_circle.png", _PNG_BYTES, "image/png")),
("mask", ("mask.png", _MASK_PNG, "image/png")),
),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
def test_image_edit_repeated_plain_image_parts_reach_openai_as_two_file_parts(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("image[]", "image.png", "image/png", _SECOND_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT},
(
("image", ("first.png", _PNG_BYTES, "image/png")),
("image", ("second.png", _SECOND_PNG, "image/png")),
),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
@pytest.mark.parametrize("field", ("markup", "markup[]"))
def test_image_edit_markup_upload_reaches_openai_as_markup_file_part(gateway: Gateway, field: str) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-2.6-flare"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("markup", "markup.png", "image/png", _MASK_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-2.6-flare", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT},
(
("image", ("red_circle.png", _PNG_BYTES, "image/png")),
(field, ("circle-outline.png", _MASK_PNG, "image/png")),
),
)
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
def _sdk_client(gateway: Gateway, model: str, alias: str) -> openai.OpenAI:
base: Final = str(gateway.client.base_url)
match alias:
case "v1":
return openai.OpenAI(base_url=f"{base}/v1", api_key=gateway.key, max_retries=0)
case "azure_deployment":
return openai.AzureOpenAI(
azure_endpoint=base,
azure_deployment=model,
api_version="2025-04-01-preview",
api_key=gateway.key,
max_retries=0,
)
raise AssertionError(alias)
@pytest.mark.parametrize("alias", ("v1", "azure_deployment"))
def test_openai_sdk_image_edit_with_image_list_and_mask_reaches_openai_as_file_parts(
gateway: Gateway, alias: str
) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-image-1"), ("n", "2"), ("prompt", _PROMPT))
assert _file_parts(parts) == (
_FilePart("image[]", "image.png", "image/png", _PNG_BYTES),
_FilePart("image[]", "image.png", "image/png", _SECOND_PNG),
_FilePart("mask", "mask.png", "image/png", _MASK_PNG),
)
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
client: Final = _sdk_client(gateway, model, alias)
result: Final = client.images.edit(
model=model,
image=[
("first.png", _PNG_BYTES, "image/png"),
("second.png", _SECOND_PNG, "image/png"),
],
mask=("mask.png", _MASK_PNG, "image/png"),
prompt=_PROMPT,
n=2,
)
assert [image.b64_json for image in result.data] == [_EDITED_IMAGE_B64]
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/images/edits")]
@pytest.mark.parametrize(
("files", "detail"),
(
pytest.param(
(
("image", ("red_circle.png", _PNG_BYTES, "image/png")),
("image[]", ("second.png", _SECOND_PNG, "image/png")),
),
"Cannot specify both 'image' and 'image[]'",
id="image",
),
pytest.param(
(
("image", ("red_circle.png", _PNG_BYTES, "image/png")),
("mask", ("mask.png", _MASK_PNG, "image/png")),
("mask[]", ("mask-array.png", _MASK_PNG, "image/png")),
),
"Cannot specify both 'mask' and 'mask[]'",
id="mask",
),
pytest.param(
(
("image", ("red_circle.png", _PNG_BYTES, "image/png")),
("markup", ("markup.png", _MASK_PNG, "image/png")),
("markup[]", ("markup-array.png", _MASK_PNG, "image/png")),
),
"Cannot specify both 'markup' and 'markup[]'",
id="markup",
),
),
)
def test_image_edit_rejects_plain_and_bracketed_spelling_together_without_calling_upstream(
gateway: Gateway,
files: Sequence[tuple[str, tuple[str, bytes, str]]],
detail: str,
) -> None:
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/images/edits"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
assert request.headers["content-type"].startswith("multipart/form-data; boundary=")
return Reply(body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = _edit(
gateway,
{"model": model, "prompt": _PROMPT},
files,
)
assert response.status_code == 422, response.text
assert response.json() == {"detail": detail}, response.text
assert wire.drain() == ()
_IMAGE_REFERENCES: Final[list[JsonValue]] = [
{"image_url": "https://images.example/red_circle.png"},
{"image_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg=="},
]
_MASK_REFERENCE: Final[dict[str, JsonValue]] = {"image_url": "https://images.example/mask.png"}
def _json_body(request: Request) -> dict[str, JsonValue]:
assert request.headers["content-type"].split(";")[0].strip() == "application/json", (
request.headers["content-type"],
request.body,
)
return JSON_OBJECT.validate_json(request.body)
def test_image_edit_json_image_references_reach_openai_as_the_same_json_body(gateway: Gateway) -> None:
"""regression: a JSON edit with image and mask references reaches OpenAI as application/json, fields unchanged."""
with (
wire_server(
lambda _: Reply(
body=json.dumps({"created": 1700000000, "data": [{"b64_json": _EDITED_IMAGE_B64}]}).encode()
)
) as wire,
gateway.scenario() as scenario,
):
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request(
"POST",
"/v1/images/edits",
{
"model": model,
"prompt": _PROMPT,
"images": _IMAGE_REFERENCES,
"mask": _MASK_REFERENCE,
"quality": "high",
"size": "1024x1024",
"output_format": "png",
},
)
received: Final = wire.drain()
assert [(request.method, request.target) for request in received] == [("POST", "/v1/images/edits")], (
response.status_code,
response.text,
)
assert received[0].headers["authorization"] == "Bearer synthetic-openai-key"
assert _json_body(received[0]) == {
"model": "gpt-image-1",
"prompt": _PROMPT,
"images": _IMAGE_REFERENCES,
"mask": _MASK_REFERENCE,
"quality": "high",
"size": "1024x1024",
"output_format": "png",
}
assert response.status_code == 200, response.text
payload: Final = _ImageResponse.model_validate_json(response.content)
assert [image.b64_json for image in payload.data] == [_EDITED_IMAGE_B64], response.text
def test_image_edit_json_image_references_return_the_openai_400_error_to_the_caller(gateway: Gateway) -> None:
"""regression: when OpenAI rejects a JSON reference edit with a 400, the caller gets that 400 and its message."""
upstream_message: Final = "Invalid image URL: the image at images[0] could not be downloaded."
upstream_error: Final = {
"error": {
"message": upstream_message,
"type": "invalid_request_error",
"param": "images",
"code": "invalid_image_url",
}
}
with (
wire_server(lambda _: Reply(status=400, body=json.dumps(upstream_error).encode())) as wire,
gateway.scenario() as scenario,
):
model: Final = scenario.model(
model="openai/gpt-image-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request(
"POST",
"/v1/images/edits",
{"model": model, "prompt": _PROMPT, "images": _IMAGE_REFERENCES, "mask": _MASK_REFERENCE},
)
received: Final = wire.drain()
assert [(request.method, request.target) for request in received] == [("POST", "/v1/images/edits")], (
response.status_code,
response.text,
)
sent: Final = _json_body(received[0])
assert (sent.get("images"), sent.get("mask")) == (_IMAGE_REFERENCES, _MASK_REFERENCE)
assert response.status_code == 400, response.text
error: Final = object_value(JSON_OBJECT.validate_json(response.content)["error"])
assert upstream_message in string_value(error["message"]), response.text