{"record":{"id":"0c35672c1bcdf81a","repo":"googleapis/mcp-toolbox","slug":"error-unmarshaling-raw-message-w","errorCode":null,"errorMessage":"error unmarshaling raw message: %w","messagePattern":"error unmarshaling raw message: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tools/bigquery/bigqueryconversationalanalytics/bigqueryconversationalanalytics.go","lineNumber":317,"sourceCode":"\tif _, err := decoder.Token(); err != nil {\n\t\tif err == io.EOF {\n\t\t\treturn \"\", nil // Empty response is valid\n\t\t}\n\t\treturn \"\", fmt.Errorf(\"error reading start of json array: %w\", err)\n\t}\n\n\tfor decoder.More() {\n\t\tvar rawMsg json.RawMessage\n\t\tif err := decoder.Decode(&rawMsg); err != nil {\n\t\t\tif err == io.EOF {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\treturn \"\", fmt.Errorf(\"error decoding raw message: %w\", err)\n\t\t}\n\n\t\tvar msg map[string]any\n\t\tif err := json.Unmarshal(rawMsg, &msg); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"error unmarshaling raw message: %w\", err)\n\t\t}\n\n\t\tvar processedMsg map[string]any\n\t\tif dataResult := extractDataResult(msg); dataResult != nil {\n\t\t\t// 1. If it's a data result, format it.\n\t\t\tprocessedMsg = formatDataRetrieved(dataResult, maxRows)\n\t\t\tif dataMsgIdx >= 0 {\n\t\t\t\t// Replace previous data with a placeholder. Intermediate data results in a\n\t\t\t\t// stream are redundant and consume unnecessary tokens.\n\t\t\t\tmessages[dataMsgIdx] = map[string]any{\"Data Retrieved\": \"Intermediate result omitted\"}\n\t\t\t}\n\t\t\tdataMsgIdx = len(messages)\n\t\t} else if sm, ok := msg[\"systemMessage\"].(map[string]any); ok {\n\t\t\t// 2. If it's a system message, unwrap it.\n\t\t\tprocessedMsg = sm\n\t\t} else {\n\t\t\t// 3. Otherwise (e.g. error), pass it through raw.\n\t\t\tprocessedMsg = msg","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/tools/bigquery/bigqueryconversationalanalytics/bigqueryconversationalanalytics.go#L299-L335","documentation":"After a raw array element is decoded, getStream unmarshals it into map[string]any to inspect its message type. Failure here means the element was valid JSON but not a JSON object (e.g. a bare string, number, or array inside the outer array), which the code cannot interpret as a GDA message.","triggerScenarios":"Invoke on the tool when the GDA response array contains an element that is not a JSON object — only possible with an unexpected API contract change or a fake/mocked endpoint emitting non-object elements.","commonSituations":"Mock servers or integration fixtures with wrong element shapes; version drift where the API adds non-object sentinel elements; hand-crafted test payloads.","solutions":["Update the toolbox to the latest version so message parsing matches the current GDA API contract.","If using mocks/fixtures, make every array element a JSON object (e.g. {\"systemMessage\": {...}} or {\"dataResult\": {...}}).","Log the offending raw message to confirm its shape and compare against the documented GDA chat response schema."],"exampleFix":"// before: fixture element is a string\n[\"ok\"]\n// after: element must be an object\n[{\"systemMessage\": {\"text\": \"ok\"}}]","handlingStrategy":"validation","validationCode":"for _, el := range elements {\n    if el[0] != '{' {\n        return fmt.Errorf(\"array element is not a JSON object: %s\", el)\n    }\n}","typeGuard":"func isJSONObject(raw json.RawMessage) bool {\n    var m map[string]any\n    return json.Unmarshal(raw, &m) == nil && m != nil\n}","tryCatchPattern":"result, err := tool.Invoke(ctx, params)\nif err != nil && strings.Contains(err.Error(), \"error unmarshaling raw message\") {\n    return fmt.Errorf(\"unexpected GDA response element; upgrade toolbox or fix mock: %w\", err)\n}","preventionTips":["Keep the toolbox version current with the GDA API contract.","Ensure test fixtures/mocks emit JSON objects only.","Compare unexpected elements against the documented chat response schema."],"tags":["json","parsing","api-contract","bigquery"],"backgroundTag":"unexpected-response-shape","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}