headroomlabs-ai/headroom · error · MagikaDetectorError

magika session init failed: {0}

Error message

magika session init failed: {0}

What it means

Brotli is installed but brotli.decompress() raised — the body is not a valid brotli stream (helpers.py:2436). Usually caused by mislabeled, truncated, or double-encoded payloads.

Source

Thrown at crates/headroom-core/src/transforms/magika_detector.rs:311

fn dedup_existing_files(paths: Vec<PathBuf>) -> Vec<PathBuf> {
    let mut out = Vec::new();
    for path in paths {
        if path.is_file() && !out.iter().any(|seen| seen == &path) {
            out.push(path);
        }
    }
    out
}

/// Errors from the magika detector. Wraps the underlying `magika::Error`
/// so callers can match on whether init or inference broke without
/// pulling magika types into their imports.
#[derive(Debug, Error)]
pub enum MagikaDetectorError {
    /// One-time session initialization failed (model load, ONNX init).
    /// Once we hit this, every subsequent call also fails — there is no
    /// retry path here. The router should surface and stop.
    #[error("magika session init failed: {0}")]
    Init(String),

    /// Inference call failed for this input. Usually transient; future
    /// calls may succeed. The error message is the magika-side text;
    /// we don't try to wrap it.
    #[error("magika inference failed: {0}")]
    Inference(String),

    /// Singleton lock was poisoned (a previous holder panicked while
    /// holding it). The detector is unusable until the process
    /// restarts. We don't auto-recover — a panicked detector means
    /// something is corrupt and continuing would mask it.
    #[error("magika session lock poisoned")]
    Poisoned,
}

/// One-process singleton holding the magika session. Lazily
/// initialized on first call to [`magika_detect`].

View on GitHub (pinned to 322425c43b)

Solutions

  1. Check the body standalone: python -c "import brotli; brotli.decompress(open('body.bin','rb').read())"
  2. Ensure the client compresses exactly once with brotli.compress() and sets Content-Encoding: br only then
  3. Fix any fronting proxy that alters bodies but forwards stale encoding headers

Example fix

# before
requests.post(url, data=plain_payload, headers={"Content-Encoding": "br"})

# after
requests.post(url, data=brotli.compress(plain_payload), headers={"Content-Encoding": "br"})
Defensive patterns

Strategy: try-catch

Validate before calling

import brotli
brotli.decompress(compressed_body)  # raises before send if invalid

Try / catch

try:
    body = await _read_request_body_bytes(request)
except ValueError as exc:
    return JSONResponse({"error": {"message": str(exc)}}, status_code=400)

Prevention

When it happens

Trigger: Body labeled 'br' that is actually gzip/plain; an upload cut mid-stream so the brotli frame is incomplete; an intermediary rewriting the body without updating Content-Encoding.

Common situations: Header/body mismatches from gateways; client bugs compressing twice; flaky connections producing partial bodies under load.

Related errors


AI-assisted analysis of headroomlabs-ai/headroom@322425c43b (2026-08-15). Data as JSON: /api/errors/40558435b6f160ea. Report an issue: GitHub.