pydantic/monty · error

identity enum payload fits in 14 bytes

Error message

identity enum payload fits in 14 bytes

What it means

An internal invariant `expect` in `fixed_serde_payload`, which serializes small identity-enum payloads (via postcard) into a fixed 16-byte stack buffer so they can be packed into a `u128`. The assertion states every serialized variant payload fits within `MAX_FIXED_BYTES` (14); the `Identity::bytes_size` mapping in the same file is what keeps variants within that bound. It fires only if a new variant or payload grows past the fixed size — a compile-time-adjacent design invariant, not a runtime condition reachable from Python code.

Source

Thrown at crates/monty/src/identity.rs:153

            Self::DefFunction(_) => 12,
            Self::Marker(_) => 14,
            Self::Property(_) => 15,
            Self::Heap(_) => 16,
        }
    }
}

/// Returns a prefix-preserving integer for a short byte sequence.
fn bytes_payload(bytes: &[u8]) -> u128 {
    bytes
        .iter()
        .fold(1, |payload, byte| (payload << u8::BITS) | u128::from(*byte))
}

/// Serializes a small enum payload into a stack buffer and preserves its length.
fn fixed_serde_payload(value: &impl Serialize) -> u128 {
    let mut buffer = [0; MAX_FIXED_BYTES];
    let serialized = postcard::to_slice(value, &mut buffer).expect("identity enum payload fits in 14 bytes");
    bytes_payload(serialized)
}

/// Maps signed integers into `u64` while keeping small magnitudes compact.
fn zigzag_i64(value: i64) -> u64 {
    if value >= 0 {
        value.unsigned_abs() << 1
    } else {
        ((value.unsigned_abs() - 1) << 1) | 1
    }
}

/// Reorders float fields so common powers of two have compact identities.
fn compact_float_bits(bits: u64) -> u64 {
    const MANTISSA_BITS: u8 = 52;
    const EXPONENT_MASK: u64 = (1 << 11) - 1;
    const MANTISSA_MASK: u64 = (1 << MANTISSA_BITS) - 1;

View on GitHub (pinned to adc986b362)

Solutions

  1. This expect enforces the invariant that every identity enum variant's serialized payload is at most MAX_FIXED_BYTES (14); a new variant larger than that must either shrink its payload or raise MAX_FIXED_BYTES with matching updates to the per-variant size table.
  2. At runtime no recovery exists — the payload cannot be truncated without corrupting id() uniqueness; fix the variant definition instead.

When it happens

Trigger: Thrown at crates/monty/src/identity.rs:153 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pydantic/monty@adc986b362 (2026-09-13). Data as JSON: /api/errors/4892acacda83da6b. Report an issue: GitHub.