Skip to content

Commit 1fb50b4

Browse files
authored
Merge pull request #597 from mkbehr/structured-output-enum
Add enum support to structured outputs.
2 parents 7bc1dca + 72d3275 commit 1fb50b4

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

functions/src/api/gemini.api.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ describe('Gemini API', () => {
9898
description: 'An integer-valued property',
9999
},
100100
},
101+
{
102+
name: 'enumProperty',
103+
schema: {
104+
type: StructuredOutputDataType.ENUM,
105+
description: 'An enum-valued property',
106+
enumItems: ['FOO', 'BAR', 'BAZ'],
107+
},
108+
},
101109
],
102110
},
103111
};
@@ -126,9 +134,14 @@ describe('Gemini API', () => {
126134
type: 'INTEGER',
127135
description: 'An integer-valued property',
128136
},
137+
enumProperty: {
138+
type: 'STRING',
139+
description: 'An enum-valued property',
140+
enum: ['FOO', 'BAR', 'BAZ'],
141+
},
129142
},
130-
propertyOrdering: ['stringProperty', 'integerProperty'],
131-
required: ['stringProperty', 'integerProperty'],
143+
propertyOrdering: ['stringProperty', 'integerProperty', 'enumProperty'],
144+
required: ['stringProperty', 'integerProperty', 'enumProperty'],
132145
},
133146
},
134147
};

functions/src/api/gemini.api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function makeStructuredOutputSchema(schema: StructuredOutputSchema): object {
4848
[StructuredOutputDataType.BOOLEAN]: 'BOOLEAN',
4949
[StructuredOutputDataType.ARRAY]: 'ARRAY',
5050
[StructuredOutputDataType.OBJECT]: 'OBJECT',
51+
[StructuredOutputDataType.ENUM]: 'STRING',
5152
};
5253
const type = typeMap[schema.type];
5354
if (!type) {
@@ -79,6 +80,7 @@ function makeStructuredOutputSchema(schema: StructuredOutputSchema): object {
7980
properties: properties,
8081
propertyOrdering: orderedPropertyNames,
8182
required: orderedPropertyNames,
83+
enum: schema.enumItems,
8284
items: itemsSchema,
8385
};
8486
}

functions/src/api/openai.api.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ describe('OpenAI-compatible API', () => {
115115
description: 'An integer-valued property',
116116
},
117117
},
118+
{
119+
name: 'enumProperty',
120+
schema: {
121+
type: StructuredOutputDataType.ENUM,
122+
description: 'An enum-valued property',
123+
enumItems: ['FOO', 'BAR', 'BAZ'],
124+
},
125+
},
118126
],
119127
},
120128
});
@@ -147,9 +155,14 @@ describe('OpenAI-compatible API', () => {
147155
type: 'INTEGER',
148156
description: 'An integer-valued property',
149157
},
158+
enumProperty: {
159+
type: 'STRING',
160+
description: 'An enum-valued property',
161+
enum: ['FOO', 'BAR', 'BAZ'],
162+
},
150163
},
151164
additionalProperties: false,
152-
required: ['stringProperty', 'integerProperty'],
165+
required: ['stringProperty', 'integerProperty', 'enumProperty'],
153166
};
154167
expect(parsedResponse.response_format).toEqual({
155168
type: 'json_schema',

functions/src/api/openai.api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function makeStructuredOutputSchema(schema: StructuredOutputSchema): object {
2222
[StructuredOutputDataType.BOOLEAN]: 'BOOLEAN',
2323
[StructuredOutputDataType.ARRAY]: 'ARRAY',
2424
[StructuredOutputDataType.OBJECT]: 'OBJECT',
25+
[StructuredOutputDataType.ENUM]: 'STRING',
2526
};
2627
const type = typeMap[schema.type];
2728
if (!type) {
@@ -55,6 +56,7 @@ function makeStructuredOutputSchema(schema: StructuredOutputSchema): object {
5556
properties: properties,
5657
additionalProperties: additionalProperties,
5758
required: orderedPropertyNames,
59+
enum: schema.enumItems,
5860
items: itemsSchema,
5961
};
6062
}

utils/src/structured_output.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ describe('Structured outputs', () => {
2323
},
2424
},
2525
},
26+
{
27+
name: 'enumProperty',
28+
schema: {
29+
type: StructuredOutputDataType.ENUM,
30+
description: 'An enum-valued property',
31+
enumItems: ['FOO', 'BAR', 'BAZ'],
32+
},
33+
},
2634
],
2735
};
2836

@@ -43,8 +51,13 @@ describe('Structured outputs', () => {
4351
type: 'integer',
4452
},
4553
},
54+
enumProperty: {
55+
description: 'An enum-valued property',
56+
type: 'enum',
57+
enum: ['FOO', 'BAR', 'BAZ'],
58+
},
4659
},
47-
required: ['stringProperty', 'intArrayProperty'],
60+
required: ['stringProperty', 'intArrayProperty', 'enumProperty'],
4861
};
4962
expect(parsedResult).toEqual(expectedResult);
5063
});

utils/src/structured_output.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ export enum StructuredOutputDataType {
1616
BOOLEAN = 'BOOLEAN',
1717
ARRAY = 'ARRAY',
1818
OBJECT = 'OBJECT',
19+
ENUM = 'ENUM',
1920
}
2021

2122
export interface StructuredOutputSchema {
2223
type: StructuredOutputDataType;
2324
description?: string;
2425
properties?: {name: string; schema: StructuredOutputSchema}[];
2526
arrayItems?: StructuredOutputSchema;
27+
enumItems?: string[];
2628
}
2729

2830
// TODO: Add StageKind or new enum to differentiate different configs
@@ -123,6 +125,7 @@ function schemaToObject(schema: StructuredOutputSchema): object {
123125
type: schema.type.toLowerCase(),
124126
properties: properties ?? undefined,
125127
items: arrayItems ?? undefined,
128+
enum: schema.enumItems ?? undefined,
126129
required: required,
127130
};
128131
}

0 commit comments

Comments
 (0)