Skip to main content

The Key/K Class

The Key class (aliased as K for brevity) provides a fluent interface for building filter expressions. Use K to reference document fields, IDs, and metadata properties.

Filterable Fields

Comparison Operators

Supported operators:
  • == - Equality (all types: string, numeric, boolean)
  • != - Inequality (all types: string, numeric, boolean)
  • > - Greater than (numeric only)
  • >= - Greater than or equal (numeric only)
  • < - Less than (numeric only)
  • <= - Less than or equal (numeric only)
Chroma supports three data types for metadata: strings, numbers (int/float), and booleans. Order comparison operators (>, <, >=, <=) currently only work with numeric types.

Set and String Operators

Supported operators:
  • is_in() - Value matches any in the list
  • not_in() - Value doesn’t match any in the list
  • contains() - On K.DOCUMENT: substring search (case-sensitive). On metadata fields: checks if an array contains a scalar value.
  • not_contains() - On K.DOCUMENT: excludes by substring. On metadata fields: checks that an array does not contain a scalar value.
  • regex() - String matches regex pattern (currently K.DOCUMENT only)
  • not_regex() - String doesn’t match regex pattern (currently K.DOCUMENT only)
String operations like contains() and regex() on K.DOCUMENT are case-sensitive by default. When used on metadata fields, contains() checks array membership rather than substring matching. The is_in() operator is efficient even with large lists.

Array Metadata

Chroma supports storing arrays of values in metadata fields. You can use contains() / not_contains() (or $contains / $not_contains in dictionary syntax) to filter records based on whether an array includes a specific scalar value.

Storing Array Metadata

Arrays can contain strings, numbers, or booleans. All elements in an array must be the same type. Empty arrays are not allowed.

Filtering Arrays

Use contains() to check if a metadata array includes a value, and not_contains() to check that it does not.

Supported Array Types

The $contains value must be a scalar that matches the array’s element type. All elements in an array must be the same type, and nested arrays are not supported.

Logical Operators

Supported operators:
  • & - Logical AND (all conditions must match)
  • | - Logical OR (any condition can match)
Combine multiple conditions using these operators. Always use parentheses to ensure correct precedence.
Always use parentheses around each condition when using logical operators. Python’s operator precedence may not work as expected without them.

Common Filtering Patterns

Edge Cases and Important Behavior

Missing Keys

When filtering on a metadata field that doesn’t exist for a document:
  • Most operators (==, >, <, >=, <=, is_in()) evaluate to false - the document won’t match
  • != evaluates to true - documents without the field are considered “not equal” to any value
  • not_in() evaluates to true - documents without the field are not in any list

Mixed Types

Avoid storing different data types under the same metadata key across documents. Query behavior is undefined when comparing values of different types.

String Pattern Matching Limitations

regex() and not_regex() only work on K.DOCUMENT. These operators do not yet support metadata fields. contains() and not_contains() have different behavior depending on the field:
  • On K.DOCUMENT: substring search (the pattern must have at least 3 literal characters)
  • On metadata fields: array membership check (see Array Metadata above)
Substring matching on metadata scalar fields (e.g. checking if a string field contains a substring) is not yet supported.
regex() and not_regex() currently only work on K.DOCUMENT. Substring matching on metadata scalar fields is not yet available. Also, patterns with fewer than 3 literal characters may return incorrect results.
Substring and regex matching on metadata scalar fields is not currently supported. Full support is coming in a future release, which will allow users to opt-in to additional indexes for string pattern matching on specific metadata fields.

Complete Example

Here’s a practical example combining different filter types:

Tips and Best Practices

  • Use parentheses liberally when combining conditions with & and | to avoid precedence issues
  • Filter before ranking when possible to reduce the number of vectors to score
  • Be specific with ID filters - using K.ID.is_in() with a small list is very efficient
  • String matching is case-sensitive - normalize your data if case-insensitive matching is needed
  • Use the right operator - is_in() for multiple exact matches, contains() for substring search

Next Steps