Skip to content

Commit f87c15e

Browse files
authored
fix(ai): Fix responseSchema bug (#9791)
1 parent e8f14eb commit f87c15e

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

.changeset/shiny-papayas-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/ai': patch
3+
---
4+
5+
Fixed a bug that causes the model to error if the user specifies `responseSchema` or `responseJsonSchema`.

packages/ai/src/models/generative-model.test.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import { use, expect } from 'chai';
18-
import { GenerativeModel } from './generative-model';
18+
import { GenerativeModel, validateGenerationConfig } from './generative-model';
1919
import {
2020
FunctionCallingMode,
2121
AI,
@@ -956,3 +956,73 @@ describe('GenerativeModel dispatch logic', () => {
956956
});
957957
});
958958
});
959+
960+
describe('validateGenerationConfig', () => {
961+
it('does not allow setting both thinkingBudget and thinkingLevel', () => {
962+
expect(() => {
963+
validateGenerationConfig({
964+
thinkingConfig: {
965+
thinkingBudget: 200
966+
}
967+
});
968+
}).to.not.throw();
969+
expect(() => {
970+
validateGenerationConfig({
971+
thinkingConfig: {
972+
thinkingLevel: ThinkingLevel.LOW
973+
}
974+
});
975+
}).to.not.throw();
976+
expect(() => {
977+
validateGenerationConfig({
978+
thinkingConfig: {
979+
thinkingBudget: 200,
980+
thinkingLevel: ThinkingLevel.LOW
981+
}
982+
});
983+
}).to.throw();
984+
});
985+
it('does not allow setting both responseSchema and responseJsonSchema', () => {
986+
expect(() => {
987+
validateGenerationConfig({
988+
responseSchema: {},
989+
responseMimeType: 'application/json'
990+
});
991+
}).to.not.throw();
992+
expect(() => {
993+
validateGenerationConfig({
994+
responseJsonSchema: {},
995+
responseMimeType: 'application/json'
996+
});
997+
}).to.not.throw();
998+
expect(() => {
999+
validateGenerationConfig({
1000+
responseSchema: {},
1001+
responseJsonSchema: {},
1002+
responseMimeType: 'application/json'
1003+
});
1004+
}).to.throw();
1005+
});
1006+
it(
1007+
'throws if responseSchema or responseJsonSchema are set' +
1008+
' and responseMimeType is not "application/json"',
1009+
() => {
1010+
expect(() => {
1011+
validateGenerationConfig({
1012+
responseSchema: {}
1013+
});
1014+
}).to.throw();
1015+
expect(() => {
1016+
validateGenerationConfig({
1017+
responseJsonSchema: {}
1018+
});
1019+
}).to.throw();
1020+
expect(() => {
1021+
validateGenerationConfig({
1022+
responseJsonSchema: {},
1023+
responseMimeType: 'text/plain'
1024+
});
1025+
}).to.throw();
1026+
}
1027+
);
1028+
});

packages/ai/src/models/generative-model.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ export class GenerativeModel extends AIModel {
190190
* Client-side validation of some common `GenerationConfig` pitfalls, in order
191191
* to save the developer a wasted request.
192192
*/
193-
function validateGenerationConfig(generationConfig: GenerationConfig): void {
193+
export function validateGenerationConfig(
194+
generationConfig: GenerationConfig
195+
): void {
194196
if (
195197
// != allows for null and undefined. 0 is considered "set" by the model
196198
generationConfig.thinkingConfig?.thinkingBudget != null &&
@@ -214,11 +216,12 @@ function validateGenerationConfig(generationConfig: GenerationConfig): void {
214216
if (
215217
(generationConfig.responseSchema != null ||
216218
generationConfig.responseJsonSchema != null) &&
217-
generationConfig.responseMimeType
219+
generationConfig.responseMimeType !== 'application/json'
218220
) {
219221
throw new AIError(
220222
AIErrorCode.UNSUPPORTED,
221-
`responseMimeType must be set if responseSchema or responseJsonSchema are set.`
223+
`responseMimeType must be set to "application/json" if` +
224+
` responseSchema or responseJsonSchema are set.`
222225
);
223226
}
224227
}

0 commit comments

Comments
 (0)