Skip to content

Commit 18cfe2e

Browse files
author
Peter Steinberger
committed
fix(memory): reject malformed direct and batch embedding vectors
1 parent 865a728 commit 18cfe2e

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎packages/memory-host-sdk/src/host/batch-output.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,39 @@ describe("applyEmbeddingBatchOutputLine", () => {
173173
expect(byCustomId.get("req-1")).toEqual([0.1, 0.2]);
174174
});
175175

176+
it.each([
177+
{ name: "non-array", embedding: "poison" },
178+
{ name: "array-like object", embedding: { length: 1 } },
179+
{ name: "boolean", embedding: false },
180+
{ name: "string coordinate", embedding: ["poison"] },
181+
{ name: "null coordinate", embedding: [null] },
182+
{ name: "NaN coordinate", embedding: [Number.NaN] },
183+
{ name: "positive infinity coordinate", embedding: [Number.POSITIVE_INFINITY] },
184+
{ name: "negative infinity coordinate", embedding: [Number.NEGATIVE_INFINITY] },
185+
])("rejects a $name instead of storing it as a valid vector", ({ embedding }) => {
186+
const remaining = new Set(["req-invalid"]);
187+
const errors: string[] = [];
188+
const byCustomId = new Map<string, number[]>();
189+
190+
applyEmbeddingBatchOutputLine({
191+
line: {
192+
custom_id: "req-invalid",
193+
response: {
194+
status_code: 200,
195+
body: { data: [{ embedding: embedding as unknown as number[] }] },
196+
},
197+
},
198+
remaining,
199+
errors,
200+
byCustomId,
201+
});
202+
203+
expect(remaining.has("req-invalid")).toBe(false);
204+
expect(errors).toHaveLength(1);
205+
expect(errors[0]).toMatch(/^req-invalid: (?:empty|invalid) embedding$/);
206+
expect(byCustomId.size).toBe(0);
207+
});
208+
176209
it("records provider error from line.error", () => {
177210
const remaining = new Set(["req-2"]);
178211
const errors: string[] = [];

‎packages/memory-host-sdk/src/host/batch-output.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ export function applyEmbeddingBatchOutputLine(params: {
174174
const data =
175175
response?.body && typeof response.body === "object" ? (response.body.data ?? []) : [];
176176
const embedding = data[0]?.embedding ?? [];
177-
if (embedding.length === 0) {
178-
params.errors.push(`${customId}: empty embedding`);
177+
if (!Array.isArray(embedding) || embedding.length === 0 || !embedding.every(Number.isFinite)) {
178+
params.errors.push(`${customId}: ${embedding?.length ? "invalid" : "empty"} embedding`);
179179
return;
180180
}
181181
params.byCustomId.set(customId, embedding);

‎packages/memory-host-sdk/src/host/embeddings-remote-fetch.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,39 @@ describe("fetchRemoteEmbeddingVectors", () => {
124124
).rejects.toThrow("embedding fetch failed: malformed JSON response");
125125
});
126126

127+
it.each([
128+
{ name: "query", input: ["query"], data: [{ embedding: [] }] },
129+
{
130+
name: "document batch",
131+
input: ["first", "second"],
132+
data: [{ embedding: [0.1] }, { embedding: [] }],
133+
},
134+
])("rejects empty vectors in a $name response", async ({ input, data }) => {
135+
postJsonMock.mockImplementationOnce(async (params) => await params.parse({ data }));
136+
137+
await expect(
138+
fetchRemoteEmbeddingVectors({
139+
url: "https://memory.example/v1/embeddings",
140+
headers: {},
141+
body: { input },
142+
errorPrefix: "embedding fetch failed",
143+
}),
144+
).rejects.toThrow("embedding fetch failed: malformed JSON response");
145+
});
146+
147+
it("preserves an empty response for an empty submitted input batch", async () => {
148+
postJsonMock.mockImplementationOnce(async (params) => await params.parse({ data: [] }));
149+
150+
await expect(
151+
fetchRemoteEmbeddingVectors({
152+
url: "https://memory.example/v1/embeddings",
153+
headers: {},
154+
body: { input: [] },
155+
errorPrefix: "embedding fetch failed",
156+
}),
157+
).resolves.toEqual([]);
158+
});
159+
127160
it("rejects wrong nested embedding vector types", async () => {
128161
postJsonMock.mockImplementationOnce(async (params) => {
129162
return await params.parse({ data: [{ embedding: [0.1, "bad"] }] });

‎packages/memory-host-sdk/src/host/embeddings-remote-fetch.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function malformedEmbeddingResponse(errorPrefix: string): Error {
1818

1919
/** Validate and return one finite embedding vector. */
2020
function readEmbeddingVector(value: unknown, errorPrefix: string): number[] {
21-
if (!Array.isArray(value)) {
21+
if (!Array.isArray(value) || value.length === 0) {
2222
throw malformedEmbeddingResponse(errorPrefix);
2323
}
2424
for (const entry of value) {

0 commit comments

Comments
 (0)