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

  1. Use simple alphanumeric/underscore filter keys (and store metadata under those same keys).
  2. Sanitize or reject user-supplied filter keys before search() (map unsafe keys to safe aliases).
  3. 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

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


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