{"record":{"id":"13fd154fe65090e6","repo":"micro/go-micro","slug":"api-request-failed-w-13fd15","errorCode":null,"errorMessage":"API request failed: %w","messagePattern":"API request failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/openai/openai.go","lineNumber":317,"sourceCode":"\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\t// Build HTTP request\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + \"/v1/chat/completions\"\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to create request: %w\", err)\n\t}\n\n\t// Set headers\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\n\n\t// Make request\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"API request failed: %w\", err)\n\t}\n\tdefer httpResp.Body.Close()\n\n\t// Read response\n\trespBody, _ := io.ReadAll(httpResp.Body)\n\tif httpResp.StatusCode != http.StatusOK {\n\t\treturn nil, nil, ai.NewHTTPError(httpResp, respBody)\n\t}\n\n\t// Parse response\n\tvar chatResp struct {\n\t\tUsage struct {\n\t\t\tPromptTokens     int `json:\"prompt_tokens\"`\n\t\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\t\tTotalTokens      int `json:\"total_tokens\"`\n\t\t} `json:\"usage\"`\n\t\tChoices []struct {\n\t\t\tMessage struct {","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/openai/openai.go#L299-L335","documentation":"This error wraps the underlying transport error returned by http.DefaultClient.Do when sending a chat-completion request to the OpenAI API. The library uses it to signal that no HTTP response was received at all (DNS failure, connection refused, TLS problem, context cancellation, etc.). The original *url.Error is preserved via %w so callers can errors.As/Is into it.","triggerScenarios":"callAPI posts to <BaseURL>/v1/chat/completions and http.DefaultClient.Do returns a non-nil error: unreachable host, bad BaseURL, proxy failure, TLS handshake failure, or the caller's ctx was cancelled before the response arrived.","commonSituations":"OPENAI_BASE_URL typo or missing scheme (e.g. 'api.openai.com' without https://), offline environment or DNS failure, corporate proxy not configured, context deadline exceeded on long completions, TLS cert issues with self-hosted/proxied endpoints.","solutions":["Verify opts.BaseURL is a valid absolute URL with scheme, e.g. https://api.openai.com","Check network connectivity / proxy env vars (HTTPS_PROXY) from the machine running the code","Unwrap with errors.As to *url.Error and inspect err.Err/err.Unwrap for the root cause","Check that the ctx passed to the call has a sane deadline; increase it for long generations"],"exampleFix":"// before\nprovider := openai.NewProvider(openai.WithBaseURL(\"api.openai.com\"))\n// after\nprovider := openai.NewProvider(openai.WithBaseURL(\"https://api.openai.com\"))","handlingStrategy":"try-catch","validationCode":"u, err := url.Parse(cfg.BaseURL)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid BaseURL %q: %w\", cfg.BaseURL, err)\n}\nif _, err := net.LookupHost(u.Hostname()); err != nil {\n    return fmt.Errorf(\"host %q unreachable: %w\", u.Hostname(), err)\n}","typeGuard":"var urlErr *url.Error\nif errors.As(err, &urlErr) {\n    // urlErr.Op, urlErr.URL, urlErr.Err describe the transport failure\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    var urlErr *url.Error\n    if errors.As(err, &urlErr) {\n        return fmt.Errorf(\"openai transport failure for %s: %w\", urlErr.URL, urlErr.Err)\n    }\n    return err\n}","preventionTips":["Validate BaseURL with url.Parse at startup","Set explicit context timeouts appropriate for LLM latency","Configure proxy env vars explicitly in containers/CI","Add a health-check ping against the API host before traffic"],"tags":["network","http","openai","transport-error"],"backgroundTag":"request-transport-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}