{"record":{"id":"ffd940f6e74a2e17","repo":"JuliusBrussee/caveman","slug":"rewriter-decode-anthropic-response-w","errorCode":null,"errorMessage":"rewriter: decode anthropic response: %w","messagePattern":"rewriter: decode anthropic response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"rewriter/provider.go","lineNumber":92,"sourceCode":"\t\t\"anthropic-version\": anthropicVersion,\n\t})\n\tif err != nil {\n\t\treturn completion{}, err\n\t}\n\n\tvar decoded struct {\n\t\tContent []struct {\n\t\t\tType string `json:\"type\"`\n\t\t\tText string `json:\"text\"`\n\t\t} `json:\"content\"`\n\t\tStopReason string `json:\"stop_reason\"`\n\t\tUsage      struct {\n\t\t\tInput  int `json:\"input_tokens\"`\n\t\t\tOutput int `json:\"output_tokens\"`\n\t\t} `json:\"usage\"`\n\t}\n\tif err := json.Unmarshal(raw, &decoded); err != nil {\n\t\treturn completion{}, fmt.Errorf(\"rewriter: decode anthropic response: %w\", err)\n\t}\n\tvar text strings.Builder\n\tfor _, block := range decoded.Content {\n\t\tif block.Type == \"text\" {\n\t\t\ttext.WriteString(block.Text)\n\t\t}\n\t}\n\treturn completion{\n\t\ttext:         text.String(),\n\t\tstopReason:   decoded.StopReason,\n\t\tinputTokens:  decoded.Usage.Input,\n\t\toutputTokens: decoded.Usage.Output,\n\t}, nil\n}\n\nfunc (c *Client) completeOpenAI(ctx context.Context, user string, maxOutput int) (completion, error) {\n\tbody := map[string]any{\n\t\t\"model\":                 c.model,","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/rewriter/provider.go#L74-L110","documentation":"completeAnthropic successfully fetched a response from the Anthropic Messages API but json.Unmarshal failed to decode the body into the expected content-blocks/stop_reason/usage structure. The provider returned a 200 with a payload that is not the documented Anthropic completion shape (unexpected content-type, HTML error page, or API contract change).","triggerScenarios":"Thrown at rewriter/provider.go:92 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Log the raw body snippet to identify what the provider actually returned","Retry once in case of a transient gateway/error page with status 200","Update the decoder if Anthropic changed its response schema"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}