pola-rs/polars · error

invalid value for POLARS_NUM_READERS_PRE_INIT

Error message

invalid value for POLARS_NUM_READERS_PRE_INIT: {x}

What it means

Polars panics when POLARS_NUM_READERS_PRE_INIT is set but cannot be parsed as a non-zero usize. calc_n_readers_pre_init (called during multi-scan initialize) uses this variable to override the computed number of readers opened before the first file properties are known.

Solutions

  1. Set the variable to a positive integer (e.g. 4).
  2. Unset it to let Polars compute readers from num_pipelines, num_sources, and pre_slice.
  3. Sanitize CI/env-file config: remove variable lines with empty values.

Example fix

// before
POLARS_NUM_READERS_PRE_INIT=0
// after
POLARS_NUM_READERS_PRE_INIT=4  # or unset
Defensive patterns

Strategy: validation

Validate before calling

const v = process.env.POLARS_NUM_READERS_PRE_INIT;
if (v !== undefined && (!/^\d+$/.test(v.trim()) || parseInt(v, 10) === 0)) {
  throw new Error(`POLARS_NUM_READERS_PRE_INIT must be a positive integer, got: '${v}'`);
}

Type guard

function isValidReaderCount(v) { return v === undefined || (/^\d+$/.test(v.trim()) && Number(v.trim()) > 0); }

Try / catch

try {
  runMultiFileScan();
} catch (e) {
  if (String(e).includes('POLARS_NUM_READERS_PRE_INIT')) {
    delete process.env.POLARS_NUM_READERS_PRE_INIT;
    runMultiFileScan();
  } else throw e;
}

Prevention

When it happens

Trigger: Executing a multi-file scan (scan_parquet/scan_ipc on multiple files) with POLARS_NUM_READERS_PRE_INIT set to '', '0', 'none', or any value outside the non-zero usize range.

Common situations: Benchmarkers setting the variable to '0' expecting 'auto' behavior, or leaving it defined-but-empty in CI environment configuration.

Understand the failure class

Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.

Related errors


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

Appendix: source

Thrown at crates/polars-stream/src/nodes/io_sources/multi_scan/functions/mod.rs:23

use polars_io::cloud::CloudOptions;
use polars_io::cloud::concurrency_config::FetchConfig;
use polars_io::metrics::IOMetrics;
use polars_io::utils::byte_source::{ByteSource, DynByteSourceBuilder};
use polars_io::utils::compression::SupportedCompression;
use polars_plan::dsl::ScanSource;
use polars_utils::slice_enum::Slice;

pub mod resolve_projections;
pub mod resolve_slice;

pub fn calc_n_readers_pre_init(
    num_pipelines: usize,
    num_sources: usize,
    pre_slice: Option<&Slice>,
) -> usize {
    if let Ok(v) = std::env::var("POLARS_NUM_READERS_PRE_INIT").map(|x| {
        x.parse::<NonZeroUsize>()
            .unwrap_or_else(|_| panic!("invalid value for POLARS_NUM_READERS_PRE_INIT: {x}"))
            .get()
    }) {
        return v;
    }

    let max_files_with_slice = match pre_slice {
        // Calculate the max number of files assuming 1 row per file.
        Some(v @ Slice::Positive { .. }) => v.end_position().max(1),
        Some(Slice::Negative { .. }) | None => usize::MAX,
    };

    // Set this generously high, there are users who scan 10,000's of small files from the cloud.
    num_pipelines
        .saturating_add(3)
        .min(max_files_with_slice)
        .min(num_sources)
        .clamp(1, 128)
}

View on GitHub (pinned to fe841f959e)