mem0ai/mem0 · error · Error
Invalid filter key: ${key}
Error message
Invalid filter key: ${key} What it means
Baidu Mochow filters are rendered into a SQL-like predicate string (metadata["key"] = value), so buildFilter validates each filter key against a safe-key regex (SAFE_FILTER_KEY) before interpolation, throwing for keys that fail. This is an injection guard: keys cannot be parameterized in the filter DSL. Keys with quotes, brackets, spaces, or special characters are rejected.
Source
Thrown at mem0-ts/src/oss/src/vector_stores/baidu.ts:266
indexName: BM25_INDEX,
indexType: IndexType.InvertedIndex,
fields: ["textLemmatized"],
fieldAttributes: [InvertedIndexFieldAttribute.Analyzed],
params: {
analyzer: InvertedIndexAnalyzer.EnglishAnalyzer,
parseMode: InvertedIndexParseMode.FineMode,
},
},
],
};
}
private buildFilter(filters: SearchFilters): string {
const conditions: string[] = [];
for (const [key, value] of Object.entries(filters)) {
if (!SAFE_FILTER_KEY.test(key)) {
throw new Error(`Invalid filter key: ${key}`);
}
if (typeof value === "string") {
conditions.push(`metadata["${key}"] = "${escapeFilterString(value)}"`);
continue;
}
if (typeof value === "number" || typeof value === "boolean") {
conditions.push(`metadata["${key}"] = ${value}`);
continue;
}
throw new Error(
`Filter value for ${key} must be str, int, float, or bool, got ${Array.isArray(value) ? "array" : typeof value}`,
);
}
return conditions.join(" AND ");View on GitHub (pinned to 001c235229)
Solutions
- Use simple alphanumeric/underscore filter keys (and store metadata under those same keys).
- Sanitize or reject user-supplied filter keys before search() (map unsafe keys to safe aliases).
- Never pass unvalidated external input as filter keys.
Example fix
// before
memory.search('q', { 'user-id': 'u1' });
// after
memory.search('q', { user_id: 'u1' }); Defensive patterns
Strategy: validation
Validate before calling
const SAFE_KEY = /^[A-Za-z_][A-Za-z0-9_]*$/;
function sanitizeFilterKeys(filters: Record<string, unknown>) {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(filters)) {
const key = SAFE_KEY.test(k) ? k : k.replace(/[^A-Za-z0-9_]/g, '_');
out[key] = v;
}
return out;
} Type guard
const isSafeFilterKey = (k: string): boolean => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k);
Prevention
- Restrict metadata keys used for filtering to word characters at write time.
- Never forward raw user input as filter keys; allowlist the accepted keys.
When it happens
Trigger: memory.search(query, { userId: 'alice' }) style filters where the key contains a dash or quote, e.g. { 'user-id': 'u1' }; filters built from raw user input keys like { "name'; DROP": 1 }; keys with spaces or non-ASCII.
Common situations: Using metadata keys with dashes or dots that other vector stores accept; forwarding arbitrary user-supplied filter keys to search(); schema drift between what you stored in metadata and what you filter on.
Related errors
- Filter value for ${key} must be str, int, float, or bool, go
- filters must contain at least one of: user_id, agent_id, run
- Unsupported metadata filter operator: {operator}
- AND operator requires a list of conditions
- OR operator requires a non-empty list of conditions
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/7238f1c52617c6ba.
Report an issue: GitHub.