micro/go-micro · error
failed to marshal request: %w
Error message
failed to marshal request: %w
What it means
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.
Source
Thrown at ai/anthropic/anthropic.go:356
func (s *streamReader) Close() error {
if s.closed {
return nil
}
s.closed = true
return s.body.Close()
}
func usage(input, output int) ai.Usage {
return ai.Usage{InputTokens: input, OutputTokens: output, TotalTokens: input + output}
}
// callAPI makes an HTTP request to the Anthropic API
func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Response, any, error) {
// Marshal request
reqBody, err := json.Marshal(req)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Build HTTP request
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/messages"
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
if err != nil {
return nil, nil, fmt.Errorf("failed to create request: %w", err)
}
// Set headers
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("x-api-key", p.opts.APIKey)
httpReq.Header.Set("anthropic-version", "2023-06-01")
// Make request
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return nil, nil, fmt.Errorf("API request failed: %w", err)View on GitHub (pinned to 24529f1404)
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.
Defensive patterns
Strategy: validation
Validate before calling
func assertJSONSafe(v map[string]any) error {
_, err := json.Marshal(v)
return err
} Try / catch
resp, err := provider.Generate(ctx, prompt)
if err != nil {
var typeErr *json.UnsupportedTypeError
if errors.As(err, &typeErr) {
log.Printf("non-serializable type in request: %v", typeErr.Type)
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- failed to parse stream chunk: %w
- failed to parse response: %w
- failed to marshal stream request: %w
- failed to marshal request: %w
- failed to marshal request: %w
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/592e0fe8e1a02648.
Report an issue: GitHub.