feat(gateway): add use_sglang mode in model gateway that translates upstream agent's /chat/completions requests to sglang server or sgl-router endpoint's /generate requests, required for RL training with sglang (e.g. slime)#715
Open
lyzustc wants to merge 2 commits into
Conversation
…stream agent's /chat/completions requests to sglang server or sgl-router endpoint's /generate requests, required for RL training with sglang like slime
This was referenced Jun 30, 2026
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.
What this adds
A new
use_sglangmode forrllm-model-gatewaythat routes every agent/v1/chat/completionsturn through SGLang's native/generateAPI(token-in
input_ids, token-out viameta_info.output_token_logprobs) insteadof the OpenAI
/v1/{chat,}/completionsendpoints.Why: RL training needs the exact prompt/completion token ids + logprobs
of every turn. SGLang's OpenAI-compatible endpoints don't expose token ids, and
sgl-routerrejects token-id prompts on/v1/completions. The only path thatyields token ids and works behind
sgl-routeris the native/generateendpoint.
use_sglangmakes the gateway:template — byte-identical to how SGLang's own
/v1/chat/completionstokenizes (flattens block content, parses tool-call args to dicts, render +
encode split to avoid double-BOS). A rollout turn therefore matches the
agent-harness + SGLang-server deployment path exactly.
input_idsto<worker>/generatewithreturn_logprob=True,capturing token ids + logprobs natively — no SGLang server patches, and
it works through
sgl-router.{content, tool_calls, reasoning_content}using SGLang's ownFunctionCallParser/ReasoningParser, then return a normal OpenAI chatresponse so the agent is unchanged. Content is surfaced losslessly (never
dropped when a tool call is present), so the agent can replay the full
message list next turn and it re-tokenizes identically.
Composes with the existing
cumulative_token_mode: turn-0 (or after aprefix break) full-renders via the chat template; subsequent turns use the
renderer bridge to prefix-extend, keeping sampled tokens verbatim for drift-free
multi-turn merging.
sglangremains an optional dependency — allsglangimports arefunction-local, so vLLM-only users are unaffected.
Code changes
sglang_helper.py(new)flatten_*,prepare_template_message,apply_chat_template), parsers (get_fc_parser,parse_completion— lazysglangimports here), and/generaterequest/response shaping (sglang_sampling_params,parse_sglang_generate,build_generate_trace,to_openai_tool_calls,generate_to_chat_response).proxy.pyuse_sglanginterception inhandle()plus the orchestration skeleton —_render_sglang_prompt,_handle_sglang_generate,_sglang_generate_streaming— which own routing / HTTP / trace-persistence / accumulator state and delegate all SGLang specifics tosglang_helper. New__init__fields:use_sglang,tokenizer,sglang_tool_call_parser,sglang_reasoning_parser, plus a one-timetokenizer.encode("")probe for special-token handling.server.py--use-sglang,--sglang-tool-call-parser,--sglang-reasoning-parser); builds the tokenizer whenuse_sglang/cumulative_token_modeis set and wires the new fields intoReverseProxy.models.pyGatewayConfiggainsuse_sglang,sglang_tool_call_parser,sglang_reasoning_parser.tests/tests/unit/test_sglang.py(client-side tokenization, block-content regression, structured tool-call parsing, native token + logprob trace capture, cumulative prefix-extension vs non-cumulative, streaming) andtests/helpers/mock_sglang.py(mock/generateserver).Usage
Launch the gateway in
use_sglangmode pointed at an SGLang server (orsgl-routerin front of several), then register the worker and point anyOpenAI-compatible agent at a gateway session — no agent code changes.
Each turn's
TraceRecordthen carries the nativeprompt_token_ids,completion_token_ids, and alignedlogprobs— ready to feed a training engine(e.g. slime GRPO).