fix(google): strip empty required arrays from tool schemas for Gemini (#52106)

Merged via squash.

Prepared head SHA: 2ec59c1332
Co-authored-by: oliviareid-svg <269669958+oliviareid-svg@users.noreply.github.com>
Co-authored-by: altaywtf <9790196+altaywtf@users.noreply.github.com>
Reviewed-by: @altaywtf
This commit is contained in:
oliviareid-svg
2026-03-27 19:00:14 +08:00
committed by GitHub
parent 8fa62985b9
commit 32a3733dbe
3 changed files with 50 additions and 0 deletions

View File

@@ -52,4 +52,48 @@ describe("cleanSchemaForGemini", () => {
expect(cleaned.properties?.bad?.properties).toEqual({});
expect(cleaned.properties?.good?.type).toBe("string");
});
it("strips empty required arrays", () => {
const cleaned = cleanSchemaForGemini({
type: "object",
properties: {
name: { type: "string" },
},
required: [],
}) as Record<string, unknown>;
expect(cleaned).not.toHaveProperty("required");
expect(cleaned.type).toBe("object");
});
it("preserves non-empty required arrays", () => {
const cleaned = cleanSchemaForGemini({
type: "object",
properties: {
name: { type: "string" },
},
required: ["name"],
}) as Record<string, unknown>;
expect(cleaned.required).toEqual(["name"]);
});
it("strips empty required arrays in nested schemas", () => {
const cleaned = cleanSchemaForGemini({
type: "object",
properties: {
nested: {
type: "object",
properties: {
optional: { type: "string" },
},
required: [],
},
},
required: ["nested"],
}) as { properties?: { nested?: Record<string, unknown> }; required?: string[] };
expect(cleaned.required).toEqual(["nested"]);
expect(cleaned.properties?.nested).not.toHaveProperty("required");
});
});

View File

@@ -291,6 +291,11 @@ function cleanSchemaForGeminiWithDefs(
continue;
}
// Google's schema validator rejects `"required": []` — omit empty arrays.
if (key === "required" && Array.isArray(value) && value.length === 0) {
continue;
}
if (key === "type" && (hasAnyOf || hasOneOf)) {
continue;
}