fix(proxy): drop leftover multipart upload objects from image edit provider params

The image edit endpoint copied the raw multipart body into the request
data, then normalized the files under image and mask. When a client
used the array field names the OpenAI SDK sends, image[] and mask[],
the original UploadFile stayed in the body as an unknown param and was
forwarded to the provider as a text field holding its Python repr.
OpenAI rejected the request with "Invalid type for 'image[0]':
expected a file, but got a string instead."

Filter every UploadFile out of the parsed body before it can reach the
provider param path. Only the explicitly declared image and mask
uploads are converted and forwarded.
This commit is contained in:
mubashir1osmani 2026-09-17 14:34:24 -04:00
parent ab03850666
commit 0b192129bf
2 changed files with 54 additions and 6 deletions

View file

@ -6,6 +6,7 @@ from typing import Final, get_type_hints
import orjson
from fastapi import APIRouter, Depends, File, HTTPException, Request, Response, UploadFile, status
from fastapi.responses import ORJSONResponse
from starlette.datastructures import UploadFile as StarletteUploadFile
import litellm
from litellm.litellm_core_utils.prompt_templates.common_utils import (
@ -294,12 +295,11 @@ async def image_edit_api(
#########################################################
# Read request body and convert UploadFiles to BytesIO
#########################################################
data: Final = dict(
coerce_numeric_form_fields(
parsed_body=await _read_request_body(request=request),
numeric_fields=IMAGE_EDIT_NUMERIC_FORM_FIELDS,
)
parsed_body: Final = coerce_numeric_form_fields(
parsed_body=await _read_request_body(request=request),
numeric_fields=IMAGE_EDIT_NUMERIC_FORM_FIELDS,
)
data: Final = {key: value for key, value in parsed_body.items() if not isinstance(value, StarletteUploadFile)}
image_files: Final = await batch_to_bytesio(image)
mask_files: Final = await batch_to_bytesio(mask)
if image_files:

View file

@ -2,12 +2,13 @@ import asyncio
import copy
import logging
from collections.abc import Iterator, Mapping
from io import BytesIO
from types import SimpleNamespace
from typing import Any, Dict
import orjson
import pytest
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, UploadFile
from fastapi.testclient import TestClient
from starlette.requests import Request
from starlette.responses import Response
@ -172,6 +173,53 @@ def test_image_edit_multipart_n_that_is_not_a_number_is_left_alone(monkeypatch):
assert captured["n"] == "two"
PNG_BYTES = b"\x89PNG\r\n\x1a\n"
MASK_BYTES = b"\x89PNG\r\n\x1a\nmask"
def test_image_edit_multipart_array_aliases_do_not_leak_upload_objects_to_the_provider(monkeypatch):
"""`image[]`/`mask[]` (what the OpenAI SDK sends) used to survive in the body as
raw UploadFile objects and get forwarded as text provider params."""
captured: dict[str, Any] = {}
soap = PNG_BYTES
soap_two = PNG_BYTES + b"2"
mask = MASK_BYTES
mask_two = MASK_BYTES + b"2"
response = _image_edit_client(monkeypatch, captured).post(
"/v1/images/edits",
files=[
("image[]", ("soap.png", soap, "image/png")),
("image[]", ("soap-2.png", soap_two, "image/png")),
("mask[]", ("mask.png", mask, "image/png")),
("mask[]", ("mask-2.png", mask_two, "image/png")),
("reference", ("ref.png", PNG_BYTES, "image/png")),
],
data={"model": "gpt-image-1", "prompt": "studio ghibli", "seed": "42"},
)
assert response.status_code == 200
assert "image[]" not in captured
assert "mask[]" not in captured
assert "reference" not in captured
assert captured["seed"] == "42"
assert not any(isinstance(value, UploadFile) for value in captured.values())
images = captured["image"]
masks = captured["mask"]
assert isinstance(images, list) and len(images) == 2
assert isinstance(masks, list) and len(masks) == 2
for value, name, content in (
(images[0], "soap.png", soap),
(images[1], "soap-2.png", soap_two),
(masks[0], "mask.png", mask),
(masks[1], "mask-2.png", mask_two),
):
assert isinstance(value, BytesIO)
assert value.name == name
assert value.getvalue() == content
@pytest.mark.asyncio
async def test_a_model_the_router_cannot_serve_answers_an_openai_typed_error(monkeypatch: pytest.MonkeyPatch):
"""A bare HTTPException carries no type or param, so the tail used to ship the