Skip to content

feat: Stream tool-call arguments incrementally on Chat and Responses #2396

Closed

Description

@inureyes

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, ToolCall state), the raw text accumulates in cb.accumulated, and only after generation parse_tool_calls runs and each call is emitted as one tool_call_start chunk plus one tool_call_arguments chunk (src/server/routes/chat.rs:1830-1870).
  • Responses: same shape, after generation, one OutputItemAdded, one FunctionCallArgumentsDelta carrying the full string, FunctionCallArgumentsDone, OutputItemDone per 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. Start fires once the name string is complete; ArgsDelta carries the raw text of the arguments object as it arrives. A string-encoded arguments value 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 what extract_qwen_parameters serializes). String-typed parameter values (per the request schema, as minimax_m3_function_schema resolves 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 after coerce_xml_param.

Every other format keeps today's one-shot behavior, through the same final emission code.

Rules:

  • tool_choice with a named function (feat(server): enforce tool_choice required and named-function semantics on chat completions #1319): no Start is sent for a call until its name is known; a call whose name does not match is never streamed. count_tool_calls_reaching_client and the finish_reason logic stay authoritative.
  • Chat Completions: the concatenation of a call's deltas must equal the final arguments string build_tool_call_responses would 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.done and 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 parsed arguments, and at least two deltas are produced for the string parameter before </parameter>.
  • A named tool_choice test: 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-4bit with the OpenAI Python SDK streaming a tool call on /v1/chat/completions and /v1/responses shows 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

  1. added
    status:ready
    type:enhancement
    priority:medium
    area:inference
    on Oct 16, 2026
  2. self-assigned this
    on Oct 20, 2026
  3. added
    status:in-progress
    and removed
    status:ready
    on Oct 20, 2026
  4. added
    status:review
    and removed
    status:in-progress
    on Oct 20, 2026
  5. added
    status:done
    and removed
    status:review
    on Oct 20, 2026
  6. added a commit that references this issue on Oct 21, 2026
    8f30824
  7. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencepriority:mediumstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions