alibaba/open-code-review · error

llm request failed: %w

Error message

llm request failed: %w

What it means

This error wraps any failure of the LLM chat request issued by `ocr llm test` (llmClient.CompletionsWithCtx). It covers network errors, HTTP error statuses from the provider, authentication rejections, timeouts (default 30s or the task's configured timeout), and malformed responses. The %w wrapper keeps the provider-specific cause.

Source

Thrown at cmd/opencodereview/llm_cmd.go:103

	// retry report only describes ocr review.
	llmClient := llm.NewLLMClient(ep, nil)

	messages := make([]llm.Message, 0, len(task.Messages))
	for _, m := range task.Messages {
		messages = append(messages, llm.Message{Role: m.Role, Content: m.Content})
	}

	resp, err := func() (*llm.ChatResponse, error) {
		ctx, cancel := context.WithTimeout(context.Background(), timeout)
		defer cancel()
		return llmClient.CompletionsWithCtx(ctx, llm.ChatRequest{
			Model:     ep.Model,
			Messages:  messages,
			MaxTokens: 2048,
		})
	}()
	if err != nil {
		return fmt.Errorf("llm request failed: %w", err)
	}

	model := ep.Model
	if resp.Model != "" {
		model = resp.Model
	}
	fmt.Printf("Source: %s\n", ep.Source)
	if region, profile, ok := bedrockContext(llmClient); ok {
		// Bedrock has no configured URL — the region decides the host — so
		// report what was resolved instead. A request that reached the wrong
		// region otherwise fails in a way that looks like a bad model ID.
		fmt.Printf("Region:  %s\n", region)
		if profile != "" {
			fmt.Printf("Profile: %s\n", profile)
		} else {
			fmt.Printf("Profile: (from the ambient AWS chain)\n")
		}
	} else {

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Read the wrapped cause after 'llm request failed:' — it distinguishes auth, network, timeout, and HTTP-status problems.
  2. For 401/403: verify the API key with `curl` against the provider, then update it via `ocr config provider`.
  3. For connection errors: check the URL in config (protocol, /v1 suffix) and test reachability with curl; check proxy settings (HTTPS_PROXY).
  4. For timeouts: raise `timeout` in the test task config or fix the latency cause (wrong region, overloaded provider).
  5. Run `ocr llm providers` to confirm the provider name and default URL are correct.

Example fix

// before (wrong base URL)
[llm]
url = "https://api.openai.com"
// after
[llm]
url = "https://api.openai.com/v1"
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight: confirm endpoint reachable and key present before the request
if os.Getenv("API_KEY") == "" { return errors.New("API key not set") }
resp, err := http.Head(strings.TrimSuffix(ep.URL, "/") + "/models")

Try / catch

resp, err := llmClient.CompletionsWithCtx(ctx, req)
if err != nil {
    var netErr net.Error
    switch {
    case errors.As(err, &netErr) && netErr.Timeout():
        // retry with longer timeout
    case errors.Is(err, context.DeadlineExceeded):
        // raise timeout in task config
    default:
        // inspect HTTP status / auth message in wrapped error
    }
    return fmt.Errorf("llm request failed: %w", err)
}

Prevention

When it happens

Trigger: Running `ocr llm test` when CompletionsWithCtx returns an error: endpoint unreachable (DNS/connection refused), 401/403 invalid API key, 404 wrong model or path, 429 rate limit, or context deadline exceeded.

Common situations: Expired or wrong API key; wrong base URL (missing or extra /v1); corporate proxy/firewall blocking the endpoint; model name not available to the account; provider outage; slow provider exceeding the 30s timeout.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/580bfbb5a8c5e6ec. Report an issue: GitHub.