fix: handle streaming requests in chat_handler#5
Merged
Conversation
chat_handler always collects the full provider response and returns JSON, but when a client sends stream=true, two things break: 1. process_chat only sets stream=false when the field is absent. If the client includes stream=true, it passes through to the upstream provider (e.g. Ollama), which returns SSE instead of JSON. response_json then fails to parse the SSE body, raising "Expecting value: line 1 column 1 (char 0)" and returning 500. 2. Even when the provider call succeeds, the response is returned as plain JSON. Streaming clients (e.g. Vercel AI SDK's @ai-sdk/openai-compatible) expect Server-Sent Events with chat.completion.chunk objects containing a delta field, so they silently discard the unexpected JSON and report no response. Fix both by: - Unconditionally forcing stream=false in process_chat so providers always return a parseable JSON response. - Detecting the client's original stream preference in chat_handler and, when true, converting the JSON response to a single SSE chunk in the chat.completion.chunk format before sending it back.
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
stream=falseunconditionally inprocess_chat()so upstream providers always return parseable JSON instead of SSEstreampreference inchat_handlerand convert the JSON response to SSE chunks (chat.completion.chunkwithdeltafield) when the client requested streamingCloses #4
Context
chat_handlerhas never supported streaming — it collects the full response and returnsweb.json_response(). Butprocess_chat()only defaultedstream=falsewhen the field was absent, so clients sendingstream=truetriggered two failures:response_json()couldn't parse → 500This broke OpenClaw's embedded agent, which uses
@ai-sdk/openai-compatibleand defaults tostream=true.Test plan
stream=falserequest returns JSON as beforestream=truerequest returns proper SSE withchat.completion.chunkobjects