fix: wrap optimization handler responses in SSE streaming events#383
Open
Klausc06 wants to merge 2 commits intoAlishahryar1:mainfrom
Open
fix: wrap optimization handler responses in SSE streaming events#383Klausc06 wants to merge 2 commits intoAlishahryar1:mainfrom
Klausc06 wants to merge 2 commits intoAlishahryar1:mainfrom
Conversation
Optimization handlers (prefix detection, quota check, title skip, suggestion skip, filepath mock) previously returned plain JSON MessagesResponse, but Claude Code always sends stream=True. This caused "Stream completed without receiving message_start" errors and forced non-streaming fallbacks on every optimized request. Convert optimization results to proper Anthropic SSE event streams (message_start → content blocks → message_delta → message_stop) using json.dumps() for safe serialization. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…itation Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Optimization handlers (fast-prefix detection, quota check, title generation skip, suggestion skip, filepath mock) previously returned a plain JSON
MessagesResponsedirectly. But Claude Code always sendsstream=True, so the client expects Anthropic SSE event stream format. This mismatch causedStream completed without receiving message_starterrors and forced non-streaming fallbacks on every optimized request.This PR adds
_messages_response_to_sse_stream()which converts aMessagesResponseinto a proper SSE event stream (message_start→content_block_start→content_block_delta→content_block_stop→message_delta→message_stop), and wraps optimization results through it.中文说明
优化处理器(前缀匹配、配额检查、标题生成跳过、建议跳过等)之前直接返回了普通的 JSON
MessagesResponse。但 Claude Code 客户端始终发送stream=True,期望收到 Anthropic SSE 事件流格式。这个不匹配导致了Stream completed without receiving message_start错误,并且每次优化命中的请求都会触发非流式降级。本次 PR 新增了
_messages_response_to_sse_stream()函数,将MessagesResponse转换为标准的 SSE 事件流,并包裹所有优化处理器的返回结果。Test plan
uv run pytest tests/api/test_routes_optimizations.pypassestext/event-stream; charset=utf-8content typemessage_start/content_block_*/message_delta/message_stopeventsStream completed without receiving message_starterrors in Claude Code client logsFiles changed
api/services.pytests/api/test_routes_optimizations.py🤖 Generated with Claude Code