mem0ai/mem0 · error

Invalid topK: ${topK}. Must be a non-negative integer.

Error message

Invalid topK: ${topK}. Must be a non-negative integer.

What it means

Thrown by validateSearchParams when topK is an integer but negative. A negative result count is nonsensical, so the SDK rejects it up front with a message echoing the value; zero is allowed (returns no results) but -1, -5, etc. throw.

Source

Thrown at mem0-ts/src/oss/src/memory/index.ts:200

 * @throws Error if threshold or topK are invalid
 */
function validateSearchParams(threshold?: number, topK?: number): void {
  if (threshold !== undefined) {
    if (typeof threshold !== "number" || isNaN(threshold)) {
      throw new Error("threshold must be a valid number");
    }
    if (threshold < 0 || threshold > 1) {
      throw new Error(
        `Invalid threshold: ${threshold}. Must be between 0 and 1 (inclusive).`,
      );
    }
  }
  if (topK !== undefined) {
    if (typeof topK !== "number" || isNaN(topK) || !Number.isInteger(topK)) {
      throw new Error("topK must be a valid integer");
    }
    if (topK < 0) {
      throw new Error(`Invalid topK: ${topK}. Must be a non-negative integer.`);
    }
  }
}

export class Memory {
  private config: MemoryConfig;
  private customInstructions: string | undefined;
  private embedder: Embedder;
  private vectorStore!: VectorStore;
  private llm: LLM;
  private reranker: Reranker | null = null;
  private db: HistoryManager;
  private collectionName: string | undefined;
  private apiVersion: string;
  telemetryId: string;
  private _initPromise: Promise<void>;
  private _initError?: Error;
  private _entityStore?: VectorStore;

View on GitHub (pinned to 001c235229)

Solutions

  1. Use a positive integer or omit topK to use the SDK default.
  2. Clamp pagination math: topK: Math.max(0, limit - offset).
  3. If -1 was meant as 'unlimited', pass a large positive integer instead or leave topK undefined.
  4. Add boundary tests on your pagination code for the last page where counts can go negative.

Example fix

// before
const topK = limit - offset; // can be negative on the last page
await memory.search('q', { topK });

// after
const topK = Math.max(0, limit - offset);
if (topK === 0) return []; // nothing left on this page
await memory.search('q', { topK });
Defensive patterns

Strategy: validation

Validate before calling

function safeTopK(computed: number): number {
  return Math.max(0, Math.floor(computed));
}

Type guard

function isNonNegativeIntegerTopK(k: unknown): k is number {
  return typeof k === 'number' && Number.isInteger(k) && k >= 0;
}

Prevention

When it happens

Trigger: Calling memory.search('q', { topK: -1 }); or computing topK from subtraction that can go negative, e.g. topK: requested - offset where offset exceeds requested.

Common situations: Pagination arithmetic bugs (limit - page*size underflowing), signaling 'no limit' with -1 as some APIs allow, or defaults built from env vars that contain '-5'.

Related errors


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