{"record":{"id":"b4a98aabcc5165ff","repo":"micro/go-micro","slug":"failed-to-marshal-request-w-b4a98a","errorCode":null,"errorMessage":"failed to marshal request: %w","messagePattern":"failed to marshal request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/ollama/ollama.go","lineNumber":229,"sourceCode":"\t\t\t\t\"content\":    followUpRaw.content,\n\t\t\t\t\"tool_calls\": followUpRaw.toolCalls,\n\t\t\t})\n\t\t\tcontinue\n\t\t}\n\n\t\tif followUpResp.Reply != \"\" {\n\t\t\tresp.Answer = followUpResp.Reply\n\t\t}\n\t\tbreak\n\t}\n\n\treturn resp, nil\n}\n\nfunc (p *Provider) callOpenAI(ctx context.Context, req map[string]any) (*ai.Response, *rawChatMessage, error) {\n\treqBody, err := json.Marshal(req)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + p.chatPath()\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to create request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\tif p.opts.APIKey != \"\" {\n\t\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\n\t}\n\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"API request failed: %w\", err)\n\t}\n\tdefer httpResp.Body.Close()\n","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/ollama/ollama.go#L211-L247","documentation":"This error wraps json.Marshal failure when encoding the OpenAI-compatible chat request map in the Ollama provider's callOpenAI. The request is built as map[string]any, so marshalling should rarely fail, but non-JSON-serializable values (channels, funcs, invalid float NaN/Inf) in the map would trigger it.","triggerScenarios":"json.Marshal(req) in callOpenAI (called from generateOpenAI) fails because the request map contains a value json cannot encode — e.g. a caller-supplied option value inserted into the map that is a channel, function, cyclic reference, or NaN/Inf number.","commonSituations":"Passing options like tools or messages built with unsupported types; NaN/Inf token or temperature values from computed defaults; cyclic structures in user-supplied metadata.","solutions":["Inspect the request map for values not JSON-serializable (channels, funcs, cycles, NaN/Inf floats)","Sanitize floats (temperature, top_p) to avoid NaN/Inf before building the map","Log the req map keys and value types to identify the offending entry","Build the request as a typed struct instead of map[string]any to catch this at compile time"],"exampleFix":"// before\nreqBody, err := json.Marshal(req)\nif err != nil {\n    return nil, nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n}\n// after\nreqBody, err := json.Marshal(req)\nif err != nil {\n    return nil, nil, fmt.Errorf(\"failed to marshal request: %w (keys: %v)\", err, mapKeys(req))\n}","handlingStrategy":"validation","validationCode":"// validate options before building the request\nfor k, v := range opts {\n    if err := json.Unmarshal(mustJSON(v), new(json.RawMessage)); err != nil {\n        return fmt.Errorf(\"option %q is not JSON-serializable: %w\", k, err)\n    }\n}\nif math.IsNaN(opts.Temperature) || math.IsInf(opts.Temperature, 0) {\n    return fmt.Errorf(\"temperature must be a finite number\")\n}","typeGuard":"func isJSONSerializable(v any) bool {\n    _, err := json.Marshal(v)\n    return err == nil\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to marshal request\") {\n        // inspect request fields for channels/funcs/cycles/NaN\n    }\n    return err\n}","preventionTips":["Only pass plain JSON-compatible values in options","Sanitize computed floats for NaN/Inf","Avoid attaching cyclic or func-valued fields to requests","Prefer typed structs over map[string]any for requests"],"tags":["json","marshalling","ollama"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}