Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/tools/mongodb/mongodbSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ export const zVoyageModels = z
.enum(["voyage-3-large", "voyage-3.5", "voyage-3.5-lite", "voyage-code-3"])
.default("voyage-3-large");

// Zod does not undestand JS boxed numbers (like Int32) as integer literals,
// so we preprocess them to unwrap them so Zod understands them.
function unboxNumber(v: unknown): number {
if (v && typeof v === "object" && typeof v.valueOf === "function") {
const n = Number(v.valueOf());
if (!Number.isNaN(n)) return n;
}
return v as number;
}

export const zVoyageEmbeddingParameters = z.object({
// OpenAPI JSON Schema supports enum only as string so the public facing
// parameters that are fed to LLM providers should expect the dimensions as
// stringified numbers which are then transformed to actual numbers.
outputDimension: z
.preprocess(
unboxNumber,
z.union([z.literal(256), z.literal(512), z.literal(1024), z.literal(2048), z.literal(4096)])
)
.union([z.literal("256"), z.literal("512"), z.literal("1024"), z.literal("2048"), z.literal("4096")])
.transform((value): number => Number.parseInt(value))
.optional()
.default(1024),
.default("1024"),
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value should be applied before the transformation, not after. The .default() should come before .transform() to provide the default string value that will then be transformed to a number. Current order means the default is applied after transformation, which expects a number but receives a string.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

@himanshusinghs himanshusinghs Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gagik do you know if the order of function application is relevant here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using the schema as is on its own and it seemed to work fine.

outputDtype: z.enum(["float", "int8", "uint8", "binary", "ubinary"]).optional().default("float"),
});

export const zVoyageAPIParameters = zVoyageEmbeddingParameters
.extend({
// Unlike public facing parameters, `zVoyageEmbeddingParameters`, the
// api parameters need to be correct number and because we do an
// additional parsing before calling the API, we override the
// outputDimension schema to expect a union of numbers.
outputDimension: z
.union([z.literal(256), z.literal(512), z.literal(1024), z.literal(2048), z.literal(4096)])
.optional()
.default(1024),
inputType: z.enum(["query", "document"]),
})
.strip();
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/tools/mongodb/create/insertMany.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describeWithMongoDB(
// embeddingParameters so that we can simulate the idea
// of unknown or mismatched quantization.

// embeddingParameters: { outputDimension: 256,
// embeddingParameters: { outputDimension: "256",
// outputDtype: "float", model: "voyage-3-large", input:
// [
// {
Expand Down Expand Up @@ -558,7 +558,7 @@ describeWithMongoDB(
documents: [{ title: "The Matrix" }],
embeddingParameters: {
model: "voyage-3.5-lite",
outputDimension: 256,
outputDimension: "256",
input: [{ titleEmbeddings: "The Matrix" }],
},
},
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/tools/mongodb/read/aggregate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
},
},
},
Expand Down Expand Up @@ -508,7 +508,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: dataType,
},
},
Expand Down Expand Up @@ -575,7 +575,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: dataType,
},
},
Expand Down Expand Up @@ -642,7 +642,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: dataType,
},
},
Expand Down Expand Up @@ -709,7 +709,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: dataType,
},
},
Expand Down Expand Up @@ -774,7 +774,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: "float",
},
filter: { name: 10 },
Expand Down Expand Up @@ -837,7 +837,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: "float",
},
filter: { name: 10 },
Expand Down Expand Up @@ -900,7 +900,7 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
limit: 10,
embeddingParameters: {
model: "voyage-3-large",
outputDimension: 256,
outputDimension: "256",
outputDType: "float",
},
filter: { name: 10 },
Expand Down
Loading