mem0ai/mem0 · error
Invalid threshold: ${threshold}. Must be between 0 and 1 (in
Error message
Invalid threshold: ${threshold}. Must be between 0 and 1 (inclusive). What it means
Thrown by validateSearchParams when threshold is a valid number but lies outside the inclusive [0, 1] range. Threshold is a similarity score cutoff, so values like 1.5 or -0.2 are meaningless and rejected before the vector search executes. The message echoes the offending value.
Source
Thrown at mem0-ts/src/oss/src/memory/index.ts:190
if (/\s/.test(trimmed)) {
throw new Error(
`Invalid ${name}: cannot contain whitespace. Provide a valid identifier without spaces.`,
);
}
return trimmed;
}
/**
* 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;View on GitHub (pinned to 001c235229)
Solutions
- Pass a fraction in [0,1]: threshold: 0.7 for 70% similarity.
- If your config stores percentages, divide by 100 at the call site: threshold: pct / 100.
- Clamp at the boundary when threshold is user-supplied: Math.min(1, Math.max(0, value)).
- Recheck semantics: higher threshold = stricter matching; 0 returns everything the store yields.
Example fix
// before
await memory.search('preferences', { threshold: 75 }); // treated as percent -> throws
// after
await memory.search('preferences', { threshold: 0.75 }); // 0–1 similarity cutoff Defensive patterns
Strategy: validation
Validate before calling
function normalizeThreshold(raw: unknown): number | undefined {
if (raw === undefined) return undefined;
const n = Number(raw);
if (!Number.isFinite(n)) return undefined;
return Math.min(1, Math.max(0, n)); // clamp percentages/mistakes into range
} Type guard
function isInRangeThreshold(t: unknown): t is number {
return typeof t === 'number' && t >= 0 && t <= 1;
} Prevention
- Store thresholds as 0–1 fractions everywhere in config, never percentages.
- Divide percentage inputs by 100 at the boundary and document the unit.
- Add tests for 0, 1, and out-of-range boundaries on any user-facing threshold.
When it happens
Trigger: Calling memory.search with threshold: 5, threshold: 1.2, or a negative value; or treating threshold as a percentage (passing 70 for 70%) instead of a 0–1 fraction.
Common situations: Config authored as a percent (70) by someone assuming 0–100 scale; UI sliders exposing 0–100; arithmetic bugs like threshold: score * 100; copy-paste from code expecting a distance bound instead of similarity.
Related errors
- threshold must be a valid number
- topK must be a valid integer
- Invalid topK: ${topK}. Must be a non-negative integer.
- Invalid threshold: {threshold}. Must be between 0 and 1 (inc
- Either memoryId or --all is required
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/86f29575d198befd.
Report an issue: GitHub.