Skip to content

fix(ai): tolerate null optional fields from openai-image hosts - #60192

Open
delrisu wants to merge 1 commit into
anomalyco:devfrom
delrisu:fix/openai-images-null-fields
Open

delrisu wants to merge 1 commit into
anomalyco:devfrom
delrisu:fix/openai-images-null-fields

Conversation

@delrisu

@delrisu delrisu commented Oct 29, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #60189

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

OpenAI-compatible image hosts (e.g. PSNC's llm.hpc.psnc.pl serving Z-Image-Turbo) commonly report absent optional response fields such as quality and background as an explicit null instead of omitting them.

The openai-images response schema declared those rendered-setting fields with Schema.optional(Schema.String), which only accepts undefined — never null. So a successful 200 response from such a host failed to decode with OpenAI Images returned an invalid response, and image generation was unusable.

This PR switches output_format, size, quality and background to optionalNullish, which tolerates null and normalizes it to undefined. This matches the null-tolerant handling already used for data[].url / data[].b64_json in the same response schema. optionalNullish (not plain optionalNull) keeps the decoded fields string | undefined, so downstream consumers that assume string | undefined are unaffected (typecheck passes).

How did you verify your code works?

  • Added a unit test in packages/ai/test/image.test.ts that returns an openai-images response where quality and background are null (plus url: null), and asserts it decodes successfully. Before the fix this test failed with Expected string | undefined at ["quality"] (the exact decode error from production); after the fix it passes.
  • bun test test/image.test.ts — 41 pass, 0 fail.
  • bun test test/provider/{zai-images,xai-images,alibaba-images,minimax-images,media-entrypoints,meta-images.recorded,xai-images.recorded,zai-images.recorded,openai-images.recorded}.test.ts — 50 pass, 0 fail (no regressions).
  • bun x tsc --noEmit in packages/ai — passes.

Visual

N/A (non-visual protocol fix).

Screenshots / recordings

N/A.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

OpenAI-compatible image hosts (e.g. PSNC's llm.hpc.psnc.pl serving
Z-Image-Turbo) commonly report absent optional response fields such as
`quality` and `background` as an explicit `null` instead of omitting
them. The openai-images response schema declared those with
Schema.optional, which rejects null, so a valid 200 response failed to
decode with "OpenAI Images returned an invalid response".

Use optionalNullish for the rendered-setting fields (output_format, size,
quality, background), matching the null-tolerant fields already used for
data[].url / data[].b64_json, and normalizing null to undefined.

Closes anomalyco#60189

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I reproduced #60189 from source on the base: a mock OpenAI-compatible host that returns background: null and quality: null gets OpenAI Images returned an invalid response with HTTP 502. With this PR the same request returns the image. The new test fails on the base and passes here. test/image.test.ts, the OpenAI images recorded tests and tsc all pass.

The null-to-undefined handling suits these fields. A null output_format falls back to working out the format from the bytes, just like a missing one, and the streaming events still override output_format as required.

Public API: unchanged. This only changes how provider responses are decoded inside packages/ai.

No blocking concerns.

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

None yet

Development

Successfully merging this pull request may close these issues.

ai: openai-compatible images reject null optional fields (background/quality) as 'invalid response'

1 participant