{"record":{"id":"99f4e3ae004f91fe","repo":"micro/go-micro","slug":"failed-to-marshal-stream-request-w","errorCode":null,"errorMessage":"failed to marshal stream request: %w","messagePattern":"failed to marshal stream request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":248,"sourceCode":"\t\tbreak\n\t}\n\n\treturn resp, nil\n}\n\n// Stream generates a streaming response from Anthropic's Messages SSE API.\nfunc (p *Provider) Stream(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (ai.Stream, error) {\n\tapiReq := map[string]any{\n\t\t\"model\":      p.opts.Model,\n\t\t\"max_tokens\": anthropicMaxTokens(p.opts),\n\t\t\"system\":     cacheableSystem(req.SystemPrompt, nil, p.opts.NoCache),\n\t\t\"messages\":   threadAnthropicMessages(req),\n\t\t\"stream\":     true,\n\t}\n\tapplyReasoningOptions(apiReq, p.opts)\n\treqBody, err := json.Marshal(apiReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal stream request: %w\", err)\n\t}\n\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, fmt.Errorf(\"failed to create stream request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Accept\", \"text/event-stream\")\n\thttpReq.Header.Set(\"x-api-key\", p.opts.APIKey)\n\thttpReq.Header.Set(\"anthropic-version\", \"2023-06-01\")\n\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"stream API request failed: %w\", err)\n\t}\n\tif httpResp.StatusCode != http.StatusOK {\n\t\tdefer httpResp.Body.Close()","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L230-L266","documentation":"The Anthropic streaming provider builds the API request body (model, messages, reasoning options, stream flag) and marshals it to JSON before POSTing to /v1/messages. If json.Marshal fails — essentially only via unsupported values in the request (e.g. invalid map keys, unencodable types introduced by custom options) — the error is wrapped and returned from Stream before any network call.","triggerScenarios":"Any value inside apiReq that encoding/json cannot marshal, such as a map with non-string keys introduced via custom provider options or a malformed custom field; structurally invalid request construction.","commonSituations":"Passing arbitrary metadata/options with non-string-keyed maps; injecting custom request fields of unsupported types; regressions after customizing provider options.","solutions":["Inspect the wrapped error to find the unencodable field; ensure option values are JSON-compatible (string-keyed maps, basic types).","Validate custom option payloads marshal on their own with json.Marshal before passing them to the provider.","Upgrade/verify the library version if the failure occurs without custom options (internal bug)."],"exampleFix":"// before\nopts := []ai.Option{ai.WithMetadata(map[int]string{1: \"x\"})} // unencodable keys\n// after\nopts := []ai.Option{ai.WithMetadata(map[string]string{\"env\": \"prod\"})}","handlingStrategy":"try-catch","validationCode":"if _, err := json.Marshal(customOptionsPayload); err != nil {\n    return fmt.Errorf(\"invalid option payload: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"stream, err := provider.Stream(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"failed to marshal stream request\") {\n    return fmt.Errorf(\"request contains non-JSON-encodable option values: %w\", err)\n}","preventionTips":["Ensure custom option maps use string keys and JSON-safe types.","Pre-validate custom payloads with json.Marshal before passing them to providers.","Test streaming requests with your full option set in CI."],"tags":["anthropic","streaming","json-marshal","provider"],"backgroundTag":"request-serialization-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}