pola-rs/polars · error
invalid value for POLARS_FORCE_NDJSON_READ_SIZE
Error message
invalid value for POLARS_FORCE_NDJSON_READ_SIZE: {x} What it means
Polars panics when POLARS_FORCE_NDJSON_READ_SIZE is set but cannot be parsed as a non-zero usize. The NDJSON line batch distributor reads it in new() to force a fixed read size (number of lines per batch); when unset or invalid-as-option it falls back to an automatically computed read size.
Solutions
- Set the variable to a plain positive integer of lines, e.g. 10000.
- Unset the variable so the read size is chosen automatically.
- Check for unit suffixes ('K', 'lines') or surrounding whitespace in the exported value.
Example fix
// before POLARS_FORCE_NDJSON_READ_SIZE=10K // after POLARS_FORCE_NDJSON_READ_SIZE=10000 # or unset
Defensive patterns
Strategy: validation
Validate before calling
const v = process.env.POLARS_FORCE_NDJSON_READ_SIZE;
if (v !== undefined && (!/^\d+$/.test(v.trim()) || parseInt(v, 10) === 0)) {
throw new Error(`POLARS_FORCE_NDJSON_READ_SIZE must be a positive integer (lines), got: '${v}'`);
} Type guard
function isValidReadSize(v) { return v === undefined || (/^\d+$/.test(v.trim()) && Number(v.trim()) > 0); } Try / catch
try {
runNdjsonScan();
} catch (e) {
if (String(e).includes('POLARS_FORCE_NDJSON_READ_SIZE')) {
delete process.env.POLARS_FORCE_NDJSON_READ_SIZE;
runNdjsonScan();
} else throw e;
} Prevention
- The unit is lines — no 'K'/'lines' suffixes allowed.
- Unset the variable to let the distributor compute the read size.
- Add an env-var validation step to ingestion job entrypoints.
When it happens
Trigger: Constructing a streaming NDJSON scan with the variable set to '', '0', '64k', or any string that fails NonZeroUsize parsing.
Common situations: Tuning NDJSON ingestion by copying values like '10K' or '1000 lines' from a blog post; the unit suffix and empty value are the common mistakes.
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_NDJSON_CHUNK_PREFETCH_LIMIT
- invalid value for POLARS_CSV_CHUNK_SIZE
- invalid value for POLARS_LINES_CHUNK_PREFETCH_LIMIT
- invalid value for POLARS_MAX_CONCURRENT_SCANS
- invalid value for POLARS_NUM_READERS_PRE_INIT
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/22dc1f3a279a55a2.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-stream/src/nodes/io_sources/ndjson/line_batch_distributor.rs:141
prev_leftover: Buffer<u8>,
read_size: usize,
chunk_idx: usize,
finished: bool,
}
impl LineBatchProducer {
fn new(
mut reader: ByteSourceReader<ReaderSource>,
reverse: bool,
row_skipper: RowSkipper,
uncompressed_file_size_hint: Option<usize>,
use_prefetch_l2: bool,
verbose: bool,
) -> PolarsResult<Self> {
let fixed_read_size = std::env::var("POLARS_FORCE_NDJSON_READ_SIZE")
.map(|x| {
x.parse::<NonZeroUsize>().ok().unwrap_or_else(|| {
panic!("invalid value for POLARS_FORCE_NDJSON_READ_SIZE: {x}")
})
})
.ok();
let read_size = fixed_read_size
.map(NonZeroUsize::get)
.unwrap_or_else(ByteSourceReader::<ReaderSource>::initial_read_size);
if verbose {
eprintln!(
"[NDJson LineBatchDistributor]: \
n_rows_to_skip: {}, \
reverse: {reverse}, \
fixed_read_size: {fixed_read_size:?}",
row_skipper.cfg_n_rows_to_skip,
);
}
View on GitHub (pinned to fe841f959e)