BoundaryML/baml · error

failed to get JSON: %w

Error message

failed to get JSON: %w

What it means

sseResponse.JSON() invokes the underlying FFI object's "json" method through raw_objects.CallMethod. Any error returned by that bridge call is wrapped as "failed to get JSON: %w". This means the native side failed to produce or serialize the JSON payload for this SSE response, not that the caller misused the API.

Source

Thrown at engine/language_client_go/pkg/rawobjects_sse_response.go:44

func (s *sseResponse) Text() (string, error) {
	result, err := raw_objects.CallMethod(s, "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 (s *sseResponse) JSON() (any, error) {
	result, err := raw_objects.CallMethod(s, "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

  1. Inspect the wrapped cause (%w) with errors.Is/As to find the native-side failure reason.
  2. Align the baml Go module version with your BAML native runtime version.
  3. Try Text() instead of JSON() if the payload is plain text, or vice versa.
  4. Ensure the runtime/context that produced the SSEResponse is still alive when you call JSON().

Example fix

// before
val, err := sseResp.JSON()
if err != nil {
    return fmt.Errorf("sse json: %v", err)
}

// after: unwrap and fall back to Text()
val, err := sseResp.JSON()
if err != nil {
    text, terr := sseResp.Text()
    if terr != nil {
        return fmt.Errorf("sse json: %w (text fallback: %v)", err, terr)
    }
    _ = text
}
Defensive patterns

Strategy: try-catch

Validate before calling

if sseResp == nil {
    return errors.New("sse response is nil")
}

Try / catch

val, err := sseResp.JSON()
if err != nil {
    var target *baml.RawObjectError
    if errors.As(err, &target) {
        log.Printf("native cause: %v", target)
    }
    return fmt.Errorf("sse json failed: %w", err)
}

Prevention

When it happens

Trigger: Calling JSON() on an SSEResponse when the underlying "json" FFI method errors: the native runtime cannot deserialize/serialize the event payload, or the raw object handle is invalid/stale.

Common situations: Version mismatch between Go bindings and native BAML runtime; malformed SSE payload from the model provider that the native layer rejects; calling JSON() on a response object that has already been consumed or whose runtime was torn down.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


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