chroma-core/chroma · error · TypeError

$not_regex requires a string pattern

Error message

$not_regex requires a string pattern

What it means

Key.notRegex(pattern) builds a $not_regex where-filter and requires the pattern to be a string at runtime. Non-string patterns — RegExp objects, numbers, null/undefined — raise a TypeError client-side before the filter is serialized.

Source

Thrown at clients/new-js/packages/chromadb/src/execution/expression/key.ts:93

   * K("tags").notContains("draft")          // metadata array not-contains
   */
  public notContains(value: string | number | boolean): WhereExpression {
    if (this.name === "#document" && typeof value !== "string") {
      throw new TypeError("K.DOCUMENT.notContains requires a string value");
    }
    return createComparisonWhere(this.name, "$not_contains", value);
  }

  public regex(pattern: string): WhereExpression {
    if (typeof pattern !== "string") {
      throw new TypeError("$regex requires a string pattern");
    }
    return createComparisonWhere(this.name, "$regex", pattern);
  }

  public notRegex(pattern: string): WhereExpression {
    if (typeof pattern !== "string") {
      throw new TypeError("$not_regex requires a string pattern");
    }
    return createComparisonWhere(this.name, "$not_regex", pattern);
  }
}

export interface KeyFactory {
  (name: string): Key;
  ID: Key;
  DOCUMENT: Key;
  EMBEDDING: Key;
  METADATA: Key;
  SCORE: Key;
}

const createKeyFactory = (): KeyFactory => {
  const factory = ((name: string) => new Key(name)) as KeyFactory;
  factory.ID = Key.ID;
  factory.DOCUMENT = Key.DOCUMENT;

View on GitHub (pinned to aecdd12c8a)

Solutions

  1. Pass a string pattern: K("title").notRegex("draft|deprecated")
  2. Use re.source when starting from a RegExp object
  3. Conditionally apply the filter only when the configured pattern is a non-empty string

Example fix

// before
K("title").notRegex(cfg.exclusionPattern); // undefined in prod -> TypeError

// after
const where = typeof cfg.exclusionPattern === "string"
  ? K("title").notRegex(cfg.exclusionPattern)
  : undefined;
Defensive patterns

Strategy: validation

Validate before calling

const pattern = typeof cfg.exclusionPattern === "string" ? cfg.exclusionPattern : undefined;
const where = pattern ? K("title").notRegex(pattern) : undefined;

Type guard

const isPatternString = (v: unknown): v is string => typeof v === "string" && v.length > 0;

Prevention

When it happens

Trigger: K("title").notRegex(re) where re is a RegExp instance; .notRegex(undefined) when a configurable exclusion pattern is not set.

Common situations: Mirroring a regex filter feature with an exclusion variant and reusing a compiled RegExp. Config-driven exclusion patterns that may be missing (undefined) in some environments. JS callers or any-typed values bypassing the declared string parameter.

Related errors


AI-assisted analysis of chroma-core/chroma@aecdd12c8a (2026-08-16). Data as JSON: /api/errors/071fc7d9e5f3f90e. Report an issue: GitHub.