codex - 💡(How to fix) Fix TypeScript SDK Thread.run() treats fatal stream errors as successful empty turns [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#22470Fetched 2026-05-14 03:35:26
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Participants
Timeline (top)
labeled ×2

Error Message

The TypeScript SDK defines a top-level fatal stream error event, but Thread.run() does not handle it. type: "error"; It does not handle event.type === "error". As a result, if the underlying CodexExec.run() stream emits a fatal top-level error event, Thread.run() resolves successfully with an empty turn instead of rejecting. Using current openai/codex main at 155c04ad4, add a focused Jest test that stubs CodexExec.run() to emit a top-level error event: yield JSON.stringify({ type: "error", message: "fatal stream error" }); await expect(thread.run("fail")).rejects.toThrow("fatal stream error"); Thread.run() should reject when it receives a fatal top-level stream error event. A narrow fix would be to handle event.type === "error" in sdk/typescript/src/thread.ts, for example by throwing new Error(event.message), and add regression coverage for this case. I searched existing issues and PRs for Thread.run, runStreamed, ThreadErrorEvent, top-level stream errors, and TypeScript SDK stream error handling, and did not find an exact duplicate.

Code Example

export type ThreadErrorEvent = {
  type: "error";
  message: string;
};

---

{
  "items": [],
  "finalResponse": "",
  "usage": null
}

---

import { Thread } from "../src/thread";

it("throws on fatal top-level stream errors", async () => {
  const exec = {
    async *run() {
      yield JSON.stringify({ type: "error", message: "fatal stream error" });
    },
  };

  const thread = new Thread(exec as never, {}, {});

  await expect(thread.run("fail")).rejects.toThrow("fatal stream error");
});

---

corepack pnpm --filter @openai/codex-sdk exec jest tests/run.test.ts -t "fatal top-level" --runInBand

---

Received promise resolved instead of rejected
Resolved to value: {"finalResponse": "", "items": [], "usage": null}
RAW_BUFFERClick to expand / collapse

What issue are you seeing?

The TypeScript SDK defines a top-level fatal stream error event, but Thread.run() does not handle it.

ThreadErrorEvent is defined in sdk/typescript/src/events.ts as:

export type ThreadErrorEvent = {
  type: "error";
  message: string;
};

and it is included in the ThreadEvent union.

However, Thread.run() in sdk/typescript/src/thread.ts only handles:

  • item.completed
  • turn.completed
  • turn.failed

It does not handle event.type === "error".

As a result, if the underlying CodexExec.run() stream emits a fatal top-level error event, Thread.run() resolves successfully with an empty turn instead of rejecting.

Observed result from a focused local regression test:

{
  "items": [],
  "finalResponse": "",
  "usage": null
}

This can make SDK callers interpret a fatal stream failure as a successful empty response.

What steps can reproduce the bug?

Using current openai/codex main at 155c04ad4, add a focused Jest test that stubs CodexExec.run() to emit a top-level error event:

import { Thread } from "../src/thread";

it("throws on fatal top-level stream errors", async () => {
  const exec = {
    async *run() {
      yield JSON.stringify({ type: "error", message: "fatal stream error" });
    },
  };

  const thread = new Thread(exec as never, {}, {});

  await expect(thread.run("fail")).rejects.toThrow("fatal stream error");
});

Then run:

corepack pnpm --filter @openai/codex-sdk exec jest tests/run.test.ts -t "fatal top-level" --runInBand

Current result:

Received promise resolved instead of rejected
Resolved to value: {"finalResponse": "", "items": [], "usage": null}

What is the expected behavior?

Thread.run() should reject when it receives a fatal top-level stream error event.

A narrow fix would be to handle event.type === "error" in sdk/typescript/src/thread.ts, for example by throwing new Error(event.message), and add regression coverage for this case.

Additional information

I searched existing issues and PRs for Thread.run, runStreamed, ThreadErrorEvent, top-level stream errors, and TypeScript SDK stream error handling, and did not find an exact duplicate.

This is separate from normal turn.failed handling. The issue is specifically that the SDK event union already includes a top-level ThreadErrorEvent, but the buffered Thread.run() API ignores it.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

codex - 💡(How to fix) Fix TypeScript SDK Thread.run() treats fatal stream errors as successful empty turns [1 participants]