Skip to content

Commit 0f9d079

Browse files
Backport: fix(openai): allow 'revised_prompt' to be returned as null (#9900)
This is an automated backport of #9891 to the release-v5.0 branch. Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
1 parent 5919c9b commit 0f9d079

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.changeset/shy-kids-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/openai': patch
3+
---
4+
5+
fix(openai): revised_prompt sometimes returns null, causing errors

packages/openai/src/image/openai-image-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { lazyValidator, zodSchema } from '@ai-sdk/provider-utils';
22
import { z } from 'zod/v4';
33

4-
// minimal version of the schema, focussed on what is needed for the implementation
4+
// minimal version of the schema, focused on what is needed for the implementation
55
// this approach limits breakages when the API changes and increases efficiency
66
export const openaiImageResponseSchema = lazyValidator(() =>
77
zodSchema(
88
z.object({
99
data: z.array(
1010
z.object({
1111
b64_json: z.string(),
12-
revised_prompt: z.string().optional(),
12+
revised_prompt: z.string().nullish(),
1313
}),
1414
),
1515
}),

packages/openai/src/image/openai-image-model.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,38 @@ describe('doGenerate', () => {
238238
expect(requestBody).not.toHaveProperty('response_format');
239239
});
240240

241+
it('should handle null revised_prompt responses', async () => {
242+
server.urls['https://api.openai.com/v1/images/generations'].response = {
243+
type: 'json-value',
244+
body: {
245+
created: 1733837122,
246+
data: [
247+
{
248+
revised_prompt: null,
249+
b64_json: 'base64-image-1',
250+
},
251+
],
252+
},
253+
};
254+
255+
const result = await provider.image('gpt-image-1').doGenerate({
256+
prompt,
257+
n: 1,
258+
size: '1024x1024',
259+
aspectRatio: undefined,
260+
seed: undefined,
261+
providerOptions: {},
262+
});
263+
264+
expect(result.images).toStrictEqual(['base64-image-1']);
265+
expect(result.warnings).toStrictEqual([]);
266+
expect(result.providerMetadata).toStrictEqual({
267+
openai: {
268+
images: [null],
269+
},
270+
});
271+
});
272+
241273
it('should include response_format for dall-e-3', async () => {
242274
prepareJsonResponse();
243275

0 commit comments

Comments
 (0)