mem0ai/mem0 · error

topK must be a valid integer

Error message

topK must be a valid integer

What it means

Thrown by validateSearchParams when topK is defined but is not an integer number — it checks typeof number, non-NaN, and Number.isInteger. Floating-point or string topK values (e.g. '10' or 5.5) are rejected because a fractional result count is meaningless for the vector query.

Source

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

/**
 * Validates search parameters.
 * @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;

View on GitHub (pinned to 001c235229)

Solutions

  1. Convert and floor before calling: topK: Math.floor(Number(opts.topK)).
  2. Validate inputs at your API boundary and reject non-integer topK with your own 400 error.
  3. Guard arithmetic: ensure any computed topK passes Number.isInteger before use.
  4. Only include topK in the options when a valid integer is available.

Example fix

// before
await memory.search('q', { topK: process.env.TOP_K }); // string '10' -> throws

// after
const topK = Number.parseInt(process.env.TOP_K ?? '10', 10);
await memory.search('q', { topK }); // integer 10
Defensive patterns

Strategy: validation

Validate before calling

function parseTopK(raw: unknown): number | undefined {
  if (raw === undefined || raw === null || raw === '') return undefined;
  const n = Number(raw);
  if (!Number.isInteger(n)) throw new TypeError('topK must be an integer');
  return n;
}

Type guard

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

Prevention

When it happens

Trigger: Calling memory.search('q', { topK: '10' }) (string from env/JSON), topK: 5.5, or topK: NaN from bad arithmetic such as parseInt on undefined producing NaN.

Common situations: Reading topK from environment variables or request queries without Number() conversion; dividing a desired count (e.g. limit/2 when limit is odd) producing a float; config files parsed as strings.

Related errors


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