feat(provider): repair malformed SSE JSON via jsonrepair#25385
feat(provider): repair malformed SSE JSON via jsonrepair#25385water-in-stone wants to merge 1 commit into
Conversation
|
@kitlangton PTAL |
d2c17f5 to
4bb5364
Compare
Some OpenAI-compatible providers (observed with Z.AI GLM-5.1 and Qwen) occasionally emit SSE data frames whose JSON is malformed - unescaped quotes inside content, or the next frame spliced mid-field. The AI SDK's parseJsonEventStream then throws and terminates the whole stream. Add a byte-level TransformStream wrapper that, for each SSE event, strict-parses every data: payload first and only falls back to jsonrepair on failure. Repaired payloads are round-trip validated before being written back; unrepairable chunks pass through unchanged so the downstream parser still surfaces the original error. Opt-in via experimental.enable_sse_json_repair (default false) to avoid any behavior change for users not hitting this bug.
4bb5364 to
e9853a2
Compare
|
@rekram1-node I have created a MR with thorough testing. PTAL. |
|
@thdxr I have created a MR with thorough testing. PTAL. If you're short on time to review this MR, consider using AI for the code review. By the way, I have already used Claude Code to review this MR. |
|
@nexxeln I have created a MR with thorough testing. PTAL. |
|
If you guys don't care about the community's voice at all, then stop wasting out time with open source. |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #25247, #23442
Type of change
What does this PR do?
Some OpenAI-compatible providers (observed with Z.AI GLM-5.1 and Qwen) occasionally emit SSE data frames whose JSON is malformed - unescaped quotes inside content, or the next frame spliced mid-field. The AI SDK's parseJsonEventStream then throws and terminates the whole stream.
Add a byte-level TransformStream wrapper that, for each SSE event, strict-parses every data: payload first and only falls back to jsonrepair on failure. Repaired payloads are round-trip validated before being written back; unrepairable chunks pass through unchanged so the downstream parser still surfaces the original error.
Opt-in via experimental.enable_sse_json_repair (default false) to avoid any behavior change for users not hitting this bug.
by the way, jsonrepair is a popular tool used for fixing malformed JSON text from LLMs. Its key features include:
How did you verify your code works?
Origin malformed JSON text:


Valid JSON text after
jsonrepairAll the unit tests under

packages/opencode/test/provider/repair-sse.test.tshave passed.Checklist
If you do not follow this template your PR will be automatically rejected.