JuliusBrussee/caveman · error

rewriter: decode anthropic response: %w

Error message

rewriter: decode anthropic response: %w

What it means

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).

Source

Thrown at rewriter/provider.go:92

		"anthropic-version": anthropicVersion,
	})
	if err != nil {
		return completion{}, err
	}

	var decoded struct {
		Content []struct {
			Type string `json:"type"`
			Text string `json:"text"`
		} `json:"content"`
		StopReason string `json:"stop_reason"`
		Usage      struct {
			Input  int `json:"input_tokens"`
			Output int `json:"output_tokens"`
		} `json:"usage"`
	}
	if err := json.Unmarshal(raw, &decoded); err != nil {
		return completion{}, fmt.Errorf("rewriter: decode anthropic response: %w", err)
	}
	var text strings.Builder
	for _, block := range decoded.Content {
		if block.Type == "text" {
			text.WriteString(block.Text)
		}
	}
	return completion{
		text:         text.String(),
		stopReason:   decoded.StopReason,
		inputTokens:  decoded.Usage.Input,
		outputTokens: decoded.Usage.Output,
	}, nil
}

func (c *Client) completeOpenAI(ctx context.Context, user string, maxOutput int) (completion, error) {
	body := map[string]any{
		"model":                 c.model,

View on GitHub (pinned to 766dce6b13)

Solutions

  1. Log the raw body snippet to identify what the provider actually returned
  2. Retry once in case of a transient gateway/error page with status 200
  3. Update the decoder if Anthropic changed its response schema
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at rewriter/provider.go:92 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18). Data as JSON: /api/errors/ffd940f6e74a2e17. Report an issue: GitHub.