pola-rs/polars · error · InvalidArgument
map keys cannot be a nested dtype, got
Error message
map keys cannot be a nested dtype, got {key!r} What it means
The maps() strategy in polars' parametric testing module refuses Map dtypes whose key dtype is nested (e.g. List, Struct). Polars Map keys must be flat scalar types, so generating data with nested keys is impossible and InvalidArgument is raised up front.
Solutions
- Use a flat key dtype such as pl.String or pl.Int64 for the Map key.
- If keys must be complex, serialize them to strings (e.g. JSON-encode) and store as a String-keyed Map.
- Check dtype.is_nested() before calling maps() and fail fast with a clearer message.
Example fix
// before maps(key=pl.List(pl.Int64), value=pl.String) // after maps(key=pl.String, value=pl.String)
Defensive patterns
Strategy: validation
Validate before calling
if key.is_nested():
raise ValueError(f"Map key must be flat, got {key}") Type guard
def is_flat_key(dtype) -> bool:
return not dtype.is_nested() and dtype != pl.Null Try / catch
from polars.exceptions import InvalidArgument
try:
strat = maps(key=key_dtype, value=value_dtype)
except InvalidArgument:
strat = maps(key=pl.String, value=value_dtype) # serialize keys instead Prevention
- Always declare Map dtypes with explicit flat key types.
- Serialize complex keys (JSON-encode) before storing in a Map.
- Run a schema lint that rejects nested Map keys in test schemas.
When it happens
Trigger: Calling maps(key=pl.List(pl.Int64), value=...) or passing any nested key dtype; also triggering via data(pl.Map(pl.List(...), value)) with a nested key.
Common situations: Modeling JSON-like structures where dict keys are arrays; translating a schema from another system that permits complex keys; typos nesting the key dtype when constructing pl.Map.
Related errors
- map keys cannot be Null; specify a key type, e.g…
- unsupported data type
- unsupported data type
- invalid Map dtype in `Series::full_null`
- polars.testing.parametric requires the 'hypothesis'…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/8572dffa8dba14b5.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/testing/parametric/strategies/data.py:388
Parameters
----------
key
The data type of the keys. Cannot be nested, since keys become Python dict keys
and those must be hashable.
value
The data type of the values.
min_size
The minimum number of entries in a map.
max_size
The maximum number of entries in a map.
allow_null
Allow nulls as possible values.
**kwargs
Additional arguments that are passed to nested data generation strategies.
"""
if key.is_nested():
msg = f"map keys cannot be a nested dtype, got {key!r}"
raise InvalidArgument(msg)
if key == Null:
# Reached via a bare `pl.Map`, which cannot say what its keys are. Generating
# `{None: None}` would be worse than refusing: Map keys are never null.
msg = "map keys cannot be Null; specify a key type, e.g. `pl.Map(pl.String, pl.Int64)`"
raise InvalidArgument(msg)
if max_size is None:
max_size = _DEFAULT_MAP_SIZE_LIMIT
inner_kwargs = {
k: v for k, v in kwargs.items() if k not in ("min_size", "max_size")
}
return st.dictionaries(
data(key, allow_null=False, **inner_kwargs),
data(value, allow_null=allow_null, **inner_kwargs),
min_size=min_size,
max_size=max_size,
)
View on GitHub (pinned to fe841f959e)