BoundaryML/baml · error
failed to get JSON: %w
Error message
failed to get JSON: %w
What it means
HTTPBody.JSON() calls the underlying JS/runtime object's 'json' method via the raw-objects bridge. If the bridge call fails (the runtime object is invalid, detached, or its 'json' method raises), the error is wrapped with 'failed to get JSON'. This means the library could not extract a JSON representation of the HTTP response body.
Source
Thrown at engine/language_client_go/pkg/rawobjects_http_body.go:44
func (h *httpBody) Text() (string, error) {
result, err := raw_objects.CallMethod(h, "text", nil)
if err != nil {
return "", fmt.Errorf("failed to get text: %w", err)
}
text, ok := result.(string)
if !ok {
return "", fmt.Errorf("unexpected type for text: %T", result)
}
return text, nil
}
func (h *httpBody) JSON() (any, error) {
result, err := raw_objects.CallMethod(h, "json", nil)
if err != nil {
return nil, fmt.Errorf("failed to get JSON: %w", err)
}
return result, nil
}
View on GitHub (pinned to bd85ce9dee)
Solutions
- Check that the HTTPBody was obtained from a live HTTPRequest/HTTPResponse and read it before the runtime context is disposed
- Print/inspect the wrapped cause (%w) to see whether the underlying 'json' method raised or was missing
- If the body may not be valid JSON, prefer Text() and unmarshal yourself with error handling
- Re-run the request to get a fresh body instead of reusing a stale one
Example fix
// before
val, err := body.JSON()
// after
if txt, err := body.Text(); err == nil {
var val any
if jerr := json.Unmarshal([]byte(txt), &val); jerr != nil {
log.Fatalf("body is not valid JSON: %v", jerr)
}
} else {
log.Fatalf("failed to read body: %v", err)
} Defensive patterns
Strategy: try-catch
Validate before calling
// Go has no pre-check; ensure the body comes from a live dispatch:
if body == nil { return errors.New("no body available") } Type guard
func validBody(b HTTPBody) bool { return b != nil } Try / catch
val, err := body.JSON()
if err != nil {
var txt string
if terr := body.Text(); terr == nil { txt = terr2txt(txt, terr) }
return fmt.Errorf("JSON body unavailable: %w", err)
} Prevention
- Read the body inside the same request lifecycle/handler that produced it
- Fall back to Text() + json.Unmarshal when the body may be non-JSON
- Avoid caching HTTPBody objects across runtime invocations
- Log the wrapped cause to distinguish missing method vs runtime exception
When it happens
Trigger: Calling JSON() on an HTTPBody whose underlying raw object no longer has a callable 'json' method, the body object was garbage-collected/detached from the runtime, or the runtime method itself returned an error (e.g. body is not valid JSON).
Common situations: Consuming a response body after the request context was torn down; interop handlers returning objects without the expected 'json' method; runtime/script reloaded between request and body read.
Related errors
- failed to get body: %w
- invalid StreamStateType: %q
- failed to get ID: %w
- failed to get URL: %w
- failed to get method: %w
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/c475372f256ab290.
Report an issue: GitHub.