pola-rs/polars · error
invalid value for POLARS_CSV_CHUNK_SIZE
Error message
invalid value for POLARS_CSV_CHUNK_SIZE: {x} What it means
Polars panics when the POLARS_CSV_CHUNK_SIZE environment variable is set but cannot be parsed as a non-zero usize. The streaming CSV source reads this variable at read start (begin_read) to size its read chunks; an unset variable falls back to the default 32 MiB chunk size, but a set-and-invalid value is a hard panic.
Solutions
- Set POLARS_CSV_CHUNK_SIZE to a plain positive integer, e.g. 33554432 (no units, no zero, no sign).
- Unset the variable entirely (unset POLARS_CSV_CHUNK_SIZE or remove it from your env file) to use the default 32 MiB chunk size.
- Check the actual value with `echo "[$POLARS_CSV_CHUNK_SIZE]"` for hidden whitespace or empty strings in shell profiles / container env files.
Example fix
// before POLARS_CSV_CHUNK_SIZE=32MB // after POLARS_CSV_CHUNK_SIZE=33554432 # or unset entirely
Defensive patterns
Strategy: validation
Validate before calling
const v = process.env.POLARS_CSV_CHUNK_SIZE;
if (v !== undefined && (!/^\d+$/.test(v.trim()) || BigInt(v.trim()) === 0n)) {
throw new Error(`POLARS_CSV_CHUNK_SIZE must be a positive integer, got: '${v}'`);
} Type guard
function isValidChunkSize(v) { return v === undefined || (/^\d+$/.test(v.trim()) && Number(v.trim()) > 0); } Try / catch
try {
runPolarsCsvScan();
} catch (e) {
if (String(e).includes('POLARS_CSV_CHUNK_SIZE')) {
delete process.env.POLARS_CSV_CHUNK_SIZE; // fall back to default and retry
runPolarsCsvScan();
} else throw e;
} Prevention
- Only set tuning env vars to bare positive integers, never with units or zero.
- Keep such env vars out of shared shell rc / container base images.
- Validate all POLARS_* env vars at process startup with a small check script.
When it happens
Trigger: Starting any streaming CSV scan (scan_csv in the streaming engine) with POLARS_CSV_CHUNK_SIZE set to an empty string, a non-numeric string (e.g. '32MB'), zero ('0'), a negative number, or a value exceeding usize range.
Common situations: Developer exports POLARS_CSV_CHUNK_SIZE=0 to 'disable' chunking, sets a human-readable value like '64m', leaves an empty value in a container env file, or copies a value with a trailing newline/space from docs.
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
- invalid value for POLARS_FORCE_NDJSON_READ_SIZE
- invalid value for POLARS_LINES_CHUNK_PREFETCH_LIMIT
- invalid value for POLARS_MAX_CONCURRENT_SCANS
- invalid value for POLARS_NDJSON_CHUNK_PREFETCH_LIMIT
- invalid value for POLARS_NUM_READERS_PRE_INIT
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/5212b9a85a5f9b64.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-stream/src/nodes/io_sources/csv/mod.rs:227
!(matches!(byte_source.as_ref(), &DynByteSource::Buffer(_)) && compression.is_none());
const ASSUMED_COMPRESSION_RATIO: usize = 4;
let decompressed_file_size_hint = match compression {
None => Some(file_size),
Some(_) => Some(file_size * ASSUMED_COMPRESSION_RATIO),
};
// Unify the two source options (uncompressed local file mmapp'ed, or streaming async with
// transparent decompression), into one unified reader source.
let reader_source = if use_async_prefetch {
// Prepare parameters for Prefetch task.
// TODO REFACTOR: use get_streaming_chunk_size(),
const DEFAULT_CSV_CHUNK_SIZE: usize = 32 * 1024 * 1024;
let memory_prefetch_func = get_memory_prefetch_func(verbose);
let chunk_size = std::env::var("POLARS_CSV_CHUNK_SIZE")
.map(|x| {
x.parse::<NonZeroUsize>()
.unwrap_or_else(|_| panic!("invalid value for POLARS_CSV_CHUNK_SIZE: {x}"))
.get()
})
.unwrap_or(DEFAULT_CSV_CHUNK_SIZE);
let prefetch_limit = self
.chunk_prefetch_sync
.prefetch_limit
.min(file_size.div_ceil(chunk_size))
.max(1);
let (prefetch_send, prefetch_recv) = tokio::sync::mpsc::channel(prefetch_limit);
// Task: Prefetch.
// Initiate parallel downloads of raw chunks.
let byte_source = byte_source.clone();
let prefetch_task = {
let prefetch_semaphore = Arc::clone(&self.chunk_prefetch_sync.prefetch_semaphore);
let prefetch_prev_all_spawned =View on GitHub (pinned to fe841f959e)