@@ -226,48 +226,34 @@ describe("cleanSchemaForGemini", () => {
226226 expect ( cleaned . properties ?. agentId ?. type ) . toBe ( "string" ) ;
227227 } ) ;
228228
229- // Gemini Function Calling requires string enum values. Numeric/boolean enums
230- // (common in tool schemas — priorities, scores, flags) must be coerced to
231- // strings and the schema retyped as `"string"`, otherwise Gemini rejects the
232- // request with `TYPE_STRING expected` errors.
233- it ( "coerces numeric enum values to strings and retypes as string" , ( ) => {
234- const cleaned = cleanSchemaForGemini ( {
235- type : "integer" ,
236- enum : [ 1 , 2 , 3 ] ,
237- } ) as { type ?: unknown ; enum ?: unknown } ;
238-
239- expect ( cleaned . type ) . toBe ( "string" ) ;
240- expect ( cleaned . enum ) . toStrictEqual ( [ "1" , "2" , "3" ] ) ;
241- } ) ;
242-
243- it ( "coerces boolean enum values to strings" , ( ) => {
244- const cleaned = cleanSchemaForGemini ( {
245- type : "boolean" ,
246- enum : [ true , false ] ,
247- } ) as { type ?: unknown ; enum ?: unknown } ;
248-
249- expect ( cleaned . type ) . toBe ( "string" ) ;
250- expect ( cleaned . enum ) . toStrictEqual ( [ "true" , "false" ] ) ;
251- } ) ;
252-
253- it ( "preserves already-string enum values without retyping" , ( ) => {
254- const cleaned = cleanSchemaForGemini ( {
255- type : "string" ,
256- enum : [ "a" , "b" , "c" ] ,
257- } ) as { type ?: unknown ; enum ?: unknown } ;
258-
259- expect ( cleaned . type ) . toBe ( "string" ) ;
260- expect ( cleaned . enum ) . toStrictEqual ( [ "a" , "b" , "c" ] ) ;
261- } ) ;
262-
263- it ( "coerces a numeric const to a string enum and retypes as string" , ( ) => {
264- const cleaned = cleanSchemaForGemini ( {
265- type : "integer" ,
266- const : 42 ,
267- } ) as { type ?: unknown ; enum ?: unknown } ;
268-
269- expect ( cleaned . type ) . toBe ( "string" ) ;
270- expect ( cleaned . enum ) . toStrictEqual ( [ "42" ] ) ;
229+ it . each ( [
230+ {
231+ name : "integer enum" ,
232+ schema : { type : "integer" , enum : [ 1 , 2 , 3 ] } ,
233+ expected : { type : "integer" , enum : [ "1" , "2" , "3" ] } ,
234+ } ,
235+ {
236+ name : "integer enum before type" ,
237+ schema : { enum : [ 1 , 2 , 3 ] , type : "integer" } ,
238+ expected : { enum : [ "1" , "2" , "3" ] , type : "integer" } ,
239+ } ,
240+ {
241+ name : "boolean enum" ,
242+ schema : { type : "boolean" , enum : [ true , false ] } ,
243+ expected : { type : "boolean" , enum : [ "true" , "false" ] } ,
244+ } ,
245+ {
246+ name : "string enum" ,
247+ schema : { type : "string" , enum : [ "a" , "b" , "c" ] } ,
248+ expected : { type : "string" , enum : [ "a" , "b" , "c" ] } ,
249+ } ,
250+ {
251+ name : "integer const before type" ,
252+ schema : { const : 42 , type : "integer" } ,
253+ expected : { enum : [ "42" ] , type : "integer" } ,
254+ } ,
255+ ] ) ( "stringifies $name values without changing the schema type" , ( { schema, expected } ) => {
256+ expect ( cleanSchemaForGemini ( schema ) ) . toStrictEqual ( expected ) ;
271257 } ) ;
272258
273259 it ( "drops null/undefined enum entries and de-duplicates" , ( ) => {
@@ -279,7 +265,7 @@ describe("cleanSchemaForGemini", () => {
279265 expect ( cleaned . enum ) . toStrictEqual ( [ "1" , "2" , "3" ] ) ;
280266 } ) ;
281267
282- it ( "coerces numeric enums inside deeply nested tool parameters " , ( ) => {
268+ it ( "stringifies nested numeric enums while preserving their number type " , ( ) => {
283269 const cleaned = cleanSchemaForGemini ( {
284270 type : "object" ,
285271 properties : {
@@ -300,7 +286,7 @@ describe("cleanSchemaForGemini", () => {
300286 } ;
301287
302288 const score = cleaned . properties ?. outer ?. items ?. properties ?. score ;
303- expect ( score ?. type ) . toBe ( "string " ) ;
289+ expect ( score ?. type ) . toBe ( "number " ) ;
304290 expect ( score ?. enum ) . toStrictEqual ( [ "1" , "2" , "3" , "4" , "5" ] ) ;
305291 } ) ;
306292
@@ -312,48 +298,4 @@ describe("cleanSchemaForGemini", () => {
312298
313299 expect ( cleaned . enum ) . toBeUndefined ( ) ;
314300 } ) ;
315-
316- // Regression: schema key order is not semantic. `{ enum: [...], type: "..." }`
317- // must produce the same output as `{ type: "...", enum: [...] }`. If the
318- // coercion is applied only while visiting `enum`, a later `type` key would
319- // overwrite `type: "string"` and Gemini would still reject the declaration.
320- it ( "coerces numeric enum with enum-before-type ordering (regression: property order independence)" , ( ) => {
321- const cleaned = cleanSchemaForGemini ( {
322- enum : [ 1 , 2 , 3 ] ,
323- type : "integer" ,
324- } ) as { type ?: unknown ; enum ?: unknown } ;
325-
326- expect ( cleaned . type ) . toBe ( "string" ) ;
327- expect ( cleaned . enum ) . toStrictEqual ( [ "1" , "2" , "3" ] ) ;
328- } ) ;
329-
330- it ( "coerces boolean enum with enum-before-type ordering" , ( ) => {
331- const cleaned = cleanSchemaForGemini ( {
332- enum : [ true , false ] ,
333- type : "boolean" ,
334- } ) as { type ?: unknown ; enum ?: unknown } ;
335-
336- expect ( cleaned . type ) . toBe ( "string" ) ;
337- expect ( cleaned . enum ) . toStrictEqual ( [ "true" , "false" ] ) ;
338- } ) ;
339-
340- it ( "coerces numeric const with const-before-type ordering" , ( ) => {
341- const cleaned = cleanSchemaForGemini ( {
342- const : 42 ,
343- type : "integer" ,
344- } ) as { type ?: unknown ; enum ?: unknown } ;
345-
346- expect ( cleaned . type ) . toBe ( "string" ) ;
347- expect ( cleaned . enum ) . toStrictEqual ( [ "42" ] ) ;
348- } ) ;
349-
350- it ( "leaves type untouched when no enum coercion happened" , ( ) => {
351- // A pure integer schema without enum/const must NOT be retyped as string.
352- const cleaned = cleanSchemaForGemini ( {
353- type : "integer" ,
354- description : "a plain integer field" ,
355- } ) as { type ?: unknown } ;
356-
357- expect ( cleaned . type ) . toBe ( "integer" ) ;
358- } ) ;
359301} ) ;
0 commit comments