fix: sanitize Gemini tool schema required fields (#64284) (thanks @xxxxxmax)
This commit is contained in:
@@ -68,12 +68,21 @@ describe("cleanSchemaForGemini", () => {
|
||||
expect(cleaned.required).toBeUndefined();
|
||||
});
|
||||
|
||||
it("leaves required as-is when properties is absent", () => {
|
||||
it("removes required from object schemas when properties is absent", () => {
|
||||
const cleaned = cleanSchemaForGemini({
|
||||
type: "object",
|
||||
required: ["a", "b"],
|
||||
}) as { required?: string[] };
|
||||
|
||||
expect(cleaned.required).toBeUndefined();
|
||||
});
|
||||
|
||||
it("leaves required as-is for non-object schemas when properties is absent", () => {
|
||||
const cleaned = cleanSchemaForGemini({
|
||||
type: "array",
|
||||
required: ["a", "b"],
|
||||
}) as { required?: string[] };
|
||||
|
||||
expect(cleaned.required).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
|
||||
@@ -214,12 +214,18 @@ function simplifyUnionVariants(params: { obj: Record<string, unknown>; variants:
|
||||
|
||||
// Gemini rejects object schemas whose `required` entries do not exist in `properties`.
|
||||
function sanitizeRequiredFields(schema: Record<string, unknown>): Record<string, unknown> {
|
||||
if (!Array.isArray(schema.required)) {
|
||||
return schema;
|
||||
}
|
||||
|
||||
if (
|
||||
!Array.isArray(schema.required) ||
|
||||
!schema.properties ||
|
||||
typeof schema.properties !== "object" ||
|
||||
Array.isArray(schema.properties)
|
||||
) {
|
||||
if (schema.type === "object") {
|
||||
delete schema.required;
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user