Skip to content

Conversation

@mikestaub
Copy link

#1189

Issue: Streaming Responses Fail with Failed to parse JSON after multiple chunks.

Summary

Interactive chat sessions were intermittently aborting when the UI attempted to
stream assistant messages. The React SDK delegated streaming to
@tambo-ai/typescript-sdk’s advanceStream, which tries to JSON.parse each
newline-delimited SSE chunk as it arrives. Under normal network conditions the
server can split a single JSON payload across multiple TCP frames, so the client
occasionally received incomplete JSON and threw Failed to parse JSON after multiple chunks.—terminating the stream and surfacing an error toast.

What Changed

  • Added a local helper advanceStreamWithBuffer (react-sdk/src/util/advance-stream.ts)
    that wraps the generated SDK call but buffers data: lines until a full SSE
    event is assembled before attempting to parse JSON.
  • Updated tambo-thread-provider and related tests to use the buffered helper
    so streaming consumers no longer experience partial JSON failures.
  • Added unit coverage in react-sdk/src/__tests__/advance-stream.test.ts to
    verify we correctly handle split chunks and still surface the original error
    when the payload remains malformed.

Why It Works

By reassembling the event payload inside the React SDK we ensure JSON.parse
only runs on complete SSE messages. This prevents premature failures caused by
network chunking, without waiting for upstream regeneration of the Stainless
SDK. The change is isolated to the client, so existing API contracts remain
unchanged while the UI regains stable streaming behavior.

@vercel
Copy link

vercel bot commented Oct 23, 2025

@mikestaub is attempting to deploy a commit to the tambo ai Team on Vercel.

A member of the Team first needs to authorize it.

@alecf
Copy link
Contributor

alecf commented Oct 23, 2025

@mikestaub the intention behind our api is that we always return fully formed JSON - we're handling the partial JSON response from the llm provider server side, and always (theoretically) emitting correct JSON.

But that doesn't mean it is always working though! We did have some code in the sdk to just ignore malformed chunks at one point, I wonder if it broke. @MichaelMilstead do you remember some of the issues we were seeing and where the fix to ignore bad chunks was?

MichaelMilstead added a commit that referenced this pull request Nov 17, 2025
Co-authored-by: CharlieHelps <charlie@charlielabs.ai>
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