{"record":{"id":"8f4c81c7d8e54e9d","repo":"micro/go-micro","slug":"failed-to-marshal-request-w-8f4c81","errorCode":null,"errorMessage":"failed to marshal request: %w","messagePattern":"failed to marshal request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/openai/openai.go","lineNumber":300,"sourceCode":"\t\treturn nil, err\n\t}\n\treturn nil, io.EOF\n}\n\nfunc (s *openAIStream) Close() error {\n\tif s.closed {\n\t\treturn nil\n\t}\n\ts.closed = true\n\treturn s.body.Close()\n}\n\n// callAPI makes an HTTP request to the OpenAI API\nfunc (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Response, map[string]any, error) {\n\t// Marshal request\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\t// Build HTTP request\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\t// Set headers\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\n\n\t// Make request\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}","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/openai/openai.go#L282-L318","documentation":"Thrown in the OpenAI provider's non-streaming callAPI when json.Marshal(req) fails while serializing the request map for /v1/chat/completions. Because req is map[string]any, marshaling fails only if some value isn't JSON-encodable (func, channel, cyclic reference, unsupported numeric type like NaN via interface).","triggerScenarios":"Calling the provider's non-streaming Complete path where the req map passed to callAPI contains a non-serializable value — custom options, tool definitions built at runtime with bad types, or NaN/Inf floats.","commonSituations":"Large JSON payloads or special characters in prompts (e.g. unpaired surrogates) can also surface as marshal failures.","solutions":["Inspect the req map keys for values json cannot encode","Constrain request values to JSON-safe types (string, int, float64, bool, slices, maps)","Reject/convert NaN and Inf floats before building the map","Log the map (with a safe encoder) before marshaling to find the offending key"],"exampleFix":"// before\ntemp := math.NaN()\nreq[\"temperature\"] = temp // json.Marshal error\n// after\nif !math.IsNaN(temp) && !math.IsInf(temp, 0) {\n    req[\"temperature\"] = temp\n}","handlingStrategy":"validation","validationCode":"if b, err := json.Marshal(req); err != nil || !json.Valid(b) {\n    return fmt.Errorf(\"request not JSON-encodable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only put JSON-encodable values into the request map","Sanitize floats (reject NaN/Inf) before building requests","Unit-test request builders with json.Marshal round-trips"],"tags":["json","serialization","openai"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}