Repository navigation
Description
Problem / Background
With stream: true, mlxcel sends nothing for a tool call until generation ends, then sends each call's whole argument string in one chunk. Agent clients that render or pre-validate arguments as they arrive see a long silent gap followed by a burst. Upstream now streams argument fragments as they are generated (Neywa-Labs/mlx-vlm#2502, https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/tools/streaming.py): Chat Completions sends them in choices[0].delta.tool_calls[i].function.arguments, Responses sends response.function_call_arguments.delta events, and the model's normal parser still validates the finished call.
Current Behavior
- Chat Completions: the streaming filter suppresses tool markup (
src/server/tool_calls/stream_filter.rs,ToolCallstate), the raw text accumulates incb.accumulated, and only after generationparse_tool_callsruns and each call is emitted as onetool_call_startchunk plus onetool_call_argumentschunk (src/server/routes/chat.rs:1830-1870). - Responses: same shape, after generation, one
OutputItemAdded, oneFunctionCallArgumentsDeltacarrying the full string,FunctionCallArgumentsDone,OutputItemDoneper call (src/server/routes/responses.rs:955-993).
Proposed Solution
Add src/server/tool_calls/incremental.rs with an IncrementalToolCallStreamer that is fed the same raw decoded text the routes already accumulate and returns events Start { index, name } and ArgsDelta { index, delta }. Both routes forward these as they occur, then run the existing end-of-generation parse_tool_calls as the authoritative result.
Formats in scope (the ones mlxcel models emit most and upstream also streams):
- JSON-bodied calls: Hermes
<tool_call>{"name":..,"arguments":{..}}</tool_call>, Mistral[TOOL_CALLS]name[ARGS]{..}and the JSON-list form, Llama 3 JSON.Startfires once thenamestring is complete;ArgsDeltacarries the raw text of theargumentsobject as it arrives. A string-encodedargumentsvalue is buffered until the string closes, then emitted as one delta. - Qwen3-Coder XML (
<function=..><parameter=..>): emit compact JSON ({"k":"v",..}, keys in generation order, matching whatextract_qwen_parametersserializes). String-typed parameter values (per the request schema, asminimax_m3_function_schemaresolves today) stream as JSON-escaped fragments; trailing whitespace is held back until more text arrives so the value-trim rule stays exact; non-string values are buffered until</parameter>and emitted aftercoerce_xml_param.
Every other format keeps today's one-shot behavior, through the same final emission code.
Rules:
tool_choicewith a named function (feat(server): enforce tool_choice required and named-function semantics on chat completions #1319): noStartis sent for a call until its name is known; a call whose name does not match is never streamed.count_tool_calls_reaching_clientand thefinish_reasonlogic stay authoritative.- Chat Completions: the concatenation of a call's deltas must equal the final
argumentsstringbuild_tool_call_responseswould have produced. For JSON-bodied calls that means the final string is the raw model text (validated to parse as a JSON object), not a re-serialization. - Responses:
function_call_arguments.doneand the final output item carry the authoritative parsed arguments. - If the final parse disagrees with what was streamed (different call count or name), log a
tracing::warn!with the request id, emit the final events from the parsed result, and do not emit further deltas for the disagreeing call.
Scope
In scope: incremental.rs (new, with incremental_tests.rs), routes/chat.rs streaming path, routes/responses.rs streaming path, docs (docs/responses-api.md streaming section and the chat tool-call docs).
Out of scope: /v1/messages (Anthropic input_json_delta), formats other than the ones listed, non-streaming responses.
Acceptance Criteria
- Unit tests feed a Hermes call, a two-call Mistral
[ARGS]reply, and a Qwen3-Coder call with a string and an integer parameter, one character at a time; the concatenated deltas per call equal the final parsedarguments, and at least two deltas are produced for the string parameter before</parameter>. - A named
tool_choicetest: a reply calling a non-chosen function first produces no deltas for that call. - A route-level test (pattern of
src/server/muse_atem_stream_chat_tests.rs/muse_atem_stream_responses_tests.rs) shows more than one argument delta chunk for a single call on both endpoints. - Integrated:
mlxcel-server -m mlx-community/Qwen3-Coder-30B-A3B-Instruct-4bitwith the OpenAI Python SDK streaming a tool call on/v1/chat/completionsand/v1/responsesshows incremental argument deltas, and the accumulated arguments parse as JSON. (needs a Metal host; not run in feat(server): stream tool-call arguments incrementally on Chat and Responses #2504)
Verification
cargo test --profile test-fast -p mlxcel tool_calls::incremental
cargo test --workspace --profile test-fast --features metal,accelerate
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
Activity
test(server): cover two streamed Qwen3-Coder calls
fix(server): step over whole characters in the tool-call streamer
fix(server): contain tool-call streamer panics on untrusted output
fix(server): build the streamed Responses output from the streamed items
docs: document raw tool-call arguments, panic containment and Respons…
feat(server): stream tool-call arguments incrementally on Chat and Re…
fix: keep non-Inkling Chat live streaming and spaced Inkling deltas
fix: preserve GLM string arguments and stream Inkling tool calls (#3653)