{"record":{"id":"592e0fe8e1a02648","repo":"micro/go-micro","slug":"failed-to-marshal-request-w","errorCode":null,"errorMessage":"failed to marshal request: %w","messagePattern":"failed to marshal request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":356,"sourceCode":"\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 usage(input, output int) ai.Usage {\n\treturn ai.Usage{InputTokens: input, OutputTokens: output, TotalTokens: input + output}\n}\n\n// callAPI makes an HTTP request to the Anthropic API\nfunc (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Response, 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/messages\"\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(\"x-api-key\", p.opts.APIKey)\n\thttpReq.Header.Set(\"anthropic-version\", \"2023-06-01\")\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)","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L338-L374","documentation":"Returned by Provider.callAPI when json.Marshal fails to serialize the request map to JSON. In practice this is nearly impossible for map[string]any built from strings/ints, so it usually indicates a non-serializable value (channel, func, cyclic structure) was placed into the request map.","triggerScenarios":"A caller-supplied option or metadata value placed into the req map is not JSON-encodable (e.g. a func, channel, or a value with a custom Marshaler returning an error), or json.Marshal returns an UnsupportedTypeError/UnsupportedValueError.","commonSituations":"Passing custom option structs into provider options where a nested value can't serialize; rarely hit in normal use since the request map holds plain scalars.","solutions":["Inspect the wrapped error: json.UnsupportedTypeError names the offending Go type.","Remove or replace non-serializable values (func, channel, complex) from request options/metadata before calling Generate.","Marshal the request yourself in a test (json.Marshal(req)) to reproduce and isolate the bad value.","Ensure metadata values are plain strings/numbers/bools/maps/slices."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func assertJSONSafe(v map[string]any) error {\n\t_, err := json.Marshal(v)\n\treturn err\n}","typeGuard":null,"tryCatchPattern":"resp, err := provider.Generate(ctx, prompt)\nif err != nil {\n\tvar typeErr *json.UnsupportedTypeError\n\tif errors.As(err, &typeErr) {\n\t\tlog.Printf(\"non-serializable type in request: %v\", typeErr.Type)\n\t}\n}","preventionTips":["Keep request option values limited to strings/numbers/bools/maps/slices","Never put func, channel, or cyclic values into provider options/metadata","Unit-test custom option structs with json.Marshal"],"tags":["json","serialization","anthropic"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}