{"record":{"id":"580bfbb5a8c5e6ec","repo":"alibaba/open-code-review","slug":"llm-request-failed-w","errorCode":null,"errorMessage":"llm request failed: %w","messagePattern":"llm request failed: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/llm_cmd.go","lineNumber":103,"sourceCode":"\t// retry report only describes ocr review.\n\tllmClient := llm.NewLLMClient(ep, nil)\n\n\tmessages := make([]llm.Message, 0, len(task.Messages))\n\tfor _, m := range task.Messages {\n\t\tmessages = append(messages, llm.Message{Role: m.Role, Content: m.Content})\n\t}\n\n\tresp, err := func() (*llm.ChatResponse, error) {\n\t\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\t\tdefer cancel()\n\t\treturn llmClient.CompletionsWithCtx(ctx, llm.ChatRequest{\n\t\t\tModel:     ep.Model,\n\t\t\tMessages:  messages,\n\t\t\tMaxTokens: 2048,\n\t\t})\n\t}()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"llm request failed: %w\", err)\n\t}\n\n\tmodel := ep.Model\n\tif resp.Model != \"\" {\n\t\tmodel = resp.Model\n\t}\n\tfmt.Printf(\"Source: %s\\n\", ep.Source)\n\tif region, profile, ok := bedrockContext(llmClient); ok {\n\t\t// Bedrock has no configured URL — the region decides the host — so\n\t\t// report what was resolved instead. A request that reached the wrong\n\t\t// region otherwise fails in a way that looks like a bad model ID.\n\t\tfmt.Printf(\"Region:  %s\\n\", region)\n\t\tif profile != \"\" {\n\t\t\tfmt.Printf(\"Profile: %s\\n\", profile)\n\t\t} else {\n\t\t\tfmt.Printf(\"Profile: (from the ambient AWS chain)\\n\")\n\t\t}\n\t} else {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/llm_cmd.go#L85-L121","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped cause after 'llm request failed:' — it distinguishes auth, network, timeout, and HTTP-status problems.","For 401/403: verify the API key with `curl` against the provider, then update it via `ocr config provider`.","For connection errors: check the URL in config (protocol, /v1 suffix) and test reachability with curl; check proxy settings (HTTPS_PROXY).","For timeouts: raise `timeout` in the test task config or fix the latency cause (wrong region, overloaded provider).","Run `ocr llm providers` to confirm the provider name and default URL are correct."],"exampleFix":"// before (wrong base URL)\n[llm]\nurl = \"https://api.openai.com\"\n// after\n[llm]\nurl = \"https://api.openai.com/v1\"","handlingStrategy":"try-catch","validationCode":"// preflight: confirm endpoint reachable and key present before the request\nif os.Getenv(\"API_KEY\") == \"\" { return errors.New(\"API key not set\") }\nresp, err := http.Head(strings.TrimSuffix(ep.URL, \"/\") + \"/models\")","typeGuard":null,"tryCatchPattern":"resp, err := llmClient.CompletionsWithCtx(ctx, req)\nif err != nil {\n    var netErr net.Error\n    switch {\n    case errors.As(err, &netErr) && netErr.Timeout():\n        // retry with longer timeout\n    case errors.Is(err, context.DeadlineExceeded):\n        // raise timeout in task config\n    default:\n        // inspect HTTP status / auth message in wrapped error\n    }\n    return fmt.Errorf(\"llm request failed: %w\", err)\n}","preventionTips":["Verify API keys and model names with curl before wiring them into config.","Set a realistic timeout for slow providers.","Check HTTPS_PROXY/firewall rules in corporate environments.","Run `ocr llm test` as a smoke check in CI before review jobs."],"tags":["network","llm","api","timeout"],"backgroundTag":"llm-request-failed","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}