pola-rs/polars · error

invalid value for POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET

Error message

invalid value for POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET: {x}

What it means

Polars panics when POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET is set but cannot be parsed as a non-zero usize. The IPC source builder reads it in set_execution_state as a byte-budget (in KiB) for prefetched batches; unset falls back to a budget computed from FetchConfig's target chunk size.

Solutions

  1. Set the variable to a bare positive integer number of KiB, e.g. 262144.
  2. Unset the variable to use the default budget derived from FetchConfig::random_access().chunk_size.
  3. Strip any unit suffixes or whitespace from the value before export.

Example fix

// before
POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET=256MB
// after
POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET=262144
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try {
  runIpcScan();
} catch (e) {
  if (String(e).includes('PREFETCH_KBYTES_BUDGET')) {
    delete process.env.POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET;
    runIpcScan();
  } else throw e;
}

Prevention

When it happens

Trigger: Starting a streaming IPC scan with the variable set to a non-numeric, zero, negative, or out-of-range string (e.g. '10MB', '', '0').

Common situations: Operators copy a memory budget like '262144KB' or '256MB' from tuning notes; the 'KB'/'MB' suffix and empty values are the usual culprits.

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/311c941aca908cce. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-stream/src/nodes/io_sources/ipc/builder.rs:75

                        panic!("invalid value for POLARS_RECORD_BATCH_PREFETCH_SIZE: {x}")
                    })
                    .get()
            })
            .unwrap_or(
                // Similar to Parquet.
                execution_state
                    .num_pipelines
                    .saturating_mul(2)
                    .max(get_request_budget() as usize)
                    .clamp(16, 2048),
            )
            .max(1);

        let prefetch_kbytes_limit = std::env::var("POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET")
            .map(|x| {
                x.parse::<NonZeroUsize>()
                    .unwrap_or_else(|_| {
                        panic!("invalid value for POLARS_RECORD_BATCH_PREFETCH_KBYTES_BUDGET: {x}")
                    })
                    .get()
            })
            .unwrap_or({
                // Similar to Parquet.
                let target_chunk_size_kb = FetchConfig::random_access().chunk_size.div_ceil(1024);
                4 * execution_state.num_pipelines * target_chunk_size_kb
            })
            // Avoid deadlock.
            .max(polars_io::cloud::concurrency_config::get_download_chunk_size().div_ceil(1024));

        if config::verbose() {
            eprintln!(
                "[IpcReaderBuilder]: prefetch_limit: {}, prefetch_kbytes_limit: {}",
                prefetch_limit, prefetch_kbytes_limit
            );
        }

View on GitHub (pinned to fe841f959e)