chroma-core/chroma · error · TypeError
k must be an integer, got
Error message
{op} k must be an integer, got {type(k).__name__} What it means
Raised by _parse_k_aggregate when the 'k' field of a $min_k or $max_k aggregate is not an integer — e.g. a string like "3" or a float was passed. The result count must be a whole number.
Solutions
- Pass k as an integer literal, not a string: "k": 3 not "k": "3"
- Coerce numeric strings with int(k) on the client before serializing
- Check that a float like 3.0 wasn't produced by upstream serialization
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at chromadb/execution/expression/operator.py:1371 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of chroma-core/chroma@aecdd12c8a (2026-08-16).
Data as JSON: /api/errors/5f38799bcd458dd4.
Report an issue: GitHub.
Appendix: source
Thrown at chromadb/execution/expression/operator.py:1371
ValueError: If required fields are missing or invalid
"""
agg_data = data[op]
if not isinstance(agg_data, dict):
raise TypeError(f"{op} requires a dict, got {type(agg_data).__name__}")
if "keys" not in agg_data:
raise ValueError(f"{op} requires 'keys' field")
if "k" not in agg_data:
raise ValueError(f"{op} requires 'k' field")
keys = agg_data["keys"]
if not isinstance(keys, (list, tuple)):
raise TypeError(f"{op} keys must be a list, got {type(keys).__name__}")
if not keys:
raise ValueError(f"{op} keys cannot be empty")
k = agg_data["k"]
if not isinstance(k, int):
raise TypeError(f"{op} k must be an integer, got {type(k).__name__}")
if k <= 0:
raise ValueError(f"{op} k must be positive, got {k}")
return _strings_to_keys(keys), k
@dataclass
class Aggregate:
"""Base class for aggregation expressions within groups.
Aggregations determine which records to keep from each group:
- MinK: Keep k records with minimum values (ascending order)
- MaxK: Keep k records with maximum values (descending order)
Examples:
# Keep top 3 by score per group (single key)
MinK(keys=Key.SCORE, k=3)
View on GitHub (pinned to aecdd12c8a)