pola-rs/polars · error · TypeError

did not expect type: in `concat

Error message

did not expect type: {qualified_type_name(elems[0])!r} in `concat`

What it means

`pl.concat` reached its final fallback without recognizing the element type: the sequence elements are none of DataFrame, LazyFrame, Series, or Expr, so it raises a TypeError naming the offending type. This is the exhaustiveness guard of the concat dispatcher.

Solutions

  1. Convert elements to a Polars type (DataFrame, Series, LazyFrame, or Expr) before concat
  2. Inspect `type(items[0])` to identify the unexpected element type
  3. Fix the producer of the list so it yields Polars objects

Example fix

// before
pl.concat([{'a': 1}, {'a': 2}])
// after
pl.concat([pl.DataFrame([{'a': 1}]), pl.DataFrame([{'a': 2}])])
Defensive patterns

Strategy: type-guard

Validate before calling

POLARS_TYPES = (pl.DataFrame, pl.LazyFrame, pl.Series, pl.Expr)
if not all(isinstance(x, POLARS_TYPES) for x in items):
    raise TypeError(f'unexpected element type: {type(items[0])!r}')

Type guard

def is_polars_concat_input(items: list) -> bool:
    return bool(items) and all(
        isinstance(x, (pl.DataFrame, pl.LazyFrame, pl.Series, pl.Expr)) for x in items
    )

Try / catch

try:
    result = pl.concat(items)
except TypeError as e:
    if 'did not expect type' in str(e):
        result = pl.concat([pl.Series(x) if isinstance(x, (list, tuple)) else x for x in items])
    else:
        raise

Prevention

When it happens

Trigger: Calling `pl.concat([dict1, dict2])`, `pl.concat(['a', 'b'])`, or passing numpy arrays / plain lists of non-Polars objects.

Common situations: Forgetting to wrap raw data with `pl.DataFrame(...)`/`pl.Series(...)`; passing mixed lists where the first element slipped an earlier type check; a helper returning the wrong object type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/1499754eebe1501a. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/functions/eager.py:374

                )
            )
        else:
            allowed = ", ".join(repr(m) for m in get_args(ConcatMethod))
            msg = f"LazyFrame `how` must be one of {{{allowed}}}, got {how!r}"
            raise ValueError(msg)

    elif is_non_empty_sequence_of(elems, pl.Series):
        if how == "vertical":
            out = wrap_s(plr.concat_series(elems))
        else:
            msg = "Series only supports 'vertical' concat strategy"
            raise ValueError(msg)

    elif is_non_empty_sequence_of(elems, pl.Expr):
        return wrap_expr(plr.concat_expr([e._pyexpr for e in elems], rechunk))
    else:
        msg = f"did not expect type: {qualified_type_name(elems[0])!r} in `concat`"
        raise TypeError(msg)

    if rechunk:
        return out.rechunk()
    return out


def union(
    items: Iterable[PolarsType],
    *,
    how: ConcatMethod = "vertical",
    strict: bool | None = None,
) -> PolarsType:
    """
    Combine multiple DataFrames, LazyFrames, or Series into a single object.

    .. warning::
        This function does not guarantee any specific ordering of rows in the result.
        If you need predictable row ordering, use `pl.concat()` instead.

View on GitHub (pinned to fe841f959e)