fix(genai): correct Anthropic SSE event-type filtering and text path …#1606
Open
Irfan-del-droid wants to merge 1 commit intofoss42:mainfrom
Open
fix(genai): correct Anthropic SSE event-type filtering and text path …#1606Irfan-del-droid wants to merge 1 commit intofoss42:mainfrom
Irfan-del-droid wants to merge 1 commit intofoss42:mainfrom
Conversation
…extraction - Track SSE event: type across chunks in streamGenAIRequest - Pass eventType through getFormattedStreamOutput -> streamOutputFormatter - Fix AnthropicModel.streamOutputFormatter: check payload type and extract text from x['delta']['text'] instead of x['text'] - Suppress null/empty formatter outputs before sink.add - Align streamOutputFormatter signature across all providers - Add flutter_driver dev dependency for integration_test driver Fixes foss42#1592
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.
Summary
Fixes #1592 — Anthropic streaming was silently broken in two ways:
AnthropicModel.streamOutputFormatterreturnedx['text']which isalways
null. The actual text lives atx['delta']['text'].streamGenAIRequestignored SSEevent:field lines entirely andforwarded every
data:line to the formatter — includingping,message_start,content_block_start, etc. — causing null noisein the output stream.
Changes
📦 Files Changed —
packages/genai/lib/models/model_provider.dart{String? eventType}parameter tostreamOutputFormatterlib/models/ai_request_model.dartgetFormattedStreamOutputto accept and forwardeventTypelib/utils/ai_request_utils.dartevent:type across chunk boundaries usinglastEventTypeeventTypetogetFormattedStreamOutputlastEventTypeon empty lines (SSE block boundaries)\r\nline endings withtrimRight()null/ empty formatter results beforesink.addlib/interface/model_providers/anthropic.dart⭐ Core fix_kContentBlockDelta,_kTextDeltaeventTypeis notcontent_block_deltax['type']as fallback wheneventTypeis absentdeltais aMapdelta['type'] == 'text_delta'before extracting textx['text']→x['delta']['text']nullfor all non-text events (ping,message_start, etc.)lib/interface/model_providers/openai.dart{String? eventType}(unused, no logic change)lib/interface/model_providers/gemini.dart{String? eventType}(unused, no logic change)lib/interface/model_providers/azureopenai.dart{String? eventType}(unused, no logic change)test/genai_test.darttest/streaming/anthropic_stream_formatter_test.dartcontent_block_deltaeventsevent:fast-pathtypefieldRoot
pubspec.yamlflutter_driver: sdk: flutterdev dependencyintegrationDriverbeing undefined intest_driver/integration_test.dartRoot Cause
Anthropic's SSE wire format emits multiple event types per response: