[Platform] Add PartialJsonParser for streaming structured output#2113
Open
wachterjohannes wants to merge 5 commits into
Open
[Platform] Add PartialJsonParser for streaming structured output#2113wachterjohannes wants to merge 5 commits into
wachterjohannes wants to merge 5 commits into
Conversation
This was referenced May 23, 2026
Adds a static, dependency-free utility that recovers the largest valid structure from an incomplete JSON string. This unblocks rendering partial objects from streamed structured output before the model finishes emitting them. The implementation handles trailing commas, unclosed strings, dangling colons, partial true/false/null literals, and unclosed object/array structures. Ported from modelflow-ai's OptimisticJsonParser. Includes example, documentation under Structured Output, and an entry in the platform CHANGELOG.
4e4507c to
36b812d
Compare
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.
This PR adds
Symfony\AI\Platform\StructuredOutput\Streaming\PartialJsonParser, a static, dependency-free utility that recovers the largest valid structure from an incomplete JSON string. This unblocks rendering partial objects from streamed structured output before the model finishes emitting them.Algorithm
PartialJsonParser::parse()attempts a strictjson_decodefirst; on failure it applies best-effort fixes in order and tries again:]/}."and\byte-wise (UTF-8 safe because both are single-byte).nulltru,fals,nul, ...) → complete them{/[by walking the string with a stack that ignores chars inside strings.If still invalid, returns
nulland populates the&$errorMessagereference withjson_last_error_msg().Usage
Notes
ModelflowAi\Chat\Util\Json\OptimisticJsonParserStreamResult/ structured-output streaming.composer test,vendor/bin/phpstan analyse, andphp-cs-fixerpass cleanly on the new files.