Skip to content

Commit 4eb9b41

Browse files
authored
fix (provider/google): support conversion of string enums to openapi spec (#4373)
1 parent 2495973 commit 4eb9b41

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

.changeset/breezy-peas-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/google': patch
3+
---
4+
5+
fix (provider/google): support conversion of string enums to openapi spec
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { google } from '@ai-sdk/google';
2+
import { generateObject } from 'ai';
3+
import 'dotenv/config';
4+
import { z } from 'zod';
5+
6+
async function main() {
7+
// enum support:
8+
const result = await generateObject({
9+
model: google('gemini-exp-1206'),
10+
schema: z.object({
11+
title: z.string(),
12+
kind: z.enum(['text', 'code', 'image']),
13+
}),
14+
prompt: 'Generate a software artifact.',
15+
});
16+
17+
console.log(JSON.stringify(result.object, null, 2));
18+
console.log();
19+
console.log('Token usage:', result.usage);
20+
console.log('Finish reason:', result.finishReason);
21+
}
22+
23+
main().catch(console.error);

packages/google/src/convert-json-schema-to-openapi-schema.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,12 @@ it('should return undefined for empty object schemas', () => {
479479
});
480480

481481
it('should handle non-empty object schemas', () => {
482-
const nonEmptySchema = {
482+
const nonEmptySchema: JSONSchema7 = {
483483
type: 'object',
484484
properties: {
485485
name: { type: 'string' },
486486
},
487-
} as const;
487+
};
488488

489489
expect(convertJSONSchemaToOpenAPISchema(nonEmptySchema)).toEqual({
490490
type: 'object',
@@ -493,3 +493,29 @@ it('should handle non-empty object schemas', () => {
493493
},
494494
});
495495
});
496+
497+
it('should convert string enum properties', () => {
498+
const schemaWithEnumProperty: JSONSchema7 = {
499+
type: 'object',
500+
properties: {
501+
kind: {
502+
type: 'string',
503+
enum: ['text', 'code', 'image'],
504+
},
505+
},
506+
required: ['kind'],
507+
additionalProperties: false,
508+
$schema: 'https://json-schema.org/draft/2019-09/schema#',
509+
};
510+
511+
expect(convertJSONSchemaToOpenAPISchema(schemaWithEnumProperty)).toEqual({
512+
type: 'object',
513+
properties: {
514+
kind: {
515+
type: 'string',
516+
enum: ['text', 'code', 'image'],
517+
},
518+
},
519+
required: ['kind'],
520+
});
521+
});

packages/google/src/convert-json-schema-to-openapi-schema.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function convertJSONSchemaToOpenAPISchema(
2727
format,
2828
const: constValue,
2929
minLength,
30+
enum: enumValues,
3031
} = jsonSchema;
3132

3233
const result: Record<string, unknown> = {};
@@ -55,6 +56,11 @@ export function convertJSONSchemaToOpenAPISchema(
5556
}
5657
}
5758

59+
// Handle enum
60+
if (enumValues !== undefined) {
61+
result.enum = enumValues;
62+
}
63+
5864
if (properties != null) {
5965
result.properties = Object.entries(properties).reduce(
6066
(acc, [key, value]) => {
@@ -81,7 +87,9 @@ export function convertJSONSchemaToOpenAPISchema(
8187
result.oneOf = oneOf.map(convertJSONSchemaToOpenAPISchema);
8288
}
8389

84-
if (minLength !== undefined) result.minLength = minLength;
90+
if (minLength !== undefined) {
91+
result.minLength = minLength;
92+
}
8593

8694
return result;
8795
}

0 commit comments

Comments
 (0)