BoundaryML/baml · error · RuntimeError
Failed deserializing HTTP body as JSON: {e:?}
Error message
Failed deserializing HTTP body as JSON:
{e:?} What it means
HTTPBody#json reads the body and parses it as JSON, then converts it to a Ruby value. If the body is not valid JSON (or cannot be read), a Ruby RuntimeError 'Failed deserializing HTTP body as JSON' with the debug error is raised.
Source
Thrown at engine/language_client_ruby/ext/ruby_ffi/src/types/request.rs:91
for byte in rb_self.inner.raw() {
array.push(*byte)?;
}
Ok(array)
}
pub fn text(ruby: &Ruby, rb_self: &Self) -> Result<String> {
rb_self.inner.text().map(String::from).map_err(|e| {
Error::new(
ruby.exception_runtime_error(),
format!("Failed to get text from HTTP body:\n{e:?}"),
)
})
}
pub fn json(ruby: &Ruby, rb_self: &Self) -> Result<magnus::Value> {
serde_magnus::serialize(&rb_self.inner.json().map_err(|e| {
Error::new(
ruby.exception_runtime_error(),
format!("Failed deserializing HTTP body as JSON:\n{e:?}"),
)
})?)
}
pub fn define_in_ruby(module: &RModule) -> Result<()> {
let cls = module.define_class("HTTPBody", class::object())?;
cls.define_method("raw", method!(HTTPBody::raw, 0))?;
cls.define_method("text", method!(HTTPBody::text, 0))?;
cls.define_method("json", method!(HTTPBody::json, 0))?;
Ok(())
}
}
View on GitHub (pinned to bd85ce9dee)
Solutions
- Check the response status and content-type before calling json.
- Read the body once; store the text and parse manually if you need both forms.
- Inspect the debug error in the message to distinguish read failure vs JSON parse failure.
Example fix
// before
data = response.body.json # fails on HTML error page
// after
body = response.body
if response.status == 200 && body.text.strip.start_with?('{', '[')
data = JSON.parse(body.text)
else
data = nil
end Defensive patterns
Strategy: validation
Validate before calling
raw = body.text rescue nil
parsed = raw && raw.strip.start_with?('{', '[') ? JSON.parse(raw) : nil Try / catch
begin data = body.json rescue RuntimeError => e data = nil end
Prevention
- Check HTTP status/content-type before parsing JSON.
- Don't reuse a body already consumed by text().
When it happens
Trigger: Calling json on an HTTPBody containing non-JSON content: empty body, HTML error pages, plain-text errors, or an already-consumed body.
Common situations: Hitting an upstream gateway/proxy error page (502 HTML), an endpoint that returns plain text, or parsing the same body after a prior text() call.
Understand the failure class
Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.
Related errors
- failed to convert Ruby object to JSON, errors were: {} Ruby
- Failed to get text from HTTP body: {e:?}
- failed to get JSON: %w
- failed to get JSON: %w
- No LLM response: {}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/d401d649f6e48e7e.
Report an issue: GitHub.