language_models: Fix the partial json streaming to not blast \ everywhere#51976
Merged
Veykril merged 1 commit intozed-industries:mainfrom Mar 20, 2026
Merged
Conversation
AmaanBilwar
pushed a commit
to AmaanBilwar/zed
that referenced
this pull request
Mar 20, 2026
…ywhere (zed-industries#51976) ## Context This PR fixes one of the issues in zed-industries#51905, where model outputs are full of errant `\` characters. heres the problem: As the response is streamed back to zed, we accumulate the message chunks and and need to convert those chunks to valid json, to do that we use `partial_json_fixer::fix_json`, when the last character of a chunk is `\`, the `fix_json` has to escape that backslash, because its inside of a string (if it isn't, its invalid json and the tool call will crash) and other wise you would end up escaping the end `"` and everything would be messed up. why is this a problem for zed: T_0 is the output at some step. T_1 is the output at the next step. the `fix_json` system is meant to be used by replacing T_0 with T_1, however in the editor, replacing the entirety of T_0 with T_1 would be slow/cause flickering/etc.. so we calculate the difference between T_0 and T_1 and just add it to the current buffer state. So when a chunk ends on `\`, we end up with something like `... end of line\\"}` at the end of T_0, in T_1, this becomes `... end of line\n ...`. then when we add the new chunk from T_1, it just picks up after the \n because its tracking the length to manage the deltas. ## How to Review utils.rs: fix_streamed_json => remove trailing backslashes from incoming json streams so that `partial_json_fixer::fix_json` doesn't try to escape them. other files: call fix_streamed_json before passing to `serde_json` I had claude write a bunch of tests while I was working on the fix, which I have kept in for now, but the end functionality of fix_streamed_json is really simple now, so maybe these arent really needed. ## Videos Behavior Before: https://github.com/user-attachments/assets/f23f5579-b2e1-4d71-9e24-f15ea831de52 Behavior After: https://github.com/user-attachments/assets/40acdc23-4522-4621-be28-895965f4f262 ## Self-Review Checklist <!-- Check before requesting review: --> - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Release Notes: - language_models: fixed partial json streaming
toshmukhamedov
pushed a commit
to toshmukhamedov/zed
that referenced
this pull request
Mar 20, 2026
…ywhere (zed-industries#51976) ## Context This PR fixes one of the issues in zed-industries#51905, where model outputs are full of errant `\` characters. heres the problem: As the response is streamed back to zed, we accumulate the message chunks and and need to convert those chunks to valid json, to do that we use `partial_json_fixer::fix_json`, when the last character of a chunk is `\`, the `fix_json` has to escape that backslash, because its inside of a string (if it isn't, its invalid json and the tool call will crash) and other wise you would end up escaping the end `"` and everything would be messed up. why is this a problem for zed: T_0 is the output at some step. T_1 is the output at the next step. the `fix_json` system is meant to be used by replacing T_0 with T_1, however in the editor, replacing the entirety of T_0 with T_1 would be slow/cause flickering/etc.. so we calculate the difference between T_0 and T_1 and just add it to the current buffer state. So when a chunk ends on `\`, we end up with something like `... end of line\\"}` at the end of T_0, in T_1, this becomes `... end of line\n ...`. then when we add the new chunk from T_1, it just picks up after the \n because its tracking the length to manage the deltas. ## How to Review utils.rs: fix_streamed_json => remove trailing backslashes from incoming json streams so that `partial_json_fixer::fix_json` doesn't try to escape them. other files: call fix_streamed_json before passing to `serde_json` I had claude write a bunch of tests while I was working on the fix, which I have kept in for now, but the end functionality of fix_streamed_json is really simple now, so maybe these arent really needed. ## Videos Behavior Before: https://github.com/user-attachments/assets/f23f5579-b2e1-4d71-9e24-f15ea831de52 Behavior After: https://github.com/user-attachments/assets/40acdc23-4522-4621-be28-895965f4f262 ## Self-Review Checklist <!-- Check before requesting review: --> - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Release Notes: - language_models: fixed partial json streaming
AmaanBilwar
pushed a commit
to AmaanBilwar/zed
that referenced
this pull request
Mar 23, 2026
…ywhere (zed-industries#51976) ## Context This PR fixes one of the issues in zed-industries#51905, where model outputs are full of errant `\` characters. heres the problem: As the response is streamed back to zed, we accumulate the message chunks and and need to convert those chunks to valid json, to do that we use `partial_json_fixer::fix_json`, when the last character of a chunk is `\`, the `fix_json` has to escape that backslash, because its inside of a string (if it isn't, its invalid json and the tool call will crash) and other wise you would end up escaping the end `"` and everything would be messed up. why is this a problem for zed: T_0 is the output at some step. T_1 is the output at the next step. the `fix_json` system is meant to be used by replacing T_0 with T_1, however in the editor, replacing the entirety of T_0 with T_1 would be slow/cause flickering/etc.. so we calculate the difference between T_0 and T_1 and just add it to the current buffer state. So when a chunk ends on `\`, we end up with something like `... end of line\\"}` at the end of T_0, in T_1, this becomes `... end of line\n ...`. then when we add the new chunk from T_1, it just picks up after the \n because its tracking the length to manage the deltas. ## How to Review utils.rs: fix_streamed_json => remove trailing backslashes from incoming json streams so that `partial_json_fixer::fix_json` doesn't try to escape them. other files: call fix_streamed_json before passing to `serde_json` I had claude write a bunch of tests while I was working on the fix, which I have kept in for now, but the end functionality of fix_streamed_json is really simple now, so maybe these arent really needed. ## Videos Behavior Before: https://github.com/user-attachments/assets/f23f5579-b2e1-4d71-9e24-f15ea831de52 Behavior After: https://github.com/user-attachments/assets/40acdc23-4522-4621-be28-895965f4f262 ## Self-Review Checklist <!-- Check before requesting review: --> - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Release Notes: - language_models: fixed partial json streaming
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.
Context
This PR fixes one of the issues in #51905, where model outputs are full of errant
\characters.heres the problem: As the response is streamed back to zed, we accumulate the message chunks and and need to convert those chunks to valid json, to do that we use
partial_json_fixer::fix_json, when the last character of a chunk is\, thefix_jsonhas to escape that backslash, because its inside of a string (if it isn't, its invalid json and the tool call will crash) and other wise you would end up escaping the end"and everything would be messed up.why is this a problem for zed:
T_0 is the output at some step.
T_1 is the output at the next step.
the
fix_jsonsystem is meant to be used by replacing T_0 with T_1, however in the editor, replacing the entirety of T_0 with T_1 would be slow/cause flickering/etc.. so we calculate the difference between T_0 and T_1 and just add it to the current buffer state. So when a chunk ends on\, we end up with something like... end of line\\"}at the end of T_0,in T_1, this becomes
... end of line\n .... then when we add the new chunk from T_1, it just picks up after the \n because its tracking the length to manage the deltas.How to Review
utils.rs:
fix_streamed_json => remove trailing backslashes from incoming json streams so that
partial_json_fixer::fix_jsondoesn't try to escape them.other files: call fix_streamed_json before passing to
serde_jsonI had claude write a bunch of tests while I was working on the fix, which I have kept in for now, but the end functionality of fix_streamed_json is really simple now, so maybe these arent really needed.
Videos
Behavior Before:
before.mov
Behavior After:
after.mov
Self-Review Checklist
Release Notes: