{"record":{"id":"c0ba7e3045e0905e","repo":"router-for-me/CLIProxyAPI","slug":"models-request-failed-with-status-d-s","errorCode":null,"errorMessage":"models request failed with status %d: %s","messagePattern":"models request failed with status (.+?): (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/fetch_codex_models/main.go","lineNumber":274,"sourceCode":"\t\t\thttpClient.Transport = transport\n\t\t}\n\t}\n\n\thttpResp, errDo := httpClient.Do(httpReq)\n\tif errDo != nil {\n\t\treturn nil, 0, errDo\n\t}\n\n\tbodyBytes, errRead := io.ReadAll(httpResp.Body)\n\tif errClose := httpResp.Body.Close(); errClose != nil && errRead == nil {\n\t\terrRead = errClose\n\t}\n\tif errRead != nil {\n\t\treturn nil, 0, errRead\n\t}\n\n\tif httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices {\n\t\treturn nil, 0, fmt.Errorf(\"models request failed with status %d: %s\", httpResp.StatusCode, strings.TrimSpace(string(bodyBytes)))\n\t}\n\n\tcount, errCount := countModels(bodyBytes)\n\tif errCount != nil {\n\t\treturn nil, 0, errCount\n\t}\n\treturn bodyBytes, count, nil\n}\n\nfunc codexModelsURL(clientVersion string) (string, error) {\n\tu, err := url.Parse(codexModelsBaseURL + codexModelsPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif strings.TrimSpace(clientVersion) != \"\" {\n\t\tq := u.Query()\n\t\tq.Set(\"client_version\", strings.TrimSpace(clientVersion))\n\t\tu.RawQuery = q.Encode()","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/cmd/fetch_codex_models/main.go#L256-L292","documentation":"fetchModels in cmd/fetch_codex_models/main.go performs GET on the Codex models endpoint and, when the HTTP status is outside 2xx, returns `models request failed with status %d: %s` including the trimmed response body. The wrapped body usually distinguishes auth failure (401/403), bad client version (400), rate limiting (429), or upstream outage (5xx).","triggerScenarios":"Access token expired or invalid (401); account not entitled to Codex models (403); wrong client_version query param (400); too many requests (429); upstream OpenAI endpoint error (5xx); corporate proxy returning its own error page with a non-2xx status.","commonSituations":"Running the fetch tool with stale credentials after failing to refresh; passing a --client-version that the endpoint no longer accepts; scripted/CI runs hammering the endpoint and hitting rate limits; region-blocked accounts.","solutions":["Read the status code and embedded body in the error message: 401/403 -> fix credentials (re-login); 400 -> correct the client version; 429 -> wait and retry; 5xx -> retry later.","Confirm the auth file refreshes cleanly (the tool refreshes automatically; failure surfaces as errors 45/46 first).","Pass the client version of a current Codex CLI release via the flag/config the tool exposes.","If behind a proxy, ensure proxy env vars reach the tool and the proxy permits chatgpt.com/backend-api endpoints."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"func isModelsHTTPStatusError(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"models request failed with status\")\n}\n\nfunc modelsStatus(err error) int { /* parse the %d from the message */ }","tryCatchPattern":"body, count, err := fetchModels(ctx, auth, token, ver)\nif isModelsHTTPStatusError(err) {\n    switch modelsStatus(err) {\n    case 401, 403: relogin()\n    case 429:      time.Sleep(backoff); retry()\n    default:       logAndAbort(err)\n    }\n}","preventionTips":["Branch on the embedded status code instead of treating all failures identically.","Rate-limit scripted fetches to stay under 429 thresholds.","Keep credentials freshly refreshed before fetching models."],"tags":["codex","http","upstream-api","cli"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}