pola-rs/polars · error

invalid POLARS_REGEX_SIZE_LIMIT

Error message

invalid POLARS_REGEX_SIZE_LIMIT

What it means

POLARS_REGEX_SIZE_LIMIT sets the regex crate's compiled-size limit for polars' LRU regex cache. get_size_limit() filters out empty values, then parses the remainder as usize; any non-empty, non-integer value fails to parse and this expect panics — at the latest when the first regex is compiled.

Source

Thrown at crates/polars-utils/src/regex_cache.rs:13

use std::cell::RefCell;

use regex::{Regex, RegexBuilder};

use crate::cache::LruCache;

fn get_size_limit() -> Option<usize> {
    Some(
        std::env::var("POLARS_REGEX_SIZE_LIMIT")
            .ok()
            .filter(|l| !l.is_empty())?
            .parse()
            .expect("invalid POLARS_REGEX_SIZE_LIMIT"),
    )
}

// Regex compilation is really heavy, and the resulting regexes can be large as
// well, so we should have a good caching scheme.
//
// TODO: add larger global cache which has time-based flush.

/// A cache for compiled regular expressions.
pub struct RegexCache {
    cache: LruCache<String, Regex>,
    size_limit: Option<usize>,
}

impl RegexCache {
    fn new() -> Self {
        Self {
            cache: LruCache::with_capacity(32),

View on GitHub (pinned to df599052da)

Solutions

  1. Set a plain integer byte count: export POLARS_REGEX_SIZE_LIMIT=10485760
  2. Unset the variable (or set it empty — empty is treated as unset) to use the default
  3. Check for hidden whitespace or units in env files and container definitions
  4. Validate tuning values in one place (deploy script) instead of ad hoc in each environment

Example fix

# before
export POLARS_REGEX_SIZE_LIMIT=10MB  # panic: invalid POLARS_REGEX_SIZE_LIMIT

# after
export POLARS_REGEX_SIZE_LIMIT=10485760
Defensive patterns

Strategy: validation

Validate before calling

import os
v = os.environ.get("POLARS_REGEX_SIZE_LIMIT")
if v:  # empty is treated as unset
    if not v.isdigit():
        raise ValueError("POLARS_REGEX_SIZE_LIMIT must be an integer byte count")

Prevention

When it happens

Trigger: Setting the variable to something like '10MB', '10_000_000' (underscores are not accepted by usize::from_str), or '-1', then using any regex feature (str.contains with regex, extract, replace) that initializes the RegexCache.

Common situations: Tuning memory limits with human-readable units; copying docker/compose env values with quotes or whitespace; regex-heavy ETL jobs where the panic only appears once a regex operation actually runs.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/2944c22f396bf642. Report an issue: GitHub.