Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/ai/src/protocols/utils/gemini-tool-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ const projectNode = (schema: unknown, nested = false): Record<string, unknown> |
["description", schema.description],
["required", schema.required],
["format", schema.format],
["type", types ? (types.length === 0 ? "null" : undefined) : schema.type],
// A single non-null type stays a plain `type` so an optional object keeps carrying its
// `properties` and `required`; Gemini reads those only on an OBJECT-typed node and rejects
// the whole request when the type is expressed as a combiner instead.
["type", types ? (types.length === 0 ? "null" : types.length === 1 ? types[0] : undefined) : schema.type],
[
"nullable",
(Array.isArray(schema.type) && schema.type.includes("null") && types && types.length > 0) || hasNullAnyOf
Expand Down Expand Up @@ -103,7 +106,7 @@ const projectNode = (schema: unknown, nested = false): Record<string, unknown> |
? hasNullAnyOf && anyOfTypes.length === 1
? undefined
: anyOfTypes.map((item) => projectNode(item, true))
: types && types.length > 0
: types && types.length > 1
? types.map((type) => ({ type }))
: undefined,
],
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/test/provider/gemini.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ describe("Gemini route", () => {
anyOf: [{ type: "number" }, { type: "string" }],
},
maybe: {
type: "string",
nullable: true,
anyOf: [{ type: "string" }],
},
nothing: {
type: "null",
Expand Down
30 changes: 30 additions & 0 deletions packages/ai/test/tool-schema-projection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ describe("tool schema projections", () => {
})
})

test("gemini keeps an optional object typed so its properties survive", () => {
// Shape an MCP server emits for an optional object parameter.
expect(
ToolSchemaProjection.gemini({
type: "object",
properties: {
cursor_theme: {
type: ["object", "null"],
properties: { theme_id: { type: "string" } },
required: ["theme_id"],
},
note: { type: ["string", "null"] },
either: { type: ["string", "number"] },
},
}),
).toEqual({
type: "object",
properties: {
cursor_theme: {
type: "object",
nullable: true,
required: ["theme_id"],
properties: { theme_id: { type: "string" } },
},
note: { type: "string", nullable: true },
either: { anyOf: [{ type: "string" }, { type: "number" }] },
},
})
})

test("openai keeps one flat object top-level schema", () => {
expect(
ToolSchemaProjection.openAI({
Expand Down
Loading