{"record":{"id":"35f1c26b3aa0ead9","repo":"mem0ai/mem0","slug":"openai-embedbatch-returned-allembeddings-lengt","errorCode":null,"errorMessage":"OpenAI embedBatch() returned ${allEmbeddings.length} embeddings for ${texts.length} texts using model '${this.model}'","messagePattern":"OpenAI embedBatch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/embeddings/openai.ts","lineNumber":51,"sourceCode":"    const allEmbeddings: number[][] = [];\n    for (let i = 0; i < texts.length; i += MAX_BATCH) {\n      const chunk = texts.slice(i, i + MAX_BATCH);\n      const response = await this.openai.embeddings.create({\n        model: this.model,\n        input: chunk,\n        encoding_format: \"float\",\n        ...(this.embeddingDims !== undefined && {\n          dimensions: this.embeddingDims,\n        }),\n      });\n      allEmbeddings.push(\n        ...response.data\n          .sort((a, b) => a.index - b.index)\n          .map((item) => item.embedding),\n      );\n    }\n    if (allEmbeddings.length !== texts.length) {\n      throw new Error(\n        `OpenAI embedBatch() returned ${allEmbeddings.length} embeddings for ${texts.length} texts using model '${this.model}'`,\n      );\n    }\n    return allEmbeddings;\n  }\n}\n","sourceCodeStart":33,"sourceCodeEnd":58,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/embeddings/openai.ts#L33-L58","documentation":"OpenAIEmbedder.embedBatch() chunks texts, collects all embeddings, and verifies the total equals the number of input texts. A count mismatch means the API honored the request but returned a different number of embedding records than inputs, which would silently corrupt vector-to-memory association if accepted, so it throws.","triggerScenarios":"A proxy/gateway between the SDK and the OpenAI API that drops or duplicates items; a model or server bug returning partial data; passing duplicate or empty-string inputs through a relay that de-duplicates. Essentially never happens against the real OpenAI API with a healthy network path.","commonSituations":"Corporate LLM gateway or LiteLLM-style proxy mangling batch responses; locally hosted OpenAI-compatible servers (vLLM, older LM Studio) with incomplete batch support; interleaved retries at the HTTP layer.","solutions":["Retry the operation: a mismatch is almost always a transient proxy/gateway issue","If behind a gateway, bypass it and call api.openai.com directly to confirm where items are lost","Update the gateway/self-hosted server to a version with correct batch embeddings support","Report a bug with the model name, batch size, and gateway in the path if it reproduces against the real API"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"function isBatchCountMismatch(err: unknown): boolean {\n  return err instanceof Error && /embedBatch\\(\\) returned \\d+ embeddings for \\d+ texts/.test(err.message);\n}","tryCatchPattern":"async function embedWithRetry(texts: string[], tries = 2) {\n  for (let i = 0; ; i++) {\n    try { return await embedder.embedBatch(texts); }\n    catch (err) {\n      if (i < tries && err instanceof Error && err.message.includes(\"embedBatch() returned\")) continue;\n      throw err;\n    }\n  }\n}","preventionTips":["Treat count mismatches as transient; retry before escalating","Keep batches modest in size when routing through gateways","Log model, batch size, and returned count when it fires to identify the faulty hop"],"tags":["openai","embeddings","batch","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}