兼容 /v1/images/edits 的 JSON 图片输入#146
Open
hzexe wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 issue
背景
当前
/v1/images/edits已支持标准 OpenAI-compatible multipart/form-data 图片编辑请求,例如-F image=@./input.png。但部分 OpenAI-compatible / LiteLLM 客户端会以 JSON body 调用图片编辑接口,把参考图放在
image/images[].image_url中,值为data:image/...;base64,...。这种请求在当前实现中会因为prompt是必填Form(...)参数而被 FastAPI 提前按表单解析,无法进入正常编辑逻辑。设计原则
这版只做增量兼容,不替换现有 multipart 解析路径:
edit_images原有File/Form参数;uploads = [*(image or []), *(image_list or [])]多图处理逻辑;image和image[]的 multipart 上传能力;prompt从必填Form(...)调整为可选,避免 JSON 请求被 FastAPI 在进入函数前拦截;Content-Type: application/json增加 JSON 分支;(bytes, filename, mime_type)结构,后续协议层不改。功能改动
保持不变
POST /v1/images/edits的标准 multipart/form-data 调用方式保持兼容;image上传保持兼容;image上传保持兼容;image[]上传保持兼容;model、n、size、response_format、stream继续进入同一 payload;services.protocol.openai_v1_image_edit.handle()未改动。新增支持
JSON 请求体支持:
image单图;images多图;data:image/...;base64,...;image/png;images[].image_url;images[].image_url.url;b64_json/base64对象字段。JSON 未传
model时沿用原接口默认值gpt-image-2;传入model时不会覆盖。安全与限制
http:///https://远程图片 URL,避免 SSRF;image/png、image/jpeg、image/jpg、image/webp、image/gif;n越界等客户端输入错误返回 400。本地回归测试
新增离线 API 测试:
test/test_v1_images_edits_json.py。覆盖:
model时沿用默认值;model时不覆盖;images[].image_url;image;image[];image+image[]混合;n越界。已本地通过:
结果:
真实服务验证
基于 fork 镜像:
在 8318 服务上完成 8 项真实接口验证,全部 HTTP 200 且产出图片:
验证结论: