Skip to content

Commit 3b2cdb2

Browse files
authored
google-common[patch]: Fix remove additional properties util (#5862)
* google-common[patch]: Fix remove additional properties util * chore: lint files
1 parent d17e18e commit 3b2cdb2

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts

+24-29
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,34 @@ import {
88
GeminiJsonSchemaDirty,
99
} from "../types.js";
1010

11-
function removeAdditionalProperties(
12-
schema: GeminiJsonSchemaDirty
11+
export function removeAdditionalProperties(
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13+
obj: Record<string, any>
1314
): GeminiJsonSchema {
14-
const updatedSchema: GeminiJsonSchemaDirty = { ...schema };
15-
if (Object.hasOwn(updatedSchema, "additionalProperties")) {
16-
delete updatedSchema.additionalProperties;
17-
}
18-
if (updatedSchema.properties) {
19-
const keys = Object.keys(updatedSchema.properties);
20-
removeProperties(updatedSchema.properties, keys, 0);
21-
}
22-
if (Object.hasOwn(updatedSchema, "items") && updatedSchema.items) {
23-
updatedSchema.items = removeAdditionalProperties(updatedSchema.items);
24-
}
15+
if (typeof obj === "object" && obj !== null) {
16+
const newObj = { ...obj };
2517

26-
return updatedSchema;
27-
}
18+
if (
19+
"additionalProperties" in newObj &&
20+
typeof newObj.additionalProperties === "boolean"
21+
) {
22+
delete newObj.additionalProperties;
23+
}
24+
25+
for (const key in newObj) {
26+
if (key in newObj) {
27+
if (Array.isArray(newObj[key])) {
28+
newObj[key] = newObj[key].map(removeAdditionalProperties);
29+
} else if (typeof newObj[key] === "object" && newObj[key] !== null) {
30+
newObj[key] = removeAdditionalProperties(newObj[key]);
31+
}
32+
}
33+
}
2834

29-
function removeProperties(
30-
properties: Record<string, GeminiJsonSchemaDirty>,
31-
keys: string[],
32-
index: number
33-
): void {
34-
if (index >= keys.length) {
35-
return;
35+
return newObj as GeminiJsonSchema;
3636
}
3737

38-
const key = keys[index];
39-
// eslint-disable-next-line no-param-reassign
40-
properties[key] = removeAdditionalProperties(properties[key]);
41-
removeProperties(properties, keys, index + 1);
38+
return obj as GeminiJsonSchema;
4239
}
4340

4441
export function zodToGeminiParameters(
@@ -48,9 +45,7 @@ export function zodToGeminiParameters(
4845
// Gemini doesn't accept either the $schema or additionalProperties
4946
// attributes, so we need to explicitly remove them.
5047
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51-
const jsonSchema = removeAdditionalProperties(
52-
zodToJsonSchema(zodObj) as GeminiJsonSchemaDirty
53-
);
48+
const jsonSchema = removeAdditionalProperties(zodToJsonSchema(zodObj));
5449
const { $schema, ...rest } = jsonSchema;
5550

5651
return rest;

0 commit comments

Comments
 (0)