fix(go/ai): fixed bad stream message format parsing #3573
Merged
+509
−64
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.
Fixes #3569
This PR fixes the "unexpected end of JSON input" error that occurs when combining streaming output (
ai.WithStreaming
) with structured response parsing (resp.Output(&struct)
) in Firebase Genkit.Problem
When using streaming responses with structured output formats (JSON, JSONL, Array, Enum), the content gets fragmented across multiple
ai.Part
objects. The format handlers were processing each part individually, expecting complete format-compliant content, but receiving only fragments. This caused parsing failures when trying to validate incomplete JSON/JSONL/Array/Enum values.Root Cause
In streaming scenarios, model responses are split into multiple parts:
Each format handler was validating individual parts, but fragments like
{"name"
are not valid JSON.Solution
Updated all format handlers to:
Changes Made
format_json.go
: Accumulate text parts before JSON extraction and validationformat_jsonl.go
: Combine text before extracting JSONL linesformat_array.go
: Merge text content before processing JSON objectsformat_enum.go
: Concatenate text before enum validationformat_text.go
: No changes needed (already works correctly)Testing
Added comprehensive streaming test cases covering:
Impact
Checklist