{"record":{"id":"c77dc4218a4d2202","repo":"micro/go-micro","slug":"failed-to-marshal-request-w-c77dc4","errorCode":null,"errorMessage":"failed to marshal request: %w","messagePattern":"failed to marshal request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/gemini/gemini.go","lineNumber":287,"sourceCode":"\t}\n\tif err := s.scanner.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn nil, io.EOF\n}\n\nfunc (s *streamReader) Close() error {\n\tif s.closed {\n\t\treturn nil\n\t}\n\ts.closed = true\n\treturn s.body.Close()\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, \"/\") +\n\t\t\"/v1beta/models/\" + p.opts.Model + \":generateContent\"\n\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(\"x-goog-api-key\", 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()","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/gemini/gemini.go#L269-L305","documentation":"callAPI (used by Gemini's non-streaming methods) failed to json.Marshal the request map into the request body. As with the streaming variant, marshaling map[string]any fails only when a value of unsupported type (channel, func, cycle) entered the request construction. The wrapped error pinpoints the offending value.","triggerScenarios":"Calling a non-streaming Gemini method (Generate/Chat/etc.) when the built req map contains a non-JSON-encodable value.","commonSituations":"Non-serializable custom types flowing in through message content or options; cyclic references in user data attached to the request; internal library regression.","solutions":["Inspect the wrapped %w error and remove/convert the non-marshalable value at its source.","Ensure messages, tools, and options contain only JSON-safe types.","Test the request map with json.Marshal in isolation to reproduce and locate the bad field.","Update the library if standard inputs trigger this — it indicates a construction bug."],"exampleFix":"// before\nreq[\"systemInstruction\"] = someObject\n// after\nraw, err := json.Marshal(someObject)\nif err != nil {\n    return nil, nil, fmt.Errorf(\"systemInstruction not serializable: %w\", err)\n}\nvar safe any\n_ = json.Unmarshal(raw, &safe)\nreq[\"systemInstruction\"] = safe","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(req); err != nil {\n    return fmt.Errorf(\"request not serializable: %w\", err)\n}","typeGuard":"func jsonSafeMap(m map[string]any) bool {\n    b, err := json.Marshal(m)\n    return err == nil && json.Valid(b)\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to marshal request\") {\n        return fmt.Errorf(\"non-serializable value in request (client bug): %w\", err)\n    }\n    return err\n}","preventionTips":["Restrict request inputs to JSON-native Go types","Break reference cycles before attaching user data to requests","Add unit tests that marshal fully populated request objects"],"tags":["json","marshal","gemini"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}