Repository navigation
fix: drop empty-string enum values from Gemini tool schemas #1532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+144
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/opencode/test/altimate/tools/tool-schema-gemini.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test" | ||
| import { Effect } from "effect" | ||
| import { LayerNode } from "@opencode-ai/core/effect/layer-node" | ||
| import { Agent } from "@/agent/agent" | ||
| import { Config } from "@/config/config" | ||
| import { RuntimeFlags } from "@/effect/runtime-flags" | ||
| import type { Provider } from "@/provider/provider" | ||
| import { ModelID, ProviderID } from "@/provider/schema" | ||
| import { ProviderTransform } from "@/provider/transform" | ||
| import { ToolJsonSchema } from "@/tool/json-schema" | ||
| import { ToolRegistry } from "@/tool/registry" | ||
| import { TestConfig } from "../../fixture/config" | ||
| import { disposeAllInstances } from "../../fixture/fixture" | ||
| import { testEffect } from "../../lib/effect" | ||
|
|
||
| const it = testEffect( | ||
| LayerNode.buildLayer(LayerNode.group([ToolRegistry.node, Agent.node]), { | ||
| replacements: [ | ||
| LayerNode.replace(Config.node, TestConfig.layer()), | ||
| LayerNode.replace(RuntimeFlags.node, RuntimeFlags.layer({ enableParallel: false })), | ||
| ], | ||
| }), | ||
| ) | ||
|
|
||
| afterEach(disposeAllInstances) | ||
|
|
||
| function isObject(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === "object" && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| // Gemini rejects a function declaration whose enum (or const) contains an empty string: | ||
| // "function_declarations[i].parameters...enum[0]: cannot be empty". | ||
| function emptyEnumValues(schema: unknown, location = "#"): string[] { | ||
| if (Array.isArray(schema)) return schema.flatMap((child, index) => emptyEnumValues(child, `${location}/${index}`)) | ||
| if (!isObject(schema)) return [] | ||
| const found: string[] = [] | ||
| if (Array.isArray(schema.enum) && schema.enum.some((value) => String(value) === "")) found.push(`${location}/enum`) | ||
| if (schema.const === "") found.push(`${location}/const`) | ||
| for (const [key, child] of Object.entries(schema)) { | ||
| if (key === "enum" || key === "const" || key === "default") continue | ||
| found.push(...emptyEnumValues(child, `${location}/${key}`)) | ||
| } | ||
| return found | ||
| } | ||
|
|
||
| function model(providerID: ProviderID): Provider.Model { | ||
| const id = "gemini-4-argon" | ||
| return { | ||
| id: ModelID.make(id), | ||
| providerID, | ||
| api: { id, npm: providerID === ProviderID.google ? "@ai-sdk/google" : "@ai-sdk/google-vertex", url: "https://example.invalid" }, | ||
| name: id, | ||
| capabilities: { | ||
| temperature: true, | ||
| reasoning: true, | ||
| attachment: false, | ||
| toolcall: true, | ||
| input: { text: true, audio: false, image: false, video: false, pdf: false }, | ||
| output: { text: true, audio: false, image: false, video: false, pdf: false }, | ||
| interleaved: false, | ||
| }, | ||
| cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, | ||
| limit: { context: 1_000_000, output: 64_000 }, | ||
| status: "active", | ||
| release_date: "2026-01-01", | ||
| options: {}, | ||
| headers: {}, | ||
| } | ||
| } | ||
|
|
||
| describe("Gemini tool JSON schemas", () => { | ||
| for (const providerID of [ProviderID.google, ProviderID.googleVertex]) { | ||
| for (const name of ["analyst", "builder"]) { | ||
| it.instance(`${providerID}: no registered ${name} tool declares an empty-string enum or const`, () => | ||
| Effect.gen(function* () { | ||
| const registry = yield* ToolRegistry.Service | ||
| const agents = yield* Agent.Service | ||
| const agent = yield* agents.get(name) | ||
| if (!agent) throw new Error(`${name} agent not found`) | ||
| const target = model(providerID) | ||
| const tools = yield* registry.tools({ providerID, modelID: target.id, agent }) | ||
| expect(tools.some((tool) => tool.id === "answer_commit")).toBe(name === "analyst") | ||
|
anandgupta42 marked this conversation as resolved.
|
||
| const errors = tools.flatMap((tool) => | ||
| emptyEnumValues(ProviderTransform.schema(target, ToolJsonSchema.fromTool(tool))).map( | ||
| (location) => `${tool.id}: ${location}`, | ||
| ), | ||
| ) | ||
| expect(errors).toEqual([]) | ||
| }), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| test("drops empty-string enum and const values but keeps the remaining enum", () => { | ||
| const result = ProviderTransform.schema(model(ProviderID.googleVertex), { | ||
| type: "object", | ||
| properties: { | ||
| unresolved: { anyOf: [{ type: "object", properties: {} }, { type: "string", enum: [""] }] }, | ||
| blank: { type: "string", const: "" }, | ||
| mode: { type: "string", enum: ["", "fast", "slow"] }, | ||
| }, | ||
| } as any) as any | ||
| expect(result.properties.unresolved.anyOf[1]).toEqual({ type: "string" }) | ||
| expect(result.properties.blank).toEqual({ type: "string" }) | ||
| expect(result.properties.mode.enum).toEqual(["fast", "slow"]) | ||
| }) | ||
|
|
||
| test("keeps an untyped node a string when its empty const or enum is dropped", () => { | ||
| const result = ProviderTransform.schema(model(ProviderID.googleVertex), { | ||
| type: "object", | ||
| properties: { | ||
| constOnly: { anyOf: [{ const: "" }, { type: "number" }] }, | ||
| enumOnly: { enum: [""] }, | ||
| nullable: { type: ["string", "null"], const: "" }, | ||
| emptyEnum: { type: "string", enum: [] }, | ||
| }, | ||
| } as any) as any | ||
| expect(result.properties.constOnly.anyOf[0]).toEqual({ type: "string" }) | ||
| expect(result.properties.enumOnly).toEqual({ type: "string" }) | ||
| expect(result.properties.nullable).toEqual({ anyOf: [{ type: "string" }], nullable: true }) | ||
| expect(result.properties.emptyEnum.enum).toEqual([]) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.