pola-rs/polars · critical
failed to encode spill file for
Error message
failed to encode spill file for '{context_id}': {e} What it means
During out-of-core spill, the DataFrame is encoded to an in-memory IPC (Arrow IPC with optional zstd) buffer via writer.finish(). If encoding fails, the code panics with 'failed to encode spill file' plus the context id and error. Spilling is treated as infallible by design, so any IPC write error aborts the process.
Solutions
- Check/correct the configured OOC compression level (clvl) — an out-of-range zstd level is the most likely cause; set it to a valid 1–22 value or 0 to disable compression.
- Cast or drop columns with dtypes the IPC writer cannot encode before enabling OOC spill.
- Upgrade polars: newer versions may support the dtype or handle the writer error gracefully.
- Reduce memory pressure so OOC spill isn't needed (chunk sizes, streaming parameters), or report the inner error to polars if it looks like a bug.
Example fix
// before POLARS_OOC_COMPRESSION_LEVEL=99 // invalid zstd level // after POLARS_OOC_COMPRESSION_LEVEL=3 // valid zstd level (1-22), or 0 to disable
Defensive patterns
Strategy: validation
Validate before calling
// validate config and dtypes before enabling OOC spill
let clvl: i64 = parse_config("POLARS_OOC_COMPRESSION_LEVEL");
assert!((0..=22).contains(&clvl), "zstd level must be 0-22");
// ensure all columns use IPC-encodable dtypes before spilling Try / catch
// spill panics on encode failure; protect the driver loop
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run_ooc_query())).map_err(|_|
"spill encode failed: check compression level config and column dtypes") Prevention
- Keep OOC compression level within 1–22 (or 0 for none).
- Restrict spilled frames to IPC-supported dtypes; cast exotic columns up front.
- Keep polars updated for IPC writer fixes.
- Log the full panic message including the inner error to diagnose the writer failure.
When it happens
Trigger: Calling spill() (crates/polars-ooc/src/spill_frame.rs:41) when IpcWriter::finish fails — typically due to an invalid/unsupported compression level configured via the OOC compression setting (ZstdLevel::try_new edge cases), IPC-unsupported column types/dtypes in the frame, or an internal writer error.
Common situations: Out-of-core streaming queries on frames containing dtypes the IPC writer cannot serialize; misconfigured compression level env/config for zstd; corrupted execution state under memory pressure.
Understand the failure class
Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.
Related errors
- activate 'decompress' feature
- could not read
- failed to create spill directory
- failed to read spill file
- failed to serialize schema hashes file
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/20ff2f7b4a8bd52b.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-ooc/src/spill_frame.rs:41
self.estimated_size()
}
async fn spill(&self, context_id: &str) -> Self::Spilled {
let mut df = self.clone();
let context_id = context_id.to_owned();
// Encode in the current task (on computational executor).
let mut buf = Vec::new();
let mut writer = IpcStreamWriter::new(&mut buf).with_compat_level(CompatLevel::newest());
let clvl = polars_config::config().ooc_spill_compression_level();
if clvl > 0 {
let zstd_lvl = ZstdLevel::try_new(clvl.try_into().unwrap()).unwrap();
writer = writer.with_compression(Some(IpcCompression::ZSTD(zstd_lvl)));
}
writer
.finish(&mut df)
.unwrap_or_else(|e| panic!("failed to encode spill file for '{context_id}': {e}",));
// Do file creation / writing on tokio.
let file = ASYNC
.spawn(async move {
let size = buf.len() as u64;
let spill_file = SpillFile::new(&context_id, "ipc", size);
if BYTES_SPILLED_TO_DISK.fetch_add(size).saturating_add(size)
> polars_config::config().ooc_disk_budget_bytes()
{
spill_file.creation_aborted();
polars_error::abort::polars_abort_ooc_out_of_disk();
}
match tokio::fs::write(spill_file.path(), buf).await {
Ok(()) => spill_file,
Err(e) => {
let msg = format!(
"failed to create spill file '{}': {e}",
spill_file.path().display()View on GitHub (pinned to fe841f959e)