Skip to content

Commit

Permalink
google-genai[patch]: Fix removing additional properties from schema (#…
Browse files Browse the repository at this point in the history
…6109)

* google-genai[patch]: Fix removing additional properties from schema

* chore: lint files
  • Loading branch information
bracesproul authored Jul 17, 2024
1 parent 1c4b2d8 commit 8050a2a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
46 changes: 33 additions & 13 deletions libs/langchain-google-genai/src/tests/chat_models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import {
convertMessageContentToParts,
} from "../utils/common.js";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function extractKeys(obj: Record<string, any>, keys: string[] = []) {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
keys.push(key);
if (typeof obj[key] === "object" && obj[key] !== null) {
extractKeys(obj[key], keys);
}
}
}
return keys;
}

test("Google AI - `temperature` must be in range [0.0,1.0]", async () => {
expect(
() =>
Expand Down Expand Up @@ -89,19 +102,6 @@ test("Google AI - `safetySettings` category array must be unique", async () => {
});

test("removeAdditionalProperties can remove all instances of additionalProperties", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function extractKeys(obj: Record<string, any>, keys: string[] = []) {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
keys.push(key);
if (typeof obj[key] === "object" && obj[key] !== null) {
extractKeys(obj[key], keys);
}
}
}
return keys;
}

const idealResponseSchema = z.object({
idealResponse: z
.string()
Expand Down Expand Up @@ -137,6 +137,26 @@ test("removeAdditionalProperties can remove all instances of additionalPropertie
expect(
arrSchemaObj.find((key) => key === "additionalProperties")
).toBeUndefined();

const analysisSchema = z.object({
decision: z.enum(["UseAPI", "UseFallback"]),
explanation: z.string(),
apiDetails: z
.object({
serviceName: z.string(),
endpointName: z.string(),
parameters: z.record(z.unknown()),
extractionPath: z.string(),
})
.optional(),
});
const parsedAnalysisSchema = removeAdditionalProperties(
zodToJsonSchema(analysisSchema)
);
const analysisSchemaObj = extractKeys(parsedAnalysisSchema);
expect(
analysisSchemaObj.find((key) => key === "additionalProperties")
).toBeUndefined();
});

test("convertMessageContentToParts correctly handles message types", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ export function removeAdditionalProperties(
if (typeof obj === "object" && obj !== null) {
const newObj = { ...obj };

if (
"additionalProperties" in newObj &&
typeof newObj.additionalProperties === "boolean"
) {
if ("additionalProperties" in newObj) {
delete newObj.additionalProperties;
}

Expand Down

0 comments on commit 8050a2a

Please sign in to comment.