{"record":{"id":"4c23ee04bbf2969b","repo":"googleapis/mcp-toolbox","slug":"error-marshalling-message-w","errorCode":null,"errorMessage":"error marshalling message: %w","messagePattern":"error marshalling message: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tools/bigquery/bigqueryconversationalanalytics/bigqueryconversationalanalytics.go","lineNumber":347,"sourceCode":"\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\n\t\t}\n\n\t\tif processedMsg != nil {\n\t\t\tmessages = append(messages, processedMsg)\n\t\t}\n\t}\n\n\tvar acc strings.Builder\n\tfor i, msg := range messages {\n\t\tjsonBytes, err := json.Marshal(msg)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"error marshalling message: %w\", err)\n\t\t}\n\t\tacc.Write(jsonBytes)\n\t\tif i < len(messages)-1 {\n\t\t\tacc.WriteString(\"\\n\")\n\t\t}\n\t}\n\n\treturn acc.String(), nil\n}\n\n// extractDataResult attempts to find the result.data deep inside the generic map.\nfunc extractDataResult(msg map[string]any) map[string]any {\n\tsm, ok := msg[\"systemMessage\"].(map[string]any)\n\tif !ok {\n\t\treturn nil\n\t}\n\tdata, ok := sm[\"data\"].(map[string]any)\n\tif !ok {","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/tools/bigquery/bigqueryconversationalanalytics/bigqueryconversationalanalytics.go#L329-L365","documentation":"After processing all streamed messages, getStream re-marshals each processed map[string]any back to JSON to build newline-delimited output. Any per-message marshal failure produces this error. Since inputs are already map[string]any from valid JSON, this is nearly impossible in stock code and signals data corruption or non-serializable values injected by custom processing.","triggerScenarios":"Invoke on the tool when a processed message map contains a value encoding/json cannot serialize — realistically only after local modifications to formatDataRetreated/extractDataResult or similar processing functions inject unsupported values (channels, funcs, NaN floats).","commonSituations":"Custom forks adding computed fields with unsupported Go types; injected NaN/Inf floats from custom formatting logic; corrupted message maps from a modified processing pipeline.","solutions":["Audit any local modifications to the message-processing functions (formatDataRetrieved, extractDataResult) for values that json.Marshal cannot handle (NaN, Inf, channels, funcs).","Sanitize numeric results (replace NaN/Inf with null) before appending messages.","Rebuild against upstream toolbox code to rule out local drift, then retry."],"exampleFix":"// before: injected NaN float in processed message\nmsg[\"score\"] = math.NaN()\n// after\nif math.IsNaN(score) { msg[\"score\"] = nil } else { msg[\"score\"] = score }","handlingStrategy":"validation","validationCode":"for k, v := range msg {\n    if !isJSONSafe(v) {\n        return fmt.Errorf(\"field %q not JSON-serializable: %T\", k, v)\n    }\n}","typeGuard":"func isJSONSafe(v any) bool {\n    switch t := v.(type) {\n    case nil, string, bool, int, int64, float64:\n        if f, ok := t.(float64); ok && (math.IsNaN(f) || math.IsInf(f, 0)) {\n            return false\n        }\n        return true\n    case []any:\n        for _, e := range t { if !isJSONSafe(e) { return false } }\n        return true\n    case map[string]any:\n        for _, e := range t { if !isJSONSafe(e) { return false } }\n        return true\n    default:\n        return false\n    }\n}","tryCatchPattern":"result, err := tool.Invoke(ctx, params)\nif err != nil && strings.Contains(err.Error(), \"error marshalling message\") {\n    return fmt.Errorf(\"processed message contains non-serializable data: %w\", err)\n}","preventionTips":["Replace NaN/Inf floats with nil in custom formatting logic.","Unit-test the message processing pipeline end-to-end through json.Marshal.","Avoid adding channels/funcs/cyclic references to message maps."],"tags":["go","json","serialization","bigquery"],"backgroundTag":"json-marshal-failed","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"}