Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions plugins/plugin-embeddings/__tests__/embedding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ describe("plugin-embeddings handleBatchTextEmbedding", () => {
/duplicate index 0/i
);
});

it("throws when the response index is not an integer", async () => {
const fetchMock = vi.fn(
async () =>
({
ok: true,
status: 200,
statusText: "OK",
json: async () => ({
object: "list",
data: [
{ object: "embedding", embedding: vectorOf(1536), index: 0.5 },
{ object: "embedding", embedding: vectorOf(1536), index: 1 },
],
model: "text-embedding-3-small",
}),
text: async () => "",
}) as unknown as Response
);
vi.spyOn(globalThis, "fetch").mockImplementation(fetchMock as unknown as typeof fetch);

await expect(handleBatchTextEmbedding(createRuntime(), ["one", "two"])).rejects.toThrow(
/out-of-range index 0.5/i
);
});
});

describe("plugin-embeddings input truncation", () => {
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-embeddings/src/models/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async function requestEmbeddings(
const vectors: number[][] = new Array(expectedCount);
for (const item of data.data) {
const idx = typeof item.index === "number" ? item.index : undefined;
if (idx === undefined || idx < 0 || idx >= expectedCount) {
if (idx === undefined || !Number.isInteger(idx) || idx < 0 || idx >= expectedCount) {
throw new Error(
`Embedding API returned out-of-range index ${String(item.index)} (expected 0..${expectedCount - 1})`
);
Expand Down
Loading