{"record":{"id":"790e22bd8b94d676","repo":"micro/go-micro","slug":"failed-to-parse-response-w","errorCode":null,"errorMessage":"failed to parse response: %w","messagePattern":"failed to parse response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":400,"sourceCode":"\t}\n\tif httpResp.StatusCode != http.StatusOK {\n\t\treturn nil, nil, ai.NewHTTPError(httpResp, respBody)\n\t}\n\n\t// Parse response\n\tvar anthropicResp struct {\n\t\tContent []struct {\n\t\t\tType  string          `json:\"type\"`\n\t\t\tText  string          `json:\"text\"`\n\t\t\tID    string          `json:\"id\"`\n\t\t\tName  string          `json:\"name\"`\n\t\t\tInput json.RawMessage `json:\"input\"`\n\t\t} `json:\"content\"`\n\t\tStopReason string `json:\"stop_reason\"`\n\t}\n\n\tif err := json.Unmarshal(respBody, &anthropicResp); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to parse response: %w\", err)\n\t}\n\n\tresponse := &ai.Response{StopReason: anthropicResp.StopReason}\n\n\t// Extract text reply\n\tvar replyParts []string\n\tfor _, block := range anthropicResp.Content {\n\t\tif block.Type == \"text\" && block.Text != \"\" {\n\t\t\treplyParts = append(replyParts, block.Text)\n\t\t}\n\t}\n\tif len(replyParts) > 0 {\n\t\tresponse.Reply = strings.Join(replyParts, \"\\n\")\n\t}\n\n\t// Extract tool calls\n\tfor _, block := range anthropicResp.Content {\n\t\tif block.Type == \"tool_use\" {","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L382-L418","documentation":"Returned by Provider.callAPI when the 200 OK response body cannot be unmarshaled into the expected Anthropic response schema. The body was read successfully but its JSON shape doesn't match the struct (content array, stop_reason, etc.), indicating a schema/version mismatch or a non-Anthropic response.","triggerScenarios":"json.Unmarshal of respBody fails: endpoint behind BaseURL returns a different JSON shape (OpenAI-compatible proxy, gateway error JSON), Anthropic changes the response schema under a different anthropic-version, or the body is truncated HTML/JSON.","commonSituations":"Pointing BaseURL at an OpenAI-format proxy whose /v1/messages isn't Anthropic-shaped, pinning an old/new anthropic-version whose schema differs, or a captive portal returning HTML with 200.","solutions":["Log respBody on failure to see the actual payload shape returned.","Confirm the configured anthropic-version matches what your BaseURL endpoint actually serves; try pinning \"2023-06-01\".","If using a proxy/gateway, verify it forwards Anthropic-format /v1/messages responses, not OpenAI-format ones.","Update the library to a version whose response struct matches your API version."],"exampleFix":"// before\nanthropic.New(anthropic.WithBaseURL(\"https://openai-proxy.internal\")) // serves OpenAI schema\n// after\nanthropic.New(anthropic.WithBaseURL(\"https://anthropic-compatible-gateway.internal\"))","handlingStrategy":"validation","validationCode":"func looksAnthropicCompatible(base string) error {\n\t// issue a minimal well-formed probe request and assert the response shape\n\t// contains keys \"content\" and \"stop_reason\" before switching providers\n\treturn probeV1Messages(base)\n}","typeGuard":"func isSchemaErr(err error) bool {\n\tvar syn *json.SyntaxError\n\tvar te *json.UnmarshalTypeError\n\treturn err != nil && (errors.As(err, &syn) || errors.As(err, &te)) && strings.Contains(err.Error(), \"failed to parse response\")\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, prompt)\nif err != nil && isSchemaErr(err) {\n\tlog.Printf(\"unexpected response schema — check anthropic-version and gateway compatibility\")\n}","preventionTips":["Pin anthropic-version and keep the library in sync with it","Verify proxies/gateways return Anthropic-format /v1/messages responses, not OpenAI format","Log raw response bodies on parse failures","Probe new endpoints with a minimal request before production use"],"tags":["json","parsing","schema","anthropic","api-version"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}