Skip to content

Commit e4a56e5

Browse files
anandgupta42claude
andcommitted
fix: keep type: "string" when an untyped empty const/enum is dropped for Gemini
- Dropping an empty-string `const`/`enum` from a node without `type` left `{}`, which accepts any value; set `type: "string"` instead - Leave an empty `enum: []` untouched rather than treating it as all-empty-strings - Add tests for untyped `const`, untyped `enum`, nullable literal, and empty `enum` Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f9dd333 commit e4a56e5

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,11 +1612,20 @@ export namespace ProviderTransform {
16121612
}
16131613

16141614
const result: any = {}
1615+
// altimate_change start — track dropped empty literals so an untyped node stays a string
1616+
let droppedEmptyLiteral = false
1617+
// altimate_change end
16151618
for (const [key, value] of Object.entries(obj)) {
16161619
// altimate_change start — Gemini rejects empty-string enum/const values ("cannot be empty");
16171620
// drop them and keep the branch's type. Tool arguments are still validated by Zod at runtime.
1618-
if (key === "const" && value === "") continue
1619-
if (key === "enum" && Array.isArray(value) && value.every((v) => String(v) === "")) continue
1621+
if (key === "const" && value === "") {
1622+
droppedEmptyLiteral = true
1623+
continue
1624+
}
1625+
if (key === "enum" && Array.isArray(value) && value.length > 0 && value.every((v) => String(v) === "")) {
1626+
droppedEmptyLiteral = true
1627+
continue
1628+
}
16201629
// altimate_change end
16211630
if (key === "enum" && Array.isArray(value)) {
16221631
// Convert all enum values to strings
@@ -1634,6 +1643,10 @@ export namespace ProviderTransform {
16341643
}
16351644
}
16361645

1646+
// altimate_change start — an untyped node that lost its empty literal would become `{}` (any value)
1647+
if (droppedEmptyLiteral && result.type === undefined) result.type = "string"
1648+
// altimate_change end
1649+
16371650
// altimate_change start — Gemini requires a single type; split JSON Schema type arrays
16381651
if (Array.isArray(result.type)) {
16391652
const hasNull = result.type.includes("null")

‎packages/opencode/test/altimate/tools/tool-schema-gemini.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,20 @@ describe("Gemini tool JSON schemas", () => {
104104
expect(result.properties.blank).toEqual({ type: "string" })
105105
expect(result.properties.mode.enum).toEqual(["fast", "slow"])
106106
})
107+
108+
test("keeps an untyped node a string when its empty const or enum is dropped", () => {
109+
const result = ProviderTransform.schema(model(ProviderID.googleVertex), {
110+
type: "object",
111+
properties: {
112+
constOnly: { anyOf: [{ const: "" }, { type: "number" }] },
113+
enumOnly: { enum: [""] },
114+
nullable: { type: ["string", "null"], const: "" },
115+
emptyEnum: { type: "string", enum: [] },
116+
},
117+
} as any) as any
118+
expect(result.properties.constOnly.anyOf[0]).toEqual({ type: "string" })
119+
expect(result.properties.enumOnly).toEqual({ type: "string" })
120+
expect(result.properties.nullable).toEqual({ anyOf: [{ type: "string" }], nullable: true })
121+
expect(result.properties.emptyEnum.enum).toEqual([])
122+
})
107123
})

0 commit comments

Comments
 (0)