Skip to content

skip events with no data while streaming#861

Open
fvdemand wants to merge 1 commit intoanthropics:mainfrom
fvdemand:bug/handle-no-data-while-streaming
Open

skip events with no data while streaming#861
fvdemand wants to merge 1 commit intoanthropics:mainfrom
fvdemand:bug/handle-no-data-while-streaming

Conversation

@fvdemand
Copy link

@fvdemand fvdemand commented Dec 15, 2025

In my cloud environment while using v0.71.2

I have continued to have the issue described here.

I have seen it occur both with content_block_delta and content_block_stop.

Somewhat of note I have not got this running locally.

Logs:

Could not parse message into JSON:
From chunk: [ 'event: content_block_delta' ]

I believe this is somewhat normal server behavior when the server is responding for a long time basically the raw string for the sse shows that there is no data. I added a condition to skip this event only when streaming.

@fvdemand fvdemand requested a review from a team as a code owner December 15, 2025 23:47
@felixfbecker
Copy link
Contributor

@fvdemand sorry to hear you're having issues. It seems like what you're describing is either a bug in our API (in which case we need to fix it there), or a bug in the SSE parsing. I wouldn't expect null data to reach this part of the code, so I'm hesitant to add a workaround here without solving the underlying cause. Do you have an example script that reproduces this issue so we can investigate?

@fvdemand
Copy link
Author

fvdemand commented Dec 16, 2025

I don't have a script that will be useful for you to reproduce. Here is the last bit that gave the error. However, I had the same issue when using the .stream and .finalMessage functions.

const getFullStreamedResponseClaude = async (completionContext) => {
  completionContext = {
    ...completionContext,
    stream: true
  };
  const stream = await anthropicClient.messages.create(completionContext);
  try {
    const chunks = [];
    for await (const chunk of stream) {
      if (chunk.type === 'content_block_delta') {
        const text = chunk.delta?.text || '';
        chunks.push(text);
      }
    }
    return chunks.join('');
  } catch(error) {
    if(error instanceof TypeError) {
      console.log('Type error for stream: ', stream);
    }
    throw error;
  }
};

Do you agree that the log in my original message indicates that the Message code received a sse in which the .raw demonstrates that there is no .data ? For clarity those are not my logs, they come from in the .messages interface.

I will try to get a request ID. Update, I do not have access to the env is there a modification I can make to this block to get the request id?

@felixfbecker
Copy link
Contributor

felixfbecker commented Jan 26, 2026

I think we might have fixed this in the API or SDK recently, are you still seeing this issue in the latest version?

@fvdemand
Copy link
Author

I have been running with this change using an npm patch. I intend to get back on the main package. This week, I will run the scenario we originally saw this in without the patch and let you know if the issue persists

DukeDeSouth added a commit to DukeDeSouth/anthropic-sdk-typescript that referenced this pull request Feb 6, 2026
… crash

In edge and cloud environments (Vercel Edge, Cloudflare Workers), network
proxies and CDNs can split SSE chunks so that an event line arrives without
its corresponding data line. The SSEDecoder correctly produces an event with
data='', but Stream.fromSSEResponse crashes with "Unexpected end of JSON
input" when trying to JSON.parse('').

This adds an empty-data guard for all event types that expect JSON data:
- completion
- message_start, message_delta, message_stop
- content_block_start, content_block_delta, content_block_stop

Events with empty data are silently skipped, allowing the stream to
continue processing subsequent valid events.

Note: The existing PR anthropics#861 checks `sse.data == null`, which does not
catch this bug since SSEDecoder always sets data to a string (empty
string '' for events without a data line, and '' == null is false).

Fixes anthropics#292 (regression), addresses anthropics#861

Added 16 new tests covering:
- Per-event-type empty data handling (7 event types)
- Stream continuation after empty events
- Interleaved empty and valid events
- Realistic edge-environment simulation
- Error event behavior with empty data
- Abort controller interaction
- All-empty stream completion
- Stress test (100 events with intermittent empty data)

Co-authored-by: Cursor <cursoragent@cursor.com>
@fvdemand
Copy link
Author

@DukeDeSouth I read your message about why this is not the right fix. It seems to make sense to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants