mem0ai/mem0 · error · Error

IDs array must be provided and have the same length as vecto

Error message

IDs array must be provided and have the same length as vectors.

What it means

insert() in the Langchain adapter throws when the ids array is missing or its length differs from the vectors array length. Each mem0 memory id must be attached to its vector as document metadata (_mem0_id), so a 1:1 correspondence is mandatory before documents are built.

Source

Thrown at mem0-ts/src/oss/src/vector_stores/langchain.ts:64

      this.dimension = (this.lcStore as any).embedding.embeddingDimension;
    }
    // If still no dimension, we might need to throw or warn, as it's needed for validation
    if (!this.dimension) {
      console.warn(
        "LangchainVectorStore: Could not determine embedding dimension. Input validation might be skipped.",
      );
    }
  }

  // --- Method Mappings ---

  async insert(
    vectors: number[][],
    ids: string[],
    payloads: Record<string, any>[],
  ): Promise<void> {
    if (!ids || ids.length !== vectors.length) {
      throw new Error(
        "IDs array must be provided and have the same length as vectors.",
      );
    }
    if (this.dimension) {
      vectors.forEach((v, i) => {
        if (v.length !== this.dimension) {
          throw new Error(
            `Vector dimension mismatch at index ${i}. Expected ${this.dimension}, got ${v.length}`,
          );
        }
      });
    }

    // Convert payloads to Langchain Document metadata format
    const { Document } = await import("@langchain/core/documents");
    const documents = payloads.map((payload, i) => {
      // Provide empty pageContent, store mem0 id and other data in metadata
      return new Document({

View on GitHub (pinned to 001c235229)

Solutions

  1. Generate one id per vector before calling insert (e.g. ids = vectors.map(() => crypto.randomUUID())).
  2. When batching, slice vectors, ids, and payloads with the same range so lengths stay equal.
  3. Add an assertion in calling code: vectors.length === ids.length === payloads.length.

Example fix

// before
await store.insert(vectors.slice(0, 10), ids, payloads.slice(0, 10));

// after
await store.insert(
  vectors.slice(0, 10),
  ids.slice(0, 10),
  payloads.slice(0, 10),
);
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(ids) || ids.length !== vectors.length || payloads.length !== vectors.length) {
  throw new Error(`Length mismatch: vectors=${vectors.length} ids=${ids?.length} payloads=${payloads.length}`);
}

Prevention

When it happens

Trigger: Calling store.insert(vectors, ids, payloads) where ids is undefined, shorter, or longer than vectors — typically from a custom pipeline or a mis-sliced batch where vectors/ids were sliced inconsistently.

Common situations: Custom callers slicing batches of vectors and ids with different offsets; forgetting to generate ids for a new batch; passing payloads.length vectors but a deduplicated ids array.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/324fd208a540574b. Report an issue: GitHub.