fix: add SSE streaming support to MLX engine#3
Open
Turcy76 wants to merge 1 commit into
Open
Conversation
- Add generate_stream() using mlx_lm.stream_generate() - Add _handle_chat_stream() for SSE response format - Fix model_alias case mismatch (9b -> 9B) for proper detection - Extract clean_response() as shared utility Fixes agent.py showing no response when using MLX backend (Option B), because stream_llm() sends stream=True but MLX engine only returned a single JSON blob instead of SSE events.
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.
Problem
When using
mlx_engine.pyas the backend (Option B),agent.pyshows no response for chat messages.The root cause is that
agent.py'sstream_llm()sends"stream": trueand expects SSE (Server-Sent Events) format, butmlx_engine.pyonly returns a single JSON response, ignoring thestreamparameter.Additionally, the model detection shows
auto ?because/propsreturns lowercase"9b"whileagent.pychecks for uppercase"9B".Changes
generate_stream()— New streaming generator usingmlx_lm.stream_generate(), with filtering for<think>tags and special tokens_handle_chat_stream()— SSE response handler that sendsdata: {...}\n\nchunks followed bydata: [DONE]_handle_chat()— Now checksstreamparameter and routes to streaming or normal handlerclean_response()— Extracted shared cleanup logic (strip special tokens + thinking tags)/propsendpoint — Fixedmodel_aliasto use uppercase (9B/35B) to matchagent.py's detectionTesting
Tested locally with MLX + Qwen3.5-9B-MLX-4bit on Apple Silicon M4:
agent.pynow displays streamed responses in bothautoandrawmodesauto 9binstead ofauto ?/benchcommand works correctly