pola-rs/polars · error · AssertionError

unexpected or unrecognised op name

Error message

unexpected or unrecognised op name ({opname})

Please report a bug to https://github.com/pola-rs/polars/issues with the content of function you were passing to the `map` expression and the following instruction object:
{inst!r}

What it means

During bytecode-to-expression translation the parser encounters a disassembled op whose name it has no rule for. It raises AssertionError telling the user to report the bug upstream with the function passed to map_elements and the offending instruction object. It signals a gap in the opcode coverage of the UDF parser.

Solutions

  1. Rewrite the UDF with simpler arithmetic/logic the parser supports, or replace with native polars expressions.
  2. Pin/upgrade polars to a version matching your Python version's opcode set.
  3. Report the bug to https://github.com/pola-rs/polars/issues including the function and instruction object from the message.

Example fix

// before
pl.col('a').map_elements(lambda x: x @ M)
// after
pl.col('a').map_elements(simple_scalar_fn)  # or native: pl.col('a') * 2
Defensive patterns

Strategy: try-catch

Validate before calling

def parser_safe(fn):
    import dis
    try:
        list(dis.get_instructions(fn))
        return True
    except TypeError:
        return False

Type guard

def is_simple_function(fn) -> bool:
    import types
    return isinstance(fn, types.FunctionType) and not isinstance(fn, types.LambdaType) or callable(fn)

Try / catch

try:
    out = df.select(pl.col('a').map_elements(fn))
except AssertionError as e:
    if 'unexpected or unrecognised op name' in str(e):
        out = df.select(pl.col('a').map_elements(fn, skip_nulls=True)) if False else fallback(fn)  # compute outside parser

Prevention

When it happens

Trigger: Passing to map_elements a function whose bytecode contains an opcode the parser doesn't handle (unusual operators, exotic constructs like complex comprehensions, async, or newer CPython opcodes); triggered from op() via _to_intermediate_stack/_height_preserving.

Common situations: Upgrading Python (new opcodes) while using an older polars; using advanced Python features inside a UDF (walrus, matrix ops, custom __mul__ objects); parser hit on the public to_expression path.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/_utils/udfs.py:675

            return OpNames.CONTROL_FLOW[opname]
        elif inst.argrepr:
            return inst.argrepr
        elif opname == "IS_OP":
            return "is not" if inst.argval else "is"
        elif opname == "CONTAINS_OP":
            return "not in" if inst.argval else "in"
        elif opname in OpNames.UNARY:
            return OpNames.UNARY[opname]
        elif opname == "BINARY_SUBSCR":
            return "replace_strict"
        else:
            msg = (
                f"unexpected or unrecognised op name ({opname})\n\n"
                "Please report a bug to https://github.com/pola-rs/polars/issues "
                "with the content of function you were passing to the `map` "
                f"expression and the following instruction object:\n{inst!r}"
            )
            raise AssertionError(msg)

    def _expr(self, value: StackEntry, col: str, param_name: str, depth: int) -> str:
        """Take stack entry value and convert to polars expression string."""
        if isinstance(value, StackValue):
            op = _RE_STRIP_BOOL.sub(r"\1", value.operator)
            e1 = self._expr(value.left_operand, col, param_name, depth + 1)
            if value.operator_arity == 1:
                if op not in OpNames.UNARY_VALUES:
                    if e1.startswith("pl.col("):
                        call = "" if op.endswith(")") else "()"
                        return f"{e1}.{op}{call}"
                    if e1[0] in OpNames.UNARY_VALUES and e1[1:].startswith("pl.col("):
                        call = "" if op.endswith(")") else "()"
                        return f"({e1}).{op}{call}"

                    # support use of consts as numpy/builtin params, eg:
                    # "np.sin(3) + np.cos(x)", or "len('const_string') + len(x)"
                    if (

View on GitHub (pinned to fe841f959e)