fix(model/openrouter): fix missing extra content (images) in SSE chunks #666
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.
Fix issue where extra content like images were being filtered out in streaming responses due to empty-check happening before modifier execution.
Key changes:
This ensures that streaming responses can include images and other extra content that modifiers add to messages, even when the main content field is empty.
What type of PR is this?
fix
Check the PR title.
(Optional) Translate the PR title into Chinese.
修复(model/openrouter): 修复 SSE 流式响应中图片等额外内容丢失的问题
(Optional) More detailed description for this PR(en: English/zh: Chinese).
en:
Problem: When using OpenRouter's streaming API, extra content (such as images) added by response modifiers were being incorrectly filtered out. This occurred because the empty-message check ran
before modifiers had a chance to attach their content.
Root Cause: The streaming pipeline checked if messages were empty (
Content == ""andToolCalls == 0) before executingResponseChunkMessageModifier, causing chunks with modifier-addedcontent to be discarded.
Solution: Reorder the execution pipeline to run modifiers first, then check for emptiness. Additionally, include
AssistantGenMultiContentin the empty-check to properly handle multimodalcontent.
Technical Improvements:
jsonitertosonic/astfor more robust JSON parsingchoicesarrayImpact: Users can now receive images and other extra content in streaming responses without data loss.
zh(optional):
问题: 使用 OpenRouter 流式 API 时,ResponseModifier添加的额外内容(如图片)被错误地过滤掉了。这是因为空消息检查在Modifier有机会附加内容之前就执行了。
根本原因: Chunk Stream在执行
ResponseChunkMessageModifier之前就检查消息是否为空(Content == ""且ToolCalls == 0),导致包含Image内容的消息块被丢弃。解决方案: 调整Stream的执行顺序,先运行Response Modifier,然后再检查是否为空。此外,在空检查中包含
AssistantGenMultiContent以正确处理多模态内容。技术改进:
jsoniter迁移到sonic/ast以实现更稳健的 JSON 解析choices数组添加适当的空值和类型验证影响: 用户现在可以在流式响应中接收图片和其他额外内容,不会丢失数据。
(Optional) Which issue(s) this PR fixes:
N/A
(optional) The PR that updates user documentation:
N/A