stream error: stream disconnected before completion: stream closed before response.complete; retrying...
Resolved 💬 46 comments Opened Jul 15, 2025 by FreedomLabsIO Closed Oct 16, 2025
💡 Likely answer: A maintainer (bolinfest, collaborator)
responded on this thread — see the highlighted reply below.
What version of Codex is running?
codex-cli 0.7.0
Which model were you using?
codex-mini-latest
What platform is your computer?
Linux 6.8.0-63-generic x86_64 x86_64
What steps can reproduce the bug?
Send any prompt after codex login
What is the expected behavior?
Model response
What do you see instead?
<img width="1505" height="698" alt="Image" src="https://github.com/user-attachments/assets/1af1875d-3460-4f72-9c0c-85003ec0d3f3" />
Additional information
_No response_
46 Comments
same error on macos anyone help?
Same error on WSL...
Same error on WSL...
Will anyone take care of this problem?
+1
same issue in macOS with azure APIs
same issue version 0.7
@bolinfest
After endless searching and experimenting, I got past this issue. I don't think it's as much Codex's fault as the transition going on at Azure right now. I went to Azure AI Foundry and pieced together this config that works with codex with plain ol' gpt-4o.
I have tested this with the latest npm version (0.7.0)
Just to clarify, do you have any amount of money topped up to your credit? Check it here: https://platform.openai.com/settings/organization/billing/overview
I had the same issue, but as soon as I topped up my balance with $5, the error message is gone. Even though I have/had $38 in credit grants.
ı have 120$ but not working
I have an Azure student license for context. The only configuration that does something for me is:
Where my "
resource" name is the "folder" with multiple models deployed, I did not find a way to access an individual deployed model. Moreover, any otherapi-versionfails. With this configuration o3-mini seems to work for more than one message (o4-mini always fails after the first response, always). see:<img width="542" height="377" alt="Image" src="https://github.com/user-attachments/assets/871666cc-146c-4a67-aa37-9aa871d4ef31" />
With o4-mini:
<img width="828" height="402" alt="Image" src="https://github.com/user-attachments/assets/08105b18-a9cd-4149-a702-06c95cd0e40c" />
The set up it's just too messy. I'm working with the 0.7.0 version installed via brew.
https://grok.com/share/bGVnYWN5_291468fe-b9cd-44a4-928e-dbc5be3b4542
I did build the latest main branch up until now, the codex cli interface is different (OpenAI Codex (research preview) v0.0.0-dev ). And now nothing works with the config I shared before, which I had to convert to JSON since for some reason the .toml version is ignored. See:
<img width="1126" height="402" alt="Image" src="https://github.com/user-attachments/assets/20b8acb0-3a70-4e7b-930d-7bded9485f9c" />
At least it loads faster and you can try more messages once past the error. I'm giving up.
<img width="960" height="564" alt="Image" src="https://github.com/user-attachments/assets/e9f474cf-2fd0-4053-bba6-089eb2ebf0ad" />
Same for me
Same error as everyone above. I have $ in my api credit.
when I logged in initially I got:
Credit redemption request failed: HTTP Error 400: Bad Request
Successfully logged in
For me, if I use the default model it works just fine, but if I update config.toml to have
model = "o3"then I receive this error endlessly. Similarly, if I launch withcodex -m o3, will get the same error. It seems specific to using o3 for me. I have a Plus subscription so I wouldn't expect there to be an issue with credits, etc.For me ocurred the same when using the models:
codex-mini-latestando4-mini.But when using
o3-miniit started working. Is codex cli limited to specific models for the moment?As a small update - I found that after I went through Verification in my Organization (using third party Persona svc) I was able to use o3 via codex/cli for quite some time (hours). I did however once again just start observing the same issue consistently, 3 or 4 retries resulting in
BackgroundEvent { message: "stream error: stream disconnected before completion..." }Ok I seem to have fixed this. You need to be verified before you can use the default model. I was able to get it working as-is by running codex -m o3-mini.
To use the other models, in the Logs section from the Dashboard (https://platform.openai.com/logs) if you click into one of the failed messages and scroll all the way to the bottom, it will tell you that you need to be verified to use certain models.
You can complete that process here: https://platform.openai.com/settings/organization/general
Once that was done, I can now use the default model codex-mini-latest (and I believe a few others you need verification for).
-m o3-minididn't work for me with 0.8.0 on MacOS. I installedcodexwith brewA colleague shared that they installed with
npm install -g @openai/codex@nativeearlier and did not get the stream error. The same for me. No stream error (still need company to fund before I can test it). Butnpm install -g @openai/codex@nativeshows the version ascodex-cli 0.0.2505291458I'm seeing this as well.
Still, tons of these stream errors.
Same here. I wanted to use codex but seeing these errors instead.
Yep same in WSL. Using an enterprise account but aside from that it's setup the same.
v0.20.0 as well, so the latest available version.
This is about my mistake—I had specified the following in my .env file for Qwen Code. The OPENAI_BASE_URL setting here was affecting Codex CLI and causing the error "🖐 stream disconnected before completion: stream closed before response.completed." Maybe you could check your environment variables or the contents of your .env file?
OPENAI_API_KEY=sk-or-v1-xxxxxxxxxx
OPENAI_BASE_URL=https://openrouter.ai/api/v1
OPENAI_MODEL=qwen/qwen3-coder:free
Fixed it by removing open-ai related environment variables in my zsh rc. I'm guessing that codex defaults to bash env vars over what you set set in the actual codex config. IDK why it would be this way, but this did fix it for me.
I'm getting this now every 5-10 minutes. Something broke in the last update.
Getting this every few minutes...
This may be because we stopped honoring
.envin0.30.0? See "breaking changes:"https://github.com/openai/codex/releases/tag/rust-v0.30.0
/cc @pakrym-oai
getting same error all the time. any fix?
Same error. almost unusable
I started getting this error today in a long session that worked fine yesterday. It seems to work fine if I start a new session. I'm guessing the model I was using during that session, apparently gpt-5-1p-codexswic, is not available anymore?
The model from /status looks the same in both the old and the new session that works:
🧠 Model
• Name: gpt-5
• Provider: OpenAI
• Reasoning Effort: Medium
• Reasoning Summaries: Auto
But I'm guessing the version of the model I was using in the old session has been retired while I was working. These are the errors I get in the old session:
✔ You approved codex to run go build ./... this time
✗ ⌨️ go build ./...
error: command timed out
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 1/5 in 200ms…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 2/5 in 394ms…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 3/5 in 865ms…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 4/5 in 1.628s…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 5/5 in 3.306s…
🖐 stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found
user
It seems there were some inference API error when you attempted to build. Can you try again?
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 1/5 in 190ms…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 2/5 in 408ms…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 3/5 in 731ms…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 4/5 in 1.759s…
⚠ stream error: stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found; retrying 5/5 in 3.334s…
🖐 stream disconnected before completion: Attempted an engine lookup because both a model and engine were passed. Engine gpt-5-1p-codexswic not found
▌ Write tests for @filename
⏎ send Ctrl+J newline Ctrl+T transcript Ctrl+C quit 513323 tokens used 80% context left
similar issue, the error today in a long session that worked fine since yesterday until this moment:
is it possible that too many tokens used (2.52M in my case) is a reason?
also could anyone please help with arguments to be passed to the command line to continue the old session? is it possible?
It is possible if you are on codex cli 0.30.0 or newer, the options are --resume and --continue. They are not documented in --help at the moment.
https://github.com/openai/codex/issues/2173#issuecomment-3264145154
Unfortunately it doesn't support resuming sessions created with older versions. As a workaround, I copied the json. session file from the .codex directory to the project I was working on, then, in a new session, I asked it to read it to familiarize itself with the previously done work. That worked reasonably well.
@vnicolici
Almost like i did before, but even without a copying. It has access to its home directory.
Huge thanks for that!
I get the same error for both codex ide and codex cli but only on my macbook, not when im on windows parallels, or on my mac mini... Super annoying...
FWIW this makes Codex completely unusable. Surprised this is still open. I have an
OPENAI_API_KEYset in my env. Should be considered a sev0.As of 0.36, we don't support implicit login. You need to use
codex login --api-key "your-api-key-here"to explicitly login using your api key.Release notes
@aibrahim-oai Thanks. This error should really be handled properly. For eg. the stream error should propagate some status code code so people can at least see it's failing for authentication reasons.
Or, better yet perform some sort of pre-flight check when you launch Codex that verifies that you have authentication configured properly, and if not guides you to do the right thing.
▌ Write tests for @filename
⏎ send Shift+⏎ newline Ctrl+C quit`@aib
`codex login --api-key
"<redacted>"
Successfully logged in
(base) evanreilly@Evans-MacBook-Pro-2 moondream-station % codex
✨⬆️ Update available! 0.23.0 -> 0.38.0.
Run npm install -g @openai/codex@latest to update.
To get started, describe a task or try one of these commands:
/init - create an AGENTS.md file with instructions for Codex
/status - show current session configuration and token usage
/diff - show git diff (including untracked files)
user
walk me through converting this to mlx
🖐 stream disconnected before completion: stream closed before response.completed
care to explain @aibrahim-oai ?
at my wits end, upgraded npm, upgraded codex, signed in with oauth, signed in with api key. Still always get
🖐 stream disconnected before completion: stream closed before response.completed
also same deal with vscode/cursor extensions
I'm not sure it's work for you, but it's work for me twice (Mac OS 26, M2 Max). If any member or developer from team Open AI/Codex, please check this issue. I think it's authoriztion/key. I attach additional information (describe problem)[/+]
name:
"🐞 Codex CLI Bugfix / Diagnostic Checklist"
about:
Fix or diagnose stream disconnects / 401 errors in codex-cli v0.43.x
title:
"[Bug] Codex CLI stream disconnect or 401 Unauthorized"
labels:
["bug", "codex-cli", "openai-cli"]
assignees: ""
🧩 Codex CLI Bugfix / Diagnostic Checklist
Target: macOS ARM —
codex-cli v0.43.0-alpha.15Purpose: Fix or diagnose
stream disconnector401 Unauthorizedissues during ChatGPT SSO sessions.---
A) Clean Environment Variables
---
B) Login via ChatGPT SSO
(officially recommended for Plus / Pro / Team users)
---
C) Launch TUI and Check Status
Inside the TUI:
---
D) Switch Model to Stable
gpt-5Select gpt-5 (not
gpt-5-codex).---
E) Quick Sanity Stream Test
If OK — switch back to
gpt-5-codexand repeat.---
🪲 If the stream disconnects
Enable Debug Logging
In another terminal:
---
✅ Minimal Fix — Force Stable Model
Option 1 — One-off Launch
Option 2 — Make Default
Then check inside TUI:
---
If the stream still breaks
1️⃣ Relogin and Clear Cache
2️⃣ Recreate Minimal Config
3️⃣ Start with Debug Logs
In another terminal:
4️⃣ Verify SSE Network Stability
5️⃣ Headless Mode (Non-TUI)
---
🔧 Clean Up OpenRouter / BASE URL / Proxy
Then check where Codex connects:
---
🔐 If
401 UnauthorizedPersistsA) Clean SSO Relogin
B) Clear Auth-Related Environment Vars
C) Sync System Time (Critical for OAuth/JWT)
---
🧭 Temporary Workaround — API Key Mode
If SSO remains broken:
---
⚙️ Minimal Config Options
Simple
Explicit Provider Definition
---
✅ Expected
/statusOutput---
Please attach:
~/.codex/log/codex-tui.lognear the failure,/statusoutput from inside the TUI.Maintainers will review logs to determine if the issue is SSO, SSE, or model access related.
---
<html>
<head>
</head>
<body>
<p class="p1">The root cause of this <span class="s1"><b>Codex CLI bug</b></span> isn’t just “one thing” — it’s a <span class="s1"><b>combination of authentication and environment misalignment</b></span> introduced in the 0.41–0.43 branch.</p>
<p class="p1">Here’s a clear technical breakdown 👇</p>
<p class="p2"><span class="s2"><hr></span></p>
<p class="p2"><span class="s2"><h2><b>🔍 Root Cause Summary</b></h2></span></p>
<p class="p3"><br></p>
<p class="p1">Codex CLI (<span class="s3">codex-cli 0.43.x</span>) uses multiple authentication and networking backends — <span class="s1"><b>ChatGPT SSO</b></span>, <span class="s1"><b>OpenAI API key</b></span>, and <span class="s1"><b>OpenRouter</b></span> routing.</p>
<p class="p4"><span class="s4">When these collide or get misdetected, the CLI </span><b>sends requests to the wrong API endpoint or with invalid tokens</b><span class="s4">, leading to:</span></p>
<p class="p2"><span class="s2"><ul><li>
<p class="p1">401 Unauthorized<span class="s1"> errors</span></p>
</li><li>
<p class="p1">Broken <span class="s1"><b>SSE (Server-Sent Events)</b></span> streams in TUI mode</p>
</li><li>
<p class="p1">Sudden disconnects when using models like <span class="s1">gpt-5-codex</span></p>
</li></ul></span></p>
<p class="p2"><span class="s2"><hr></span></p>
<p class="p2"><span class="s2"><h2><b>⚙️ Detailed Breakdown</b></h2></span></p>
<p class="p3"><br></p>
<p class="p2"><span class="s2"><h3><b>1.<span class="Apple-converted-space"> </span></b></h3><h3><b>SSO Token Mismatch</b></h3></span></p>
<p class="p2"><span class="s2"><ul><li>
<p class="p1">The “Sign in with ChatGPT” SSO flow issues a <span class="s1"><b>short-lived JWT</b></span> token, valid only for ChatGPT web context.</p>
</li><li>
<p class="p1">Codex CLI, however, sometimes tries to reuse it for <span class="s1"><b>OpenAI’s API endpoints (api.openai.com/v1/responses)</b></span> — which reject it with <span class="s2">401 Unauthorized</span>.</p>
</li><li>
<p class="p1">This happens because CLI doesn’t refresh the SSO token correctly or misidentifies the provider.</p>
</li></ul></span></p>
<p class="p3"><br></p>
<p class="p4"><b>Symptoms:</b><b></b></p>
<p class="p2"><span class="s2"><ul><li>
<p class="p1"><span class="s1">/status</span> shows “Logged in with ChatGPT” but every call to OpenAI fails.</p>
</li><li>
<p class="p1"><span class="s1">codex.api_request</span> lines in logs return <span class="s1">401</span> even though login appears successful.</p>
</li></ul></span></p>
<p class="p2"><span class="s2"><hr></span></p>
<p class="p2"><span class="s2"><h3><b>2.<span class="Apple-converted-space"> </span></b></h3><h3><b>Conflicting Environment Variables</b></h3></span></p>
<p class="p3"><br></p>
<p class="p1">Many users have legacy or hidden exports like:</p>
<pre><code>OPENAI_API_KEY
OPENAI_BASE_URL
OPENROUTER_API_KEY
API_BASE
BASE_URL</code></pre>
<p class="p1">If any of these are set, Codex overrides its SSO provider logic and starts routing via:</p>
<p class="p2"><span class="s1"><ul><li>
<p class="p1"><span class="s1">openrouter.ai</span> (proxy endpoint)</p>
</li><li>
<p class="p1">or a stale custom <span class="s1">OPENAI_BASE_URL</span></p>
</li></ul></span></p>
<p class="p3"><br></p>
<p class="p1">Result:</p>
<p class="p1">SSO token is valid, but requests go to the wrong host → instant disconnection or 401.</p>
<p class="p2"><span class="s1"><hr></span></p>
<p class="p2"><span class="s1"><h3><b>3.<span class="Apple-converted-space"> </span></b></h3><h3><b>Model Access Policy Change</b></h3></span></p>
<p class="p2"><span class="s1"><ul><li>
<p class="p1"><span class="s1">gpt-5-codex</span><span class="s2"> sometimes requires </span><b>org-level verification</b><span class="s2">.</span></p>
</li><li>
<p class="p1">Normal <span class="s1">gpt-5</span> is public for Plus/Pro/Team SSO users.</p>
</li><li>
<p class="p1">So the same token can succeed with <span class="s1">gpt-5</span>, but fail with <span class="s1">gpt-5-codex</span>.</p>
</li></ul></span></p>
<p class="p3"><br></p>
<p class="p1">This causes false reports of “streaming broken,” while the real issue is <span class="s2"><b>permission denial</b></span> on that specific model.</p>
<p class="p2"><span class="s1"><hr></span></p>
<p class="p2"><span class="s1"><h3><b>4.<span class="Apple-converted-space"> </span></b></h3><h3><b>TUI Stream (SSE) Regression</b></h3></span></p>
<p class="p3"><br></p>
<p class="p1">Versions <span class="s3">0.41–0.42</span> had partial regressions in the SSE handler:</p>
<p class="p1">the <span class="s2"><b>event parser</b></span> and <span class="s2"><b>connection timeout</b></span> were not robust to partial chunked responses.</p>
<p class="p1">So even when the model response was valid, the client aborted early.</p>
<p class="p3"><br></p>
<p class="p1">That’s why:</p>
<p class="p2"><span class="s1"><ul><li>
<p class="p1"><span class="s1">codex exec "Say 'pong'"</span> (headless mode) works perfectly,</p>
</li><li>
<p class="p1">but <span class="s1">codex</span> (interactive TUI) drops mid-stream.</p>
</li></ul></span></p>
<p class="p2"><span class="s1"><hr></span></p>
<p class="p2"><span class="s1"><h3><b>5.<span class="Apple-converted-space"> </span></b></h3><h3><b>Network Layer / SSL Inspection</b></h3></span></p>
<p class="p3"><br></p>
<p class="p1">When all else is correct, some macOS setups still interrupt streaming:</p>
<p class="p2"><span class="s1"><ul><li>
<p class="p1">HTTPS interception (AdGuard, Little Snitch, antiviruses)</p>
</li><li>
<p class="p1">Corporate proxy rewriting chunked transfer encoding</p>
</li><li>
<p class="p1">Broken SSE keep-alive frames</p>
</li></ul></span></p>
<p class="p3"><br></p>
<p class="p1">You can confirm by:</p>
<pre><code>curl -N https://stream.wikimedia.org/v2/stream/recentchange</code></pre>
<p class="p1">If that stream also dies → it’s the network layer, not Codex.</p>
<p class="p2"><span class="s1"><hr></span></p>
<p class="p2"><span class="s1"><h2><b>🧩 So in short</b></h2></span></p>
Category | Root Cause | Fix
-- | -- | --
Auth | ChatGPT SSO token used on API endpoint | Full relogin + remove stale auth.json
Routing | Wrong base URL (OpenRouter / proxy) | Clear env vars & config
Model | Restricted gpt-5-codex | Temporarily use gpt-5
Client | SSE parser bug in TUI | Use codex exec (headless)
Network | Proxy / SSL inspection | Test SSE with curl; disable filters
<p class="p1"><span class="s1"><hr></span></p>
<p class="p1"><span class="s1"><h3><b>✅ Confirmed Workarounds</b></h3></span></p>
<p class="p1"><span class="s1"><ol start="1"><li>
<p class="p1"><b>Hard-reset login:</b></p>
</li></ol></span></p>
<pre><code>codex logout
rm -rf ~/.codex ~/.config/codex
codex login</code></pre>
<p class="p1"><span class="s1"><ol start="1"><li>
<p class="p1">→ choose “Sign in with ChatGPT”.</p>
</li><li>
<p class="p1"><b>Force base model and endpoint:</b></p>
</li></ol></span></p>
<pre><code>mkdir -p ~/.codex
printf 'model="gpt-5"\n' > ~/.codex/config.toml
export OPENAI_BASE_URL="https://api.openai.com/v1"
codex --model gpt-5</code></pre>
<p class="p1"><span class="s1"><ol start="2"><li>
<p class="p1"><br></p>
</li><li>
<p class="p1"><b>If still 401:</b><b></b></p>
<p class="p2">Use API key auth instead of SSO (temporary fallback):</p>
</li></ol></span></p>
<pre><code>codex login
choose “Use OpenAI API key”
codex --model gpt-4o</code></pre>
<p class="p1"><br></p>
<p class="p2"><span class="s1"><hr></span></p>
</body>
</html>
Please use
and then