claude-code - 💡(How to fix) Fix Bug: rate_limit_event injected mid-NDJSON-line in stream-json output, corrupting assistant event parsing [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
anthropics/claude-code#49640Fetched 2026-04-17 08:35:27
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
labeled ×4

In Claude Code CLI v2.1.74, rate_limit_event objects are being written to stdout asynchronously in the middle of another NDJSON event (the final assistant message), splitting the line at the rate_limit_event's own \n terminator. This corrupts the stream-json output and causes the assistant response to be silently dropped.

Error Message

  • In our app, this surfaces as: "AI 응답이 비어 있습니다 (세션 오류 또는 프로세스 이상)" (empty response error)

Root Cause

In Claude Code CLI v2.1.74, rate_limit_event objects are being written to stdout asynchronously in the middle of another NDJSON event (the final assistant message), splitting the line at the rate_limit_event's own \n terminator. This corrupts the stream-json output and causes the assistant response to be silently dropped.

Fix Action

Workaround

We implemented a defensive parser fix that:

  1. Detects rate_limit_event injection within a corrupted line
  2. Removes the injected JSON using balanced-brace traversal
  3. Saves the partial fragment and joins it with the next line to reconstruct valid JSON

This handles the case but shouldn't be necessary if rate_limit_event is emitted at proper line boundaries.

Code Example

{"type":"assistant","message":{"content":[{"type":"text","text":"...partial text{"type":"rate_limit_event","rate_limit_info":{"status":"allowed","resetsAt":...},...}
 ...continuation of text..."}],...},"stop_reason":"end_turn",...}

---

{"type":"assistant","message":{...full text...},"stop_reason":"end_turn",...}\n
{"type":"rate_limit_event",...}\n
RAW_BUFFERClick to expand / collapse

Summary

In Claude Code CLI v2.1.74, rate_limit_event objects are being written to stdout asynchronously in the middle of another NDJSON event (the final assistant message), splitting the line at the rate_limit_event's own \n terminator. This corrupts the stream-json output and causes the assistant response to be silently dropped.

Steps to Reproduce

  1. Run Claude Code CLI with --output-format stream-json
  2. Trigger a long response that causes a rate limit notification during generation (e.g., a large text output near the five-hour limit)
  3. Observe stdout

Observed Behavior

The raw stdout contains a split NDJSON stream:

{"type":"assistant","message":{"content":[{"type":"text","text":"...partial text{"type":"rate_limit_event","rate_limit_info":{"status":"allowed","resetsAt":...},...}
 ...continuation of text..."}],...},"stop_reason":"end_turn",...}

The rate_limit_event JSON is injected inside the text content of the assistant event, and its own \n terminator splits the outer assistant event's line into two fragments:

  • Line 1: {"type":"assistant",...,"text":"...partial text{"type":"rate_limit_event",...} — fails JSON parsing (unescaped " from rate_limit_event inside text string)
  • Line 2: ...continuation of text..."}],...}} — not valid JSON (doesn't start with {)

Both lines are unparseable, so the assistant's response text is lost entirely.

Expected Behavior

rate_limit_event should only be emitted at NDJSON line boundaries — i.e., after the current event's \n terminator — never in the middle of another event's output.

Expected stream order:

{"type":"assistant","message":{...full text...},"stop_reason":"end_turn",...}\n
{"type":"rate_limit_event",...}\n

Impact

  • Callers using --output-format stream-json receive an empty response when a rate_limit_event is injected mid-stream
  • The response text is present in raw stdout bytes but irrecoverable through standard NDJSON line parsing
  • In our app, this surfaces as: "AI 응답이 비어 있습니다 (세션 오류 또는 프로세스 이상)" (empty response error)

Environment

  • Claude Code CLI version: 2.1.74
  • Output format: --output-format stream-json
  • Platform: macOS 15.x
  • Observed during: Spark multi-agent round (long text generation, ~3700 output tokens)

Workaround

We implemented a defensive parser fix that:

  1. Detects rate_limit_event injection within a corrupted line
  2. Removes the injected JSON using balanced-brace traversal
  3. Saves the partial fragment and joins it with the next line to reconstruct valid JSON

This handles the case but shouldn't be necessary if rate_limit_event is emitted at proper line boundaries.

extent analysis

TL;DR

Emit rate_limit_event objects at NDJSON line boundaries to prevent corruption of the stream-json output.

Guidance

  • Verify that rate_limit_event objects are being written to stdout asynchronously and adjust the writing mechanism to ensure they are emitted after the current event's \n terminator.
  • Review the Claude Code CLI v2.1.74 code to identify where rate_limit_event objects are being injected into the output stream and modify it to respect NDJSON line boundaries.
  • Consider implementing a buffering mechanism to temporarily hold rate_limit_event objects until the current event has been fully written to the output stream.
  • Test the modified code with the provided steps to reproduce the issue to ensure the fix resolves the corruption of the stream-json output.

Example

No explicit code example is provided due to the lack of specific code details in the issue, but the fix should involve modifying the output writing mechanism to respect NDJSON line boundaries.

Notes

The provided workaround, which involves detecting and removing injected rate_limit_event JSON, can be used as a temporary solution but should not be necessary if the rate_limit_event objects are emitted at proper line boundaries.

Recommendation

Apply the workaround until the root cause is fixed, as it provides a defensive parsing mechanism to handle corrupted lines and reconstruct valid JSON.

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

claude-code - 💡(How to fix) Fix Bug: rate_limit_event injected mid-NDJSON-line in stream-json output, corrupting assistant event parsing [1 participants]