Skip to content

[ATOM gRPC] atomesh: structured output to ATOM gRPC workers; answers as ATOM's HTTP routes give them - #3677

Open
Jasen2201 wants to merge 94 commits into
jasen/atom-grpc-e-mesh-discovery-pdfrom
jasen/atom-grpc-f-mesh-chat-render
Open

Jasen2201 wants to merge 94 commits into
jasen/atom-grpc-e-mesh-discovery-pdfrom
jasen/atom-grpc-f-mesh-chat-render

Conversation

@Jasen2201

@Jasen2201 Jasen2201 commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Part F of the 8 PRs proposed in #3657 (regrouped by function). No blocking issues.

Based on E, which it needs.

Why

A chat to an ATOM gRPC worker reached ATOM without its structured-output input. Its answers also differed from ATOM's HTTP ones in stops, end-of-sequence text, reasoning, forced tool calls, usage and stream endings.

What changed

  • An ATOM worker's reply (D's for a pair) is read through AtomOutput, shared with main's bridge ([atomesh] Rust frontend down to LLMEngine, replacing the Python standalone service #3698). No stop cuts it, the end ids are hidden, the prompt's reasoning opener (or the model's implicit one) is read first, and the worker's parsers apply.
  • It takes ATOM HTTP's shape: empty content: "", the opening empty delta, and ATOM's usage fields (render/atom_usage.rs), streamed where the worker's usage_on_finish puts them. A forced tool_choice is parsed in the model's syntax.
  • For ATOM workers, prepare_chat fills a StructuredOutput, with every message's tools and thinking read from the chat's body as sent (prepare::sent_chat_body, kept also for a model with an ATOM worker). Media and /v1/responses are refused; a completion's logprobs answer null.
  • For every backend, streams decode a Complete's unstreamed ids with their logprobs, unary tool arguments follow the declared schemas, and cached tokens are reconciled against the route estimate.

Size: over 1,000 lines and 15 files: one unit, a chat to ATOM answered as ATOM's HTTP chat answers it.

Review focus

  1. AtomOutput keys on the producing worker's runtime, or on a Pipeline built with reading_output_as_atom.
  2. prepare::sent_chat_body keeps a chat's body as sent for a model with an ATOM worker too; prepare_chat_with reads its message tools and thinking from it.
  3. The SSE goldens differ from E's only in the final "." the Complete carries.

Testing

Each change has a test that fails without it; see Evidence.

jasen/atom-grpc-f-mesh-chat-render @ f2453c2: all tests pass; fmt clean; 5 new clippy warnings.

Follow-ups

none

What changed, in detail
  • AtomOutput (grpc/pipeline.rs): none of the request's stops cut the reply; the eos_token_ids GetModelInfo lists are hidden stop ids unless ignore_eos; ResponseContext.prompt_opener is the reasoning_open_markers entry the prompt ends on, fed to the reasoning parser before the reply (unary replies over 64 KiB are parsed piece by piece); parsers are the router's flags, else the worker's, else the model-name mapping.
  • render/atom_usage.rs: unary chat: cached tokens and ttft_s / tpot_s / latency_s; streamed chat: cached tokens on a usage chunk sent after the finish chunk, include_usage or not; unary completion: timing (carried in /generate's meta_info, serialized through MetaInfoTiming without rebuilding the reply); n>1: the prompt and its cached tokens once, the slowest timing, num_choices.
  • Native tool calls: an ATOM reply cut off inside one is closed by its tool parser's end-of-input rule (ToolParser::end_of_input_closers; Qwen XML and MiniMax), as ATOM's HTTP parser keeps such a call; a <tool_call> is not closed past text that follows its </function>. In an ATOM worker's reply only (ToolParser::read_as_atom), atomesh's Qwen XML parser also reads a bare <function=...> call outside a <tool_call> wrapper, as ATOM's HTTP parser does, whole or cut off after its declared name, in a parameter's name or in a tag after a closed parameter; a bare header's name ends at a <, as the unary pattern reads it; one that prose quotes, or that names no declared tool, stays text, and so does a <tool_call> no <function= follows; the reply's text is all of it outside its calls. Text the parser holds at end of input as the start of a call that never came is sent as content (ToolParser::end_of_input_text; Qwen XML and MiniMax), so the streamed reply's text is the unary one's. SGLang's and vLLM's replies read as on main, except that, for every backend, streamed Qwen XML calls arriving in one delta each carry their own arguments, {} for a call without parameters.
  • Streamed reasoning: for every backend, the reasoning parser reads each delta token by token, as the unary reply does, so a delta of several ids, or a replayed 256-id piece, may end inside a delimiter.
  • Message tools: MessageTools::read on the body as sent, sent in message_tools_json, given to the template per message, and added to the tool list the reply is parsed against. A named or required tool_choice still needs a top-level tool (ingress validation, all routers).
Stack

ATOM serves atom.grpc.v1.AtomEngine beside its HTTP server (token ids in, token ids out, ATOM's P/D flow unchanged), and atomesh routes to ATOM gRPC workers over a Generate call per request or one Session per instance. One linear stack, merged bottom to top: A, B, C, D, E, F, G, H; each PR's base is the one before it.

Evidence

In atom/mesh (rustc and clippy 1.100.0), at f2453c2: cargo fmt --check and cargo clippy --all-targets with and without atom-engine pass.

cargo test --features atom-engine --lib            1550 passed, 0 failed, 8 ignored
cargo test --features atom-engine --test <each>    304 passed, 0 failed, 6 ignored
cargo test --lib                                   1545 passed, 0 failed, 8 ignored
cargo test --test <each>                           304 passed, 0 failed, 6 ignored

Clippy against E (warnings matched by message and file):

[clippy] base 111, branch 116, new 5, gone 0
[clippy-ae] base 111, branch 116, new 5, gone 0

The five: result_large_err on three functions returning a Response error, as their neighbours do, and two field_reassign_with_default in render tests. The fixes at f2453c2 add none.

test_load_counter_performance (untouched file) and the fixed-port routing_tests fail now and then on this shared host at load average 50-140; each passes on rerun.

Tests that fail without their change: atom_output_tests (stops kept, end ids hidden, the worker's parsers under an alias), f_atom_reasoning_opener, the ATOM and SGLang usage tests in render/tests.rs (n=2 included) and h_completion_adapter, test_streaming_atom_output_without_text_opens_with_an_empty_content, g_complete_suffix, h_atom_native_tool_calls (cut-off and bare calls, the streamed text matching the unary one, and bare calls read as text for other backends, included), the Qwen XML parser's a_quoted_wrapper_before_a_bare_call_stays_text, calls_in_one_delta_stream_their_own_arguments, text_after_a_cut_off_wrapper_s_function_is_not_closed_into_the_call, a_held_opener_no_call_followed_is_text_at_end_of_input, a_bare_call_naming_no_declared_tool_is_text, a_call_after_an_unclosed_bare_header_is_read and text_between_and_after_calls_is_text, MiniMax's a_held_partial_marker_is_text_at_end_of_input, a_long_reply_arriving_in_its_complete_alone_is_split_as_when_streamed (a closer cut at the 256th id), an_atom_chat_carries_the_tools_a_message_of_any_role_declares, the refusal table run through ATOM's request builder, and k_cache_reconcile. test_generate_aggregator_appends_atom_s_timing_to_meta_info pins a timed /generate reply's bytes, before and after its serialization changed.

🤖 Generated with Claude Code

…as ATOM's HTTP routes give them

For a chat served by ATOM gRPC workers, atomesh sends ATOM the chat's
structured-output input with the request: tools, tool_choice and
response_format as the client sent them, each message's own tools in
`message_tools_json`, the merged template kwargs the prompt was rendered
with (`merged_template_kwargs`), and whether the rendered prompt ends in
one of the worker's reasoning markers. SGLang and vLLM requests are
unchanged.

For a model with an ATOM gRPC worker, the gRPC routers answer 400
`unsupported_media` to a chat or `/generate` carrying image, video, audio
or embedding input, before preparing it, since ATOM receives only the
rendered token ids; the regular gRPC router answers 501
`unsupported_api` to `/v1/responses` for such a model, and
`/v1/messages` keeps its 501. SGLang and vLLM workers serve media and
Responses as before.

atomesh records how long a gRPC request's chat template and encode take,
as `mesh_prepare_stage_seconds` by stage, beside the HTTP ingress's own.

atomesh's gRPC `/generate` stream decodes each chunk through the
request's stop decoder, as the chat stream and the unary response do, and
flushes it when the stream completes, so a character split across chunks
is no longer lost and both `/generate` and `/v1/completions` streams join
to the unary text; the streamed text also honours stop, stop_token_ids,
skip_special_tokens and no_stop_trim. The generate SSE goldens are
re-recorded with the restored spaces.

The unary chat answer and the streamed usage chunk report the prefix-cache
hit ATOM sends on each Complete in `usage.prompt_tokens_details.cached_tokens`,
as ATOM's HTTP chat does. The protocol crate's chat `Usage` has no field
for it, so `render::with_cached_tokens` adds it to the answer's JSON.

An ATOM worker ends an output where a stop string's own ids match, and
ATOM's HTTP routes return all of it. atomesh decodes the output of an ATOM
worker, or an ATOM D, with none of the request's stops
(`drop_stops_for_atom`), so a stop string the model writes with other ids
no longer cuts the text, inside reasoning or content, where ATOM's HTTP
chat keeps it.

A gRPC chat answer always carries `message.content`, "" when the output
has none, and a streamed chat opens on the engine's first message with a
delta carrying the role and `"content": ""`, as ATOM's HTTP chat does.
The chat SSE goldens are re-recorded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Jasen2201 and others added 5 commits November 22, 2026 13:44
…t and render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…omesh structured output and render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… workers

ATOM's Generate carries no logprobs, so a chat with `logprobs` or a /generate
with `return_logprob` came back with null logprobs where ATOM's HTTP chat
route returns them. For a model with an ATOM gRPC worker, the regular and P/D
gRPC routers now answer such a request 400 `unsupported_logprobs`, beside the
media refusal. /v1/completions keeps its null logprobs, as ATOM's HTTP
/v1/completions answers them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Jasen2201 added a commit that referenced this pull request Nov 22, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Jasen2201 added a commit that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jasen2201
Jasen2201 added this pull request to stack #3735 November 23, 2026 07:59
Copilot AI balanced review requested due to automatic review settings November 23, 2026 09:12
Jasen2201 and others added 4 commits November 24, 2026 17:03
…s end

The MiniMax parser holds a trailing partial namespace token or `<tool_call>`
outside a call until more text arrives. At end of input nothing released it, so a
streamed ATOM reply ending in `The prefix is <tool_c` lost `<tool_c`, which the
unary reply keeps. Its `end_of_input_text` now returns that text outside a call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…as streamed

The unary read of an ATOM reply took any bare `<function=NAME>...</function>`
for a call, so prose such as `The format is <function=YOUR FUNCTION NAME></function>.`
became a call named `YOUR FUNCTION NAME`, while the streamed reply, which reads
a bare call only when it names a declared tool, kept it as text. The unary read
now asks the same of a bare call; the reply's text ends at the first call it keeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The unary read of an ATOM reply ended its text at the first call, so
`Before <function=get_weather>...</function> After` answered `Before ` and
dropped ` After`, which the streamed reply and ATOM's HTTP chat keep. As ATOM reads
a reply, its text is now all of it outside the calls kept (a wrapped call's from
its `<tool_call>`); SGLang's and vLLM's replies read as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed output and render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 17:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +494 to +496
// A bare one is a call only when it names a tool and call syntax
// follows; else it is prose, and so is its `<function=...>`.
if self.bare_call {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main's Qwen parser reads this input the same way: streamed, it announces get_weather, drops the prose, and get_unstreamed_tool_args sends {}; unary returns the text. This PR's recovery does not reach it (end_of_input_closers is None when prose follows the name). Checking a wrapped call's name before announcing it is a separate change to the shared parser.

/// parameter may be cut off in its name, which is then no parameter.
static WRAPPED_OR_BARE_CALLS: LazyLock<Regex> = LazyLock::new(|| {
let pattern = concat!(
r"(?s)<tool_call>\s*(.*?)\s*</tool_call>",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main's Qwen parser returns the same unary content, Use , for this input: its extractor also matches from the quoted <tool_call>. Anchoring the wrapped match on a function header is a separate change to the shared parser; this PR only changes the streamed read, which keeps the sentence.

… output and render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 17:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +661 to +663
if !self.bare_call {
closers.push_str(self.tool_call_end_token);
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f544044: end_of_input_closers no longer appends </tool_call> when text follows the call's </function>, so the unary reply keeps After, with the call read at </function>. Streamed, the reply reads as main's parser reads it; ending a wrapped call at </function> while streaming is a separate change to the shared parser. Test: text_after_a_cut_off_wrapper_s_function_is_not_closed_into_the_call.

Jasen2201 and others added 2 commits November 24, 2026 18:09
The streamed Qwen XML parser read parameters from its whole buffer, so
two calls arriving in one delta both took the later call's values, and a
call with no parameters closed with an empty argument string instead of
`{}`. It now reads parameters only up to the open call's end tag and
streams `{}` for a call that closes without any, unary as streamed, for
bare and wrapped calls alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/function>

For `Before <tool_call><function=f>...</function> After`, appending
`</tool_call>` at end of input made both reads drop ` After` into the
call. `end_of_input_closers` now returns no closers when text follows the
call's `</function>`, so the unary reply keeps that text (the call still
read at `</function>`, as ATOM's HTTP parser ends it) and the streamed one
reads as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 18:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…al into structured output and render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 21:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jasen2201 and others added 3 commits November 24, 2026 21:36
…eads it

Streaming read `Example <function=NAME ... <function=get_weather></function>` as one
header named `NAME ... <function=get_weather`, so the call was lost, while the
unary pattern (`<function=[^<>]*>`) skips the quoted header and finds it. A bare
header's name now ends at a `<` as on unary: the text up to it is prose, and the
parser reads on from the nested `<function=`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`process_chunk_tokens` collected each decoded piece in a Vec only to concatenate
them, and `run_generate_stream` passes it a Complete's whole unstreamed suffix. It
now appends each piece to one String; only the chat's reasoning path keeps the
pieces. Both share the decoding loop (`decode_chunk`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… tree

An ATOM worker's timed reply was turned into a serde_json Value, its text and every
output id copied, only to add three timing keys to each meta_info. A Serialize
wrapper now flattens the timing (`MetaInfoTiming`) after the meta_info's fields,
writing the same bytes; the completions adapter reads it back through the same type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 21:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…sh structured output and chat render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +208 to +210
workers
.iter()
.any(|worker| worker.metadata().runtime_type == RuntimeType::Atom)
let result = pieces
.iter()
.try_fold(ParserResult::default(), |mut read, piece| {
let piece = parser.parse_reasoning_streaming_incremental(piece)?;
Main keeps a chat's body as sent for the routers whose render reads fields
the typed request drops (RouterTrait::sent_chat_body, route_chat_as_sent).
The ATOM gRPC routers now take the message tools and `thinking` from that
body instead of a second raw-body route: route_chat_body and the raw part
of into_native_parts go, sent_chat_body also keeps the body for a model
with an ATOM worker, and prepare_chat_with reads MessageTools and
read_thinking from it (prepare_chat_declaring and execute_chat_declaring
fold into prepare_chat_with and execute_chat_with). A model with
ChatControls renders with its encoder and names its own opener; the
template path keeps per-message tools through render_chat_messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +703 to +705
if self.in_tool_call && self.current_tool_name_sent {
return String::new();
}
Comment on lines +212 to +213
let (pieces, _stopped) = process_chunk_pieces(&mut stop_decoder, &token_ids);
let chunk_text = pieces.concat();

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants