mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 02:00:21 +00:00
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:
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user