{"record":{"id":"5fa25fc8ceb188fe","repo":"BoundaryML/baml","slug":"failed-to-get-json-w-rawobjects-sse-response","errorCode":null,"errorMessage":"failed to get JSON: %w","messagePattern":"failed to get JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/pkg/rawobjects_sse_response.go","lineNumber":44,"sourceCode":"\nfunc (s *sseResponse) Text() (string, error) {\n\tresult, err := raw_objects.CallMethod(s, \"text\", nil)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get text: %w\", err)\n\t}\n\n\ttext, ok := result.(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"unexpected type for text: %T\", result)\n\t}\n\n\treturn text, nil\n}\n\nfunc (s *sseResponse) JSON() (any, error) {\n\tresult, err := raw_objects.CallMethod(s, \"json\", nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get JSON: %w\", err)\n\t}\n\n\treturn result, nil\n}\n","sourceCodeStart":26,"sourceCodeEnd":49,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_sse_response.go#L26-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the wrapped cause (%w) with errors.Is/As to find the native-side failure reason.","Align the baml Go module version with your BAML native runtime version.","Try Text() instead of JSON() if the payload is plain text, or vice versa.","Ensure the runtime/context that produced the SSEResponse is still alive when you call JSON()."],"exampleFix":"// before\nval, err := sseResp.JSON()\nif err != nil {\n    return fmt.Errorf(\"sse json: %v\", err)\n}\n\n// after: unwrap and fall back to Text()\nval, err := sseResp.JSON()\nif err != nil {\n    text, terr := sseResp.Text()\n    if terr != nil {\n        return fmt.Errorf(\"sse json: %w (text fallback: %v)\", err, terr)\n    }\n    _ = text\n}","handlingStrategy":"try-catch","validationCode":"if sseResp == nil {\n    return errors.New(\"sse response is nil\")\n}","typeGuard":null,"tryCatchPattern":"val, err := sseResp.JSON()\nif err != nil {\n    var target *baml.RawObjectError\n    if errors.As(err, &target) {\n        log.Printf(\"native cause: %v\", target)\n    }\n    return fmt.Errorf(\"sse json failed: %w\", err)\n}","preventionTips":["Always unwrap with errors.As/Is to surface the native-side cause before reporting.","Keep baml Go bindings and BAML CLI versions in lockstep.","Do not use SSEResponse objects after their runtime/context has been torn down."],"tags":["go","ffi","sse","json"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}