{"record":{"id":"bac0c1d670062e7a","repo":"micro/go-micro","slug":"failed-to-marshal-request-w-bac0c1","errorCode":null,"errorMessage":"failed to marshal request: %w","messagePattern":"failed to marshal request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/together/together.go","lineNumber":161,"sourceCode":"\t}\n\n\treturn resp, nil\n}\n\n// maxToolRounds bounds the tool-execution loop in a single Generate. Each\n// round is a model call plus the tools it asks for, so this is the ceiling on\n// one question's cost as well as its length; it is high enough that no honest\n// piece of multi-step work reaches it.\nconst maxToolRounds = 12\n\nfunc (p *Provider) Stream(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (ai.Stream, error) {\n\treturn openaiapi.Stream(ctx, p.opts, req, \"/v1/chat/completions\")\n}\n\nfunc (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Response, map[string]any, 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, \"/\") + \"/v1/chat/completions\"\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\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\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\n\trespBody, _ := io.ReadAll(httpResp.Body)","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/together/together.go#L143-L179","documentation":"The Together AI provider's callAPI could not JSON-marshal the request map before POSTing to /v1/chat/completions. Since the map holds plain JSON-able values, this practically means a non-serializable value (NaN/Inf float, channel, func, cyclic reference) entered the request map.","triggerScenarios":"The req map[string]any built for the Together chat API contains a value json.Marshal rejects — e.g. NaN produced by numeric post-processing of temperature/top_p, or a custom field with an unsupported type.","commonSituations":"Dynamically assembled sampling params where a computation yields NaN (0/0 division), callers passing structured objects that contain funcs or channels into the request map.","solutions":["Validate sampling params: reject NaN/Inf for temperature/top_p before building the map","Sanitize or default non-finite floats (math.IsNaN/IsInf) when constructing the request","Marshal the request map in a test to reproduce the exact error message","Ensure only JSON-representable values are placed into the request map"],"exampleFix":"// before\ntemp := computeTemperature() // may be NaN\nreq[\"temperature\"] = temp\n// after\nif math.IsNaN(temp) || math.IsInf(temp, 0) { temp = 0.7 }\nreq[\"temperature\"] = temp","handlingStrategy":"validation","validationCode":"func sanitizeParams(params map[string]any) {\n    for k, v := range params {\n        if f, ok := v.(float64); ok && (math.IsNaN(f) || math.IsInf(f, 0)) {\n            params[k] = nil // or a default\n        }\n    }\n}\n// call before passing the map to the provider","typeGuard":"func isJSONSafeFloat(f float64) bool {\n    return !math.IsNaN(f) && !math.IsInf(f, 0)\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"failed to marshal request\") {\n    return fmt.Errorf(\"non-JSON value in Together request map: %w\", err)\n}","preventionTips":["Check temperature/top_p for NaN/Inf after any computation","Restrict request maps to JSON-primitive types","Add a marshal unit test for every request shape","Default non-finite sampling params to library defaults"],"tags":["json","serialization","together","chat"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}