feat(go/ai): add streaming support for GenerateData with partial JSON parsing #3221
+496
−1
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.
Description
This PR adds streaming support for
GenerateData
in Genkit Go, enabling partial JSON parsing during streaming responses.Background
When using
GenerateData
with streaming callbacks, the following error occurs:Bad stream: SyntaxError: Unexpected end of JSON input
This happens because
GenerateData
doesn't support parsing partial JSON during streaming, which is a common use case when generating structured data (like menus, configurations, etc.) with streaming enabled for better user experience.Solution
Following the JavaScript implementation approach, this PR implements:
js/ai/src/extract.ts
ParseChunk
method to all formatters to handle streaming dataModelResponseChunk
to store parsed partial data, matchingjs/ai/src/generate/chunk.ts
Example
This allows for an implementation as per the document: https://genkit.dev/go/docs/flows/#streaming-flows
This brings feature parity with the JavaScript SDK and resolves the TODO comment in the code.
Test Results
All tests pass successfully:
Checklist (if applicable):