Repository navigation
perf(ai): speed up partial JSON streaming by 20-27% by skipping a parse that always fails - #23461
Open
deadstrobe5 wants to merge 1 commit into
Open
deadstrobe5 wants to merge 1 commit into
deadstrobe5 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
parsePartialJsonruns on every delta ofstreamTextwithOutput.object, ofstreamObject, and on everytool-input-deltainprocessUIMessageStream(so everyuseChattool call). It starts withJSON.parseon the whole accumulated text. While streaming, that text almost always ends inside an unclosed string, object or array, so this parse fails, builds aJSONParseError, and only then doesfixJsonrun and the repaired text get parsed again.Summary
fixJsonalready tracks the parser state, so this addsrepairJson, which also returnsisTruncated. When it is set,parsePartialJsonskips the parse that is bound to fail. The results do not change. A differential test compared old and new on every prefix of 613 random, pretty-printed and mutated documents (28,305 inputs), and every result matched.End-to-End Verification
Full
streamText/readUIMessageStreampipelines with a mock model streaming 8-char deltas, Node 26. Each run is a separate process, 5 runs per side:streamText+Output.object, 20 KBreadUIMessageStreamtool input, 20 KBstreamText+Output.object, 50 KBChecklist
pnpm changesetin the project root)Future Work
processUIMessageStreamalsostructuredClones the message on every chunk, which is the next per-chunk cost on theuseChatpath.