pola-rs/polars · error

Writing to JSON

Error message

Writing {:?} to JSON

What it means

The serializer's match over ArrowDataType is exhaustive only for types polars-json supports; anything else reaches `todo!("Writing {:?} to JSON", other)`, which panics with a not-yet-implemented message naming the type. It marks an unimplemented serialization path, not corrupt data.

Solutions

  1. Cast unsupported columns to a JSON-compatible type (String, Int64, Float64, etc.) before writing.
  2. Drop the unsupported column from the DataFrame before serialization.
  3. Implement the missing serializer arm in crates/polars-json/src/json/write/serialize.rs if you own the build.
  4. Upgrade polars — the todo may be implemented in a newer release.

Example fix

// before
let df: DataFrame = ...; // contains Interval column
writer.serialize(&df);
// after
let df = df.drop("interval_col").unwrap(); // or cast to a supported type
writer.serialize(&df);
Defensive patterns

Strategy: validation

Validate before calling

const JSON_SERIALIZABLE: &[ArrowDataType] = &[
    ArrowDataType::Null, ArrowDataType::Boolean, ArrowDataType::Int64, ArrowDataType::Float64,
    ArrowDataType::Utf8, ArrowDataType::LargeUtf8, /* ... supported set for your polars version */
];
// verify each column's dtype against the supported set (or try a cast fallback) before writing

Type guard

fn is_json_supported(dt: &ArrowDataType) -> bool {
    !matches!(dt, ArrowDataType::Interval(_) | ArrowDataType::Union(_, _) | ArrowDataType::FixedSizeBinary(_))
}

Try / catch

// panics are not catchable via Result; use catch_unwind if you must survive foreign schemas
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| writer.serialize(&df)));
if result.is_err() { /* fall back to dropping/casting exotic columns */ }

Prevention

When it happens

Trigger: Passing a DataFrame/column containing an Arrow data type polars-json cannot serialize yet (e.g. Interval, FixedSizeBinary, Union, Dictionary depending on version) into the JSON writer via new/serialize/serialize_to_utf8 or nested struct/list serializers.

Common situations: Serializing data produced by other Arrow tooling with exotic types; upgrading arrow crates introduces a new type that flows through untyped channels; users constructing unusual schemas manually.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/41cdfa4535d9c924. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-json/src/json/write/serialize.rs:634

                convert,
                offset,
                take,
            )
        },
        ArrowDataType::Time64(tu) => {
            let convert = match tu {
                TimeUnit::Nanosecond => time64ns_to_time,
                tu => panic!("Invalid time unit '{tu:?}' for Time."),
            };
            time_serializer(
                array.as_any().downcast_ref().unwrap(),
                convert,
                offset,
                take,
            )
        },
        ArrowDataType::Null => null_serializer(array.len(), offset, take),
        other => todo!("Writing {:?} to JSON", other),
    }
}

fn serialize_item<'a>(
    buffer: &mut Vec<u8>,
    record: impl Iterator<Item = (&'a str, &'a [u8])>,
    is_first_row: bool,
) {
    if !is_first_row {
        buffer.push(b',');
    }
    buffer.push(b'{');
    let mut first_item = true;
    for (key, value) in record {
        if !first_item {
            buffer.push(b',');
        }
        first_item = false;

View on GitHub (pinned to fe841f959e)