pola-rs/polars · error

invalid value for POLARS_FORCE_CSV_INFER_READ_SIZE

Error message

invalid value for POLARS_FORCE_CSV_INFER_READ_SIZE: {x}

What it means

This panic fires when the environment variable POLARS_FORCE_CSV_INFER_READ_SIZE is set to a value that cannot be parsed as a non-zero usize. The variable forces a fixed read size during CSV schema inference on compressed readers, overriding the adaptive initial_read_size. Because the value is consumed as configuration, Polars panics instead of ignoring the bad value.

Solutions

  1. Set POLARS_FORCE_CSV_INFER_READ_SIZE to a plain positive integer (e.g. 1048576) or unset it to use adaptive read sizing
  2. Remove units, decimals, signs, and whitespace from the value
  3. Verify the variable is not exported globally in shell profiles after temporary debugging

Example fix

// before
export POLARS_FORCE_CSV_INFER_READ_SIZE=1KB
// after
export POLARS_FORCE_CSV_INFER_READ_SIZE=65536
Defensive patterns

Strategy: validation

Validate before calling

const v = process.env.POLARS_FORCE_CSV_INFER_READ_SIZE;
if (v !== undefined && !/^[0-9]+$/.test(v.trim())) throw new Error(`POLARS_FORCE_CSV_INFER_READ_SIZE must be a positive integer, got: ${v}`);

Type guard

function isValidReadSize(v) { return typeof v === 'string' && /^[1-9][0-9]*$/.test(v.trim()); }

Try / catch

// unset invalid tuning vars before running inference
const raw = process.env.POLARS_FORCE_CSV_INFER_READ_SIZE;
if (raw && !/^\d+$/.test(raw.trim())) { delete process.env.POLARS_FORCE_CSV_INFER_READ_SIZE; }

Prevention

When it happens

Trigger: export POLARS_FORCE_CSV_INFER_READ_SIZE=0, 'abc', '4096.0', '-1', or an empty string; triggered in for_each_line_from_reader_from_compressed_reader (via read_until_start_and_infer_schema_from_compressed_reader) when reading compressed CSVs for schema inference.

Common situations: Users forcing deterministic read sizes to work around inference bugs on gzipped CSVs and mistyping the value; using units like '1KB'; stale values left in CI or .env files after the debugging session ends.

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

Appendix: source

Thrown at crates/polars-io/src/csv/read/streaming.rs:409

/// Iterate over valid CSV lines produced by reader.
///
/// Returning `ConsumeDiscard` after `ConsumeKeep` is a logic error, since a segmented `Buffer`
/// can't be constructed.
fn for_each_line_from_reader_from_compressed_reader(
    parse_options: &CsvParseOptions,
    is_file_start: bool,
    mut prev_leftover: Buffer<u8>,
    initial_read_size: usize,
    reader: &mut CompressedReader,
    mut line_fn: impl FnMut(Buffer<u8>) -> PolarsResult<LineUse>,
) -> PolarsResult<Buffer<u8>> {
    let mut is_first_line = is_file_start;

    let fixed_read_size = std::env::var("POLARS_FORCE_CSV_INFER_READ_SIZE")
        .map(|x| {
            x.parse::<NonZeroUsize>()
                .unwrap_or_else(|_| {
                    panic!("invalid value for POLARS_FORCE_CSV_INFER_READ_SIZE: {x}")
                })
                .get()
        })
        .ok();

    let mut read_size = fixed_read_size.unwrap_or(initial_read_size);
    let mut retain_offset = None;

    loop {
        let (mut slice, bytes_read) = reader.read_next_slice(&prev_leftover, read_size)?;
        if slice.is_empty() {
            return Ok(Buffer::new());
        }

        if is_first_line {
            is_first_line = false;
            const UTF8_BOM_MARKER: Option<&[u8]> = Some(b"\xef\xbb\xbf");
            if slice.get(0..3) == UTF8_BOM_MARKER {

View on GitHub (pinned to fe841f959e)