JuliusBrussee/caveman · error

rewriter: decode openai response

Error message

rewriter: decode openai response: %w

What it means

completeOpenAI got a 2xx from the OpenAI-compatible chat-completions endpoint but the body did not decode into the expected choices[]/usage structure. The endpoint is likely not actually OpenAI-compatible (different schema, proxied error JSON, or an HTML page returned with success status).

Solutions

  1. Inspect the raw response body to see the actual schema returned
  2. Verify the endpoint truly implements OpenAI chat-completions response format
  3. Update the decoder to match the provider's actual response shape
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at rewriter/provider.go:145 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/7ef6f543fc9dc435. Report an issue: GitHub.

Appendix: source

Thrown at rewriter/provider.go:145

	})
	if err != nil {
		return completion{}, err
	}

	var decoded struct {
		Choices []struct {
			Message struct {
				Content string `json:"content"`
			} `json:"message"`
			FinishReason string `json:"finish_reason"`
		} `json:"choices"`
		Usage struct {
			Prompt     int `json:"prompt_tokens"`
			Completion int `json:"completion_tokens"`
		} `json:"usage"`
	}
	if err := json.Unmarshal(raw, &decoded); err != nil {
		return completion{}, fmt.Errorf("rewriter: decode openai response: %w", err)
	}
	var text, stop string
	if len(decoded.Choices) > 0 {
		text = decoded.Choices[0].Message.Content
		stop = decoded.Choices[0].FinishReason
	}
	return completion{
		text:         text,
		stopReason:   stop,
		inputTokens:  decoded.Usage.Prompt,
		outputTokens: decoded.Usage.Completion,
	}, nil
}

// openAIAcceptsTemperature reports whether a chat-completions model still takes
// a caller-set temperature. The reasoning families (gpt-5*, o1/o3/o4*) do not.
func openAIAcceptsTemperature(model string) bool {
	name := strings.ToLower(strings.TrimSpace(model))

View on GitHub (pinned to 766dce6b13)