pola-rs/polars · error

no valid schema can be derived for the query

Error message

no valid schema can be derived for the query

What it means

Raised by the public `project` method of the IR builder in crates/polars-plan/src/plans/builder_ir.rs when `expr_irs_to_schema` cannot derive an output Schema for the projection expressions. This means at least one expression's dtype could not be resolved during IR conversion (typically an expression whose input dtype is unknown/unresolvable).

Solutions

  1. Check that every expression in the projection references columns present in the input schema (print lf.collect_schema())
  2. Fix or remove the offending expression; add explicit casts where dtypes are ambiguous
  3. Update polars — some unresolved-dtype bugs are fixed in newer releases

Example fix

# before
lf.select(pl.col("total") / pl.col("cnt"))  # 'cnt' was renamed to 'count'
# after
lf = lf.rename({"count": "cnt"})
lf.select(pl.col("total") / pl.col("cnt"))
Defensive patterns

Strategy: validation

Validate before calling

schema = lf.collect_schema()
for expr in exprs:
    for name in expr.meta_output_names():
        assert name in schema, f"unknown column {name} in projection"

Try / catch

try:
    result = lf.select(exprs).collect()
except pl.exceptions.PanicException as e:
    if "no valid schema can be derived" in str(e):
        print("projection references missing/unresolvable columns; check collect_schema()")
        raise
    raise

Prevention

When it happens

Trigger: Calling select/project with expressions whose output dtype cannot be resolved, e.g. expressions referencing columns that don't exist in the input schema, or dtype-resolving expressions over an incompatible schema.

Common situations: Selecting a column that was renamed/dropped upstream in a lazy pipeline, or using expressions that depend on schema info unavailable at plan time (e.g. resolve of unknown dtypes in custom/anonymous expressions).

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at crates/polars-plan/src/plans/builder_ir.rs:68

        Ok(b)
    }

    /// An escape hatch to add an `Expr`. Working with IR is preferred.
    pub fn add_expr(&mut self, expr: Expr) -> PolarsResult<ExprIR> {
        let schema = self.lp_arena.get(self.root).schema(self.lp_arena);
        let mut ctx = ExprToIRContext::new(self.expr_arena, &schema);
        to_expr_ir(expr, &mut ctx)
    }

    pub fn project(self, exprs: Vec<ExprIR>, options: ProjectionOptions) -> Self {
        // if len == 0, no projection has to be done. This is a select all operation.
        if exprs.is_empty() {
            self
        } else {
            let input_schema = self.schema();
            let schema = expr_irs_to_schema(&exprs, &input_schema, self.expr_arena)
                .expect("no valid schema can be derived for the query");

            let lp = IR::Select {
                expr: exprs,
                input: self.root,
                schema: Arc::new(schema),
                options,
            };
            let node = self.lp_arena.add(lp);
            IRBuilder::new(node, self.expr_arena, self.lp_arena)
        }
    }

    pub fn project_simple_nodes<I, N>(self, nodes: I) -> PolarsResult<Self>
    where
        I: IntoIterator<Item = N>,
        N: Into<Node>,
        I::IntoIter: ExactSizeIterator,
    {

View on GitHub (pinned to fe841f959e)