BoundaryML/baml · info

write!() to `String` should never fail

Error message

write!() to `String` should never fail

What it means

Defensive unreachable panic in Uint8Array.to_hex: writing two lowercase hex characters per byte into a String via write!(). Formatted writes into String only fail if the underlying formatter errors, which String's Write impl never does. Hitting this indicates a Rust std-level impossibility.

Solutions

  1. No user action required; report to maintainers if ever observed.
  2. Check for unusual tooling/patched std builds.
Defensive patterns

Strategy: try-catch

Try / catch

std::panic::catch_unwind(|| uint8array_to_hex(bytes)).unwrap_or_else(|_| Err(InternalError))

Prevention

When it happens

Trigger: Not triggerable by user input; only if std::fmt::Write for String somehow errors, which cannot happen in practice.

Common situations: Never in production; would only appear if the Rust standard library itself were broken or the code was modified.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/837d16406da47d6d. Report an issue: GitHub.

Appendix: source

Thrown at baml_language/crates/bex_vm/src/package_baml/uint8array.rs:142

                    ),
                })?;
                let lo = parse_hex_digit(lo).ok_or(VmBamlError::InvalidArgument {
                    message: format!(
                        "uint8array.from_hex: invalid hex digit at position {}",
                        i * 2 + 1
                    ),
                })?;
                Ok(hi << 4 | lo)
            })
            .collect()
    }

    fn to_hex(uint8array: &[u8]) -> bex_str::BexStr {
        use std::fmt::Write;
        let mut s = String::with_capacity(uint8array.len() * 2);
        for &b in uint8array {
            let Ok(()) = write!(s, "{b:02x}") else {
                unreachable!("write!() to `String` should never fail");
            };
        }
        bex_str::BexStr::from(s)
    }

    fn from_base64(base64_str: &bex_str::BexStr) -> Result<Vec<u8>, VmRustFnError> {
        use base64::Engine;
        base64::engine::general_purpose::STANDARD
            .decode(base64_str.as_str())
            .map_err(|e| VmBamlError::InvalidArgument {
                message: format!("failed to decode base64: {e}"),
            })
            .map_err(VmRustFnError::BamlError)
    }

    fn to_base64(uint8array: &[u8]) -> bex_str::BexStr {
        use base64::Engine;
        bex_str::BexStr::from(base64::engine::general_purpose::STANDARD.encode(uint8array))

View on GitHub (pinned to bd85ce9dee)