BoundaryML/baml · error

Failed to fetch media bytes: {e:?}

Error message

Failed to fetch media bytes: {e:?}

What it means

Error while collecting response body bytes in media fetching: the HTTP request for a media URL succeeded at the transport level, but reading the response body stream failed (connection reset mid-body, timeout, or decoding error). The debug-formatted reqwest error in {e:?} carries the transport specifics; this fires from the base64-encoding path used when media is supplied as a remote URL.

Source

Thrown at engine/baml-runtime/src/internal/llm_client/traits/mod.rs:788

        }
    }
}

async fn to_base64_with_inferred_mime_type(
    ctx: &RuntimeContext,
    media_url: &MediaUrl,
) -> Result<(String, String)> {
    if let Some((mime_type, base64)) = as_base64(media_url.url.as_str()) {
        return Ok((base64.to_string(), mime_type.to_string()));
    }
    let response = match fetch_with_proxy(&media_url.url, ctx.proxy_url()).await {
        Ok(response) => response,
        Err(e) => return Err(anyhow::anyhow!("Failed to fetch media: {e:?}")),
    };
    if response.status().is_success() {
        let bytes = match response.bytes().await {
            Ok(bytes) => bytes,
            Err(e) => return Err(anyhow::anyhow!("Failed to fetch media bytes: {e:?}")),
        };
        let base64 = BASE64_STANDARD.encode(&bytes);
        // TODO: infer based on file extension?
        let mime_type = match infer::get(&bytes) {
            Some(t) => t.mime_type(),
            None => "application/octet-stream",
        }
        .to_string();
        Ok((base64, mime_type))
    } else {
        Err(anyhow::anyhow!(
            "Failed to fetch media: {} {}, {}",
            response.status(),
            media_url.url,
            response.text().await.unwrap_or_default(),
        ))
    }
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Retry the fetch — this is often transient
  2. Reduce media file size before uploading/serving it
  3. Use a base64 data URL instead of a remote URL to avoid the network read
  4. Increase any configured HTTP timeouts
Defensive patterns

Strategy: retry

Try / catch

try {
  await runBamlFn();
} catch (e) {
  if (/Failed to fetch media bytes/.test(String(e.message)) && retries < 3) {
    return runBamlFn(retries + 1); // transient mid-body network failure
  }
  throw e;
}

Prevention

When it happens

Trigger: Connection dropped or timed out mid-body while streaming the media response; server closed the connection before sending the full payload.

Common situations: Large media files over flaky connections; server-side timeouts on big downloads; interrupted proxies; transient network issues.

Related errors


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