BoundaryML/baml · error

blob size does not fit usize

Error message

blob size does not fit usize

What it means

BlobRefV1.size_bytes is a 64-bit proto integer; converting it to usize for the internal BlobRef fails when the value is negative or larger than usize::MAX. The TryFrom<crate::value::pb::BlobRefV1> impl surfaces this as InvalidData with this message.

Source

Thrown at baml_language/crates/bex_events/src/value/record.rs:237

                ValueAvailability::Lost => crate::value::pb::ValueAvailability::Lost as i32,
            },
            original_size_bytes: value_ref
                .original_size_bytes
                .and_then(|value| u64::try_from(value).ok()),
            retained_size_bytes: value_ref
                .retained_size_bytes
                .and_then(|value| u64::try_from(value).ok()),
            diagnostic: value_ref.diagnostic.clone(),
        }
    }
}

impl TryFrom<crate::value::pb::BlobRefV1> for BlobRef {
    type Error = io::Error;

    fn try_from(value: crate::value::pb::BlobRefV1) -> Result<Self, Self::Error> {
        let size_bytes = usize::try_from(value.size_bytes).map_err(|_| {
            io::Error::new(io::ErrorKind::InvalidData, "blob size does not fit usize")
        })?;
        Ok(Self {
            algorithm: value.algorithm,
            digest: value.digest,
            size_bytes,
        })
    }
}

impl From<&BlobRef> for crate::value::pb::BlobRefV1 {
    fn from(value: &BlobRef) -> Self {
        Self {
            algorithm: value.algorithm.clone(),
            digest: value.digest.clone(),
            size_bytes: u64::try_from(value.size_bytes).unwrap_or(u64::MAX),
        }
    }
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Correct the writer so blob size_bytes is always a valid non-negative u64 within usize range
  2. Use a 64-bit reader build if blob sizes are legitimately near the limit
  3. Validate blob refs at ingest and drop records with implausible sizes
  4. Guard the conversion manually with usize::try_from(...).ok() and handle None upstream

Example fix

// before
let size_bytes = usize::try_from(value.size_bytes).map_err(...)?;
// after
let size_bytes = usize::try_from(value.size_bytes).unwrap_or(0); // or skip record when 0
Defensive patterns

Strategy: type-guard

Validate before calling

fn blob_size_ok(v: &pb::BlobRefV1) -> bool { v.size_bytes >= 0 && (v.size_bytes as u64) <= usize::MAX as u64 }

Type guard

fn valid_blob_ref(b: &pb::BlobRefV1) -> bool { usize::try_from(b.size_bytes).is_ok() }

Try / catch

BlobRef::try_from(raw).map_err(|e| if e.to_string().contains("blob size") { skip_blob(raw.digest) } else { e })

Prevention

When it happens

Trigger: Decoding a BlobRefV1 whose size_bytes was written as a negative number or a value exceeding the target platform's usize (notably 32-bit readers).

Common situations: Corrupted blob references, writer bugs storing sentinels, 32-bit builds reading traces produced on 64-bit hosts with very large blobs.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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