{"record":{"id":"4bbf5034bee28ef1","repo":"mem0ai/mem0","slug":"azure-openai-embedbatch-returned-allembeddings","errorCode":null,"errorMessage":"Azure OpenAI embedBatch() returned ${allEmbeddings.length} embeddings for ${texts.length} texts using model '${this.model}'","messagePattern":"Azure OpenAI embedBatch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/embeddings/azure.ts","lineNumber":56,"sourceCode":"    const MAX_BATCH = 100;\n    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.client.embeddings.create({\n        model: this.model,\n        input: chunk,\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        `Azure OpenAI embedBatch() returned ${allEmbeddings.length} embeddings for ${texts.length} texts using model '${this.model}'`,\n      );\n    }\n    return allEmbeddings;\n  }\n}\n","sourceCodeStart":38,"sourceCodeEnd":63,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/embeddings/azure.ts#L38-L63","documentation":"Thrown by the AzureOpenAIEmbedder when the total number of embeddings returned across all batched requests does not equal the number of input texts. The embedder chunks inputs, sorts each response by index, and accumulates; a mismatch means the API dropped, duplicated, or mis-indexed results — returning mismatched vectors would corrupt memory storage, so it aborts. The message includes both counts and the model name for diagnosis.","triggerScenarios":"Large embedBatch call where one chunked request fails partially or the service returns fewer data items; duplicated index values after sort shifting alignment; using a batch size near the API limit where inputs get merged/split unexpectedly (e.g. very long strings counted as multiple tokens).","commonSituations":"Batch embedding entire chat histories or document chunks in one call; occasional transient inconsistency under load; inputs containing empty strings that some deployments skip.","solutions":["Retry with a smaller batch: split texts into chunks of ~100-500 and call embedBatch per chunk, so a mismatch is isolated","Log the counts from the message — if returned > sent, dedupe/inspect index values; if returned < sent, look for empty or oversized inputs","Sanitize inputs (trim, drop empties, cap length) before embedding","Add a retry with backoff for transient responses — mismatch on one request is often transient"],"exampleFix":"// before\nconst vectors = await embedder.embedBatch(allTexts); // 10k texts in one call\n\n// after\nconst vectors: number[][] = [];\nfor (let i = 0; i < allTexts.length; i += 256) {\n  vectors.push(...(await embedder.embedBatch(allTexts.slice(i, i + 256))));\n}","handlingStrategy":"retry","validationCode":"const CHUNK = 256; // safely under Azure per-request limits\nfor (let i = 0; i < texts.length; i += CHUNK) {\n  const part = await embedder.embedBatch(texts.slice(i, i + CHUNK));\n  // part.length === slice length or the embedder already threw for this small chunk\n}","typeGuard":null,"tryCatchPattern":"try {\n  vectors = await embedder.embedBatch(texts);\n} catch (e) {\n  if (e instanceof Error && /embedBatch\\(\\) returned \\d+ embeddings for \\d+ texts/.test(e.message)) {\n    // count mismatch: split and retry chunk-by-chunk so a bad chunk is isolated\n    vectors = [];\n    for (let i = 0; i < texts.length; i += 100) {\n      vectors.push(...(await embedder.embedBatch(texts.slice(i, i + 100))));\n    }\n  } else throw e;\n}","preventionTips":["Chunk embedBatch calls to a few hundred texts instead of one giant call","Trim inputs and drop empty strings before batching","Log both counts from the message to distinguish truncation from duplication"],"tags":["azure","openai","embeddings","data-integrity","batching"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}