Skip to content

fix(js): preserve image indices for images.edit mask - #4475

Open
feiiiiii5 wants to merge 2 commits into
Arize-ai:mainfrom
feiiiiii5:feiiiiii5/1126-D273
Open

feiiiiii5 wants to merge 2 commits into
Arize-ai:mainfrom
feiiiiii5:feiiiiii5/1126-D273

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

Fixes #4375

images.edit passes the source image and mask as one flat upload list. getInputImageAttributes() compacts indices whenever an upload cannot be read, so when the source is an async iterable (or exceeds base64ImageMaxLength) the readable mask shifts to index 0 and is emitted as input.images.0.image.url — it becomes the source image.

Fix

Added an opt-in preserveIndices option to getInputImageAttributes() that reserves a slot for unreadable uploads. It is enabled only for the images.edit path; all other callers keep the existing contiguous-index behavior. Added a patch changeset.

Verification

  • New regression test with an unreadable async-iterable source and a readable PNG mask: base-red expected 'data:image/png;base64,...' to be undefined at input.images.0; with fix mask is at input.images.1 and index 0 is absent.
  • Target file 73/73; whole package 199 passed; type:check, oxlint, oxfmt --check and build all clean.

No real API calls or credentials (all openai.post calls mocked). Only pinned SDK/Node versions tested.

@ningsiyuan1024

Copy link
Copy Markdown

Thanks for the fix. I checked the multi-image case against 2d10c59 and this PR at 55b63f0 with Node 24.14.0 and OpenAI SDK 7.30.0.

I called the real client.images.edit({ image: [first, second], mask, … }) with three distinct synthetic PNGs. A local fetch handler decoded the SDK's multipart request and compared both image[] parts and the mask part byte for byte. I also checked the exported OpenTelemetry span.

First source and trace config Base trace PR trace
Readable File 0: first, 1: second, 2: mask Same
One-shot async iterable 0: second, 1: mask 0 absent, 1: second, 2: mask
Readable File over base64ImageMaxLength 0: __REDACTED__, 1: second, 2: mask Same
hideInputImages: true No input.images.* attributes Same

The numbers above are the indices in input.images.N.image.url. Every run produced one successful span and one edit request with all three file parts in the expected order. The one-shot source was iterated exactly once on both revisions and its full bytes reached the multipart request. The local handler supplied the response without contacting OpenAI.

This confirms the index fix with two source images and checks that it leaves uploads, size redaction, and image hiding intact. If you'd like to extend the regression here, the extra assertions are that input.images.1.image.url contains the second source and input.images.2.image.url contains the mask when the first source can't be captured.

@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Thanks @ningsiyuan1024 for the independent verification with real client.images.edit() calls and byte-for-byte multipart comparison — that is exactly the missing evidence for the async-iterable case, and the table confirms the index fix without changing uploads, redaction, or hiding.

I'll add the two suggested assertions to the regression so the test pins not only the first slot's shift but also that input.images.1 and input.images.2 carry the second source and the mask when the first source is uncapturable. Pushing a focused test-only commit shortly.

@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Pushed the suggested assertions in a test-only commit (55b63f02 → new head). The new case supplies [unreadableSource, secondImage] plus a mask and asserts slot 0 is absent, slot 1 is the JPEG data URL, and slot 2 is the mask data URL.

Verification note: this workspace has no installed JS dependencies for this package and no network, so I could not run vitest locally. The new test mirrors the existing edited-image tests in the same module. If you have the suite running, a quick vitest run test/openai.images.test.ts on the new head would confirm it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[bug] JS openai: images.edit records its mask as an ordinary input image

2 participants