Repository navigation
fix(server): hide Llama 3.x python_tag calls, add Responses reasoning - #2496
Conversation
Llama 3.x `<|python_tag|>` tool calls streamed their raw JSON as `delta.content` and then again as `delta.tool_calls`, because the stream filter had no row for the marker. `CHAT_DELIMITERS` now enters tool-call state on `<|python_tag|>` and leaves it on `<|eom_id|>` or end of stream. The non-streamed cleanup drops the same block, and `try_llama3` accepts the tag after visible text and stops at `<|eom_id|>`, so both shapes agree. The bare-JSON form has no marker and is still not held back. `/v1/responses` read reasoning only from `<think>` forms and hard-coded `reasoning_tokens: 0`. `split_reasoning` now reads the sources the chat route uses: the parser's `reasoning_content` (Harmony `analysis`), then the `<think>` extraction, then the `StreamFilter` split. Both arms count `reasoning_tokens` by encoding their reasoning item text with the model tokenizer. New parity tests in stream_parity_tests.rs and unit tests in responses.rs fail without the fix. tool_calls, responses, muse_atem and reasoning_echo lib tests and clippy pass on a CPU-only build. Refs #2470
The new StreamFilter fallback in split_reasoning returns the whole output as reasoning when a primed thinking block never closes (max_tokens hit mid-thought), but the visible text still carried the same output, so the non-streamed reply surfaced it twice: once as the reasoning item and once as output_text. The streamed reply already sends it as reasoning only. split_reasoning now applies the chat route's strip_unclosed_primed_thinking to the visible text, so both arms of /v1/responses match the chat route. A regression test covers the primed unclosed case with and without parsed tools. Refs #2470
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
extract_reasoning_content fed the whole generation into one StreamFilter::feed call. The filter rescans its full pending buffer for every delimiter on each hit, so a delimiter-dense reply was O(hits x length): 32k repeated <|python_tag|> took 42 s and 32k <tool_call> took 80 s on the async request path. This PR made both the new Llama 3.x markers and the non-stream /v1/responses path (split_reasoning) reach it. The replay now feeds 128-byte chunks cut on UTF-8 char boundaries, the same shape as the per-token streaming path, and the same inputs finish in about 0.29 s. Filter internals are unchanged. A new test asserts chunked output equals a per-char feed for delimiter-dense, multibyte and Harmony inputs. Validated with the tool_calls, routes::chat, routes::responses, responses, chat_request, muse_atem and reasoning_echo suites, clippy -D warnings and cargo fmt --check. Refs #2470
|
Security and performance review Fixed:
Remaining (not fixed here):
Cleared: UTF-8 slicing (all cuts come from ASCII Gates: the tool_calls, routes::chat, routes::responses, responses, chat_request, muse_atem and reasoning_echo suites pass, and clippy -D warnings and fmt --check are clean. |
The non-stream `/v1/responses` arm encoded the reasoning item on the tokio executor thread. Tiktoken BPE is quadratic per piece (a 25k-char punctuation run takes about 26 s), so a long reasoning item on a Kimi K3 or HunYuan checkpoint could stall a worker. The handler has no blocking section to fold the count into, so it now runs `count_reasoning_tokens` through `tokio::task::spawn_blocking`. The counting itself is unchanged, and the stream arm already counts inside its own blocking task, so the two arms stay equal. A new async test checks that the blocking-pool count equals the direct count. Validated with the responses, server::responses and tool_calls lib tests, clippy -D warnings, and cargo fmt --check on a CPU-only build. Refs #2470
Describe the #2470 behavior changes: Llama 3.x python_tag calls are held back from streamed content and non-JSON python_tag blocks are dropped in both modes (docs/server-features.md), and /v1/responses returns Harmony reasoning as a reasoning output item with a real reasoning_tokens count (docs/responses-api.md). Add the CHANGELOG Fixed entry. Validation: cargo test --profile test-fast -p mlxcel --lib for server::tool_calls:: (457), server::routes::responses (25) and server::responses (56) pass; clippy --lib --tests -D warnings and cargo fmt --check are clean. Refs #2470
Implementation hardeningReviewed PR #2496 and the full issue #2470 thread. Follow-up PR #2498, commit
|
## Summary Follow-up hardening for merged [PR #2496](#2496), reviewed against [issue #2470](#2470). Non-streamed Responses could insert parser-only newlines between Harmony analysis segments or omit later think blocks, making reasoning text and token counts differ from streaming. Both modes now use the existing bounded streaming filter before legacy fallbacks. Regression tests cover both formats with and without tools, and the Responses documentation describes the common accumulation rule. Security review found no additional authorization or input-boundary gaps. Tagged tool-call suppression remains intact. Performance review confirms bounded UTF-8 replay and token counting off the async executor are preserved; no separate Harmony parser was introduced. ## Validation - CUDA tool-call, Responses and reasoning regressions: 580 passed; delimiter-dense reasoning replay: 1 passed. - `cargo clippy -j 4 -p mlxcel --lib --tests -- -D warnings`, formatting and diff checks passed; clippy used an independent target directory to exclude stale worktree artifacts. - Real GB10 HTTP tests passed: Llama 3.1 8B 4-bit produced one tool call without raw call content; GPT-OSS 20B MXFP4 returned identical reasoning and 44 reasoning tokens in both Responses modes. - The multi-segment Responses mismatch disclosed in the original PR is fixed. Its unmarked bare-JSON tool-call and chat whitespace caveats remain outside this correction. Refs #2470 Refs #2496
Summary
<|python_tag|>calls no longer stream asdelta.content.CHAT_DELIMITERSenters tool-call state on<|python_tag|>and exits on<|eom_id|>or end of stream.clean_content_markersdrops the same block, so no-tools and unparsed calls match the streamed output (feat(tokenizer): Kimi K3 tiktoken family and native XTML renderer #1743 rule).try_llama3now also accepts the tag after visible text, which becomes the content, and stops at<|eom_id|>./v1/responsesemits areasoningitem for gpt-oss Harmony.split_reasoningreads the chat route's sources in this order:parse_tool_calls(...).reasoning_content, then the existing<think>extraction, then theStreamFiltersplit (chat::extract_reasoning_content, which already routes Harmony). No second Harmony parser was added. The streamed arm already routedanalysisto reasoning through the filter.output_tokens_details.reasoning_tokensis the reasoning item's text, trimmed and encoded with the model tokenizer. Both arms use the same encoding. The parity suite shows streamed and non-streamed reasoning text are equal, so the counts are equal.Not held back: the bare-JSON Llama 3.x form (
{"name": ..., "parameters": ...}with no tag). It has no marker. The filter would have to hold back every{until end of stream to classify it, which would stall every JSON-shaped answer. It still streams as content and is then parsed intotool_calls.Behavior note: a
<|python_tag|>block that is not JSON (Llama's built-in code interpreter) is now dropped from content, streamed and non-streamed, like other tool markup.Changes during review
<think>(27928305). When generation stopped mid-thought, the non-streamed reply sent the same text as thereasoningitem and asoutput_text.split_reasoningnow strips unclosed primed thinking from the visible text through the chat route'sstrip_unclosed_primed_thinking.82846066).extract_reasoning_contentfed the whole reply to oneStreamFilter::feed, which rescans its buffer on every delimiter hit. 32k repeated markers took 35 to 80 s. It now feeds 128-byte chunks cut on UTF-8 boundaries (about 0.3 s for the same inputs). A test pins chunked output to per-character output.9af71a31). The non-stream arm now counts throughspawn_blocking, as the stream arm already did. Tiktoken BPE is quadratic per piece, so a pathological reply could block a tokio worker.b7327309):docs/responses-api.md,docs/server-features.mdand a CHANGELOG entry.Known gaps, not in this PR: multi-segment Harmony analysis is joined with
\nby the parser but with nothing byStreamFilter. Whitespace right before a tool-call marker streams as content, while the non-streamed reply trims it. Both are carried into #2464.Test plan
llama3_python_tag_*, 4 tests) coverchunkings(). They fail when the stream_filter/parser/formats changes are stashed.responses.rstests (Harmony split with and without parsed tools,reasoningoutput item, streamed vs non-streamedreasoning_tokensfor<think>, primed close-only and Harmony, zero case). They fail whensplit_reasoningis reverted to main's logic and the counter returns 0.cargo test --libforserver::tool_calls::,server::routes::responses,server::responses,server::muse_atem,server::reasoning_echo, pluscargo clippy --lib --tests -D warnings. All pass on a CPU-only macOS build (MLXCEL_BUILD_METAL=0).Resolves #2470