skip events with no data while streaming#861
skip events with no data while streaming#861fvdemand wants to merge 1 commit intoanthropics:mainfrom
Conversation
|
@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 |
|
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. 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? |
|
I think we might have fixed this in the API or SDK recently, are you still seeing this issue in the latest version? |
|
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 |
… 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>
|
@DukeDeSouth I read your message about why this is not the right fix. It seems to make sense to me |
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:
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.