Skip to content

Commit 97dff40

Browse files
Yansuclaude
andcommitted
fix(provider): fix property order bug in Gemini numeric enum stringification
The sanitizeGemini() function checked result.type when processing enum values, but result.type may not be set yet if the "enum" key appears before the "type" key during object iteration. This caused numeric enum values to be stringified but the type to remain as "integer"/"number" instead of being changed to "string", which Gemini API rejects. Move the type conversion to after all properties are iterated so it works regardless of property order. Fixes anomalyco#12784 Co-Authored-By: Claude <[email protected]>
1 parent 05355a6 commit 97dff40

2 files changed

Lines changed: 149 additions & 4 deletions

File tree

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,17 +783,19 @@ export namespace ProviderTransform {
783783
if (key === "enum" && Array.isArray(value)) {
784784
// Convert all enum values to strings
785785
result[key] = value.map((v) => String(v))
786-
// If we have integer type with enum, change type to string
787-
if (result.type === "integer" || result.type === "number") {
788-
result.type = "string"
789-
}
790786
} else if (typeof value === "object" && value !== null) {
791787
result[key] = sanitizeGemini(value)
792788
} else {
793789
result[key] = value
794790
}
795791
}
796792

793+
// If we have integer/number type with enum, change type to string
794+
// (done after iteration so property order doesn't matter)
795+
if (Array.isArray(result.enum) && (result.type === "integer" || result.type === "number")) {
796+
result.type = "string"
797+
}
798+
797799
// Filter required array to only include fields that exist in properties
798800
if (result.type === "object" && result.properties && Array.isArray(result.required)) {
799801
result.required = result.required.filter((field: any) => field in result.properties)

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,149 @@ describe("ProviderTransform.maxOutputTokens", () => {
269269
})
270270
})
271271

272+
describe("ProviderTransform.schema - gemini numeric enum stringification", () => {
273+
const geminiModel = {
274+
providerID: "google",
275+
api: {
276+
id: "gemini-3-pro",
277+
},
278+
} as any
279+
280+
test("converts numeric enum values to strings", () => {
281+
const schema = {
282+
type: "object",
283+
properties: {
284+
level: {
285+
type: "integer",
286+
enum: [0, 1, 2, 3, 4],
287+
},
288+
},
289+
} as any
290+
291+
const result = ProviderTransform.schema(geminiModel, schema) as any
292+
293+
expect(result.properties.level.enum).toEqual(["0", "1", "2", "3", "4"])
294+
expect(result.properties.level.type).toBe("string")
295+
})
296+
297+
test("converts numeric enum values regardless of property order", () => {
298+
// Simulate a schema where "enum" appears before "type" in iteration order
299+
const prop: any = {}
300+
prop.enum = [1, 2, 3]
301+
prop.type = "integer"
302+
303+
const schema = {
304+
type: "object",
305+
properties: {
306+
priority: prop,
307+
},
308+
} as any
309+
310+
const result = ProviderTransform.schema(geminiModel, schema) as any
311+
312+
expect(result.properties.priority.enum).toEqual(["1", "2", "3"])
313+
expect(result.properties.priority.type).toBe("string")
314+
})
315+
316+
test("changes number type to string for enums", () => {
317+
const schema = {
318+
type: "object",
319+
properties: {
320+
score: {
321+
type: "number",
322+
enum: [0.5, 1.0, 1.5],
323+
},
324+
},
325+
} as any
326+
327+
const result = ProviderTransform.schema(geminiModel, schema) as any
328+
329+
expect(result.properties.score.enum).toEqual(["0.5", "1", "1.5"])
330+
expect(result.properties.score.type).toBe("string")
331+
})
332+
333+
test("preserves string enum values unchanged", () => {
334+
const schema = {
335+
type: "object",
336+
properties: {
337+
color: {
338+
type: "string",
339+
enum: ["red", "green", "blue"],
340+
},
341+
},
342+
} as any
343+
344+
const result = ProviderTransform.schema(geminiModel, schema) as any
345+
346+
expect(result.properties.color.enum).toEqual(["red", "green", "blue"])
347+
expect(result.properties.color.type).toBe("string")
348+
})
349+
350+
test("handles mixed enum values (numbers and strings)", () => {
351+
const schema = {
352+
type: "object",
353+
properties: {
354+
value: {
355+
type: "integer",
356+
enum: [0, "auto", 1, "none"],
357+
},
358+
},
359+
} as any
360+
361+
const result = ProviderTransform.schema(geminiModel, schema) as any
362+
363+
expect(result.properties.value.enum).toEqual(["0", "auto", "1", "none"])
364+
expect(result.properties.value.type).toBe("string")
365+
})
366+
367+
test("converts nested numeric enums", () => {
368+
const schema = {
369+
type: "object",
370+
properties: {
371+
config: {
372+
type: "object",
373+
properties: {
374+
mode: {
375+
type: "integer",
376+
enum: [1, 2, 3],
377+
},
378+
},
379+
},
380+
},
381+
} as any
382+
383+
const result = ProviderTransform.schema(geminiModel, schema) as any
384+
385+
expect(result.properties.config.properties.mode.enum).toEqual(["1", "2", "3"])
386+
expect(result.properties.config.properties.mode.type).toBe("string")
387+
})
388+
389+
test("does not affect non-gemini providers", () => {
390+
const openaiModel = {
391+
providerID: "openai",
392+
api: {
393+
id: "gpt-4",
394+
},
395+
} as any
396+
397+
const schema = {
398+
type: "object",
399+
properties: {
400+
level: {
401+
type: "integer",
402+
enum: [0, 1, 2],
403+
},
404+
},
405+
} as any
406+
407+
const result = ProviderTransform.schema(openaiModel, schema) as any
408+
409+
// OpenAI should keep numeric enums as-is
410+
expect(result.properties.level.enum).toEqual([0, 1, 2])
411+
expect(result.properties.level.type).toBe("integer")
412+
})
413+
})
414+
272415
describe("ProviderTransform.schema - gemini array items", () => {
273416
test("adds missing items for array properties", () => {
274417
const geminiModel = {

0 commit comments

Comments
 (0)