Skip to content

feat(vllm_model): add opt-in fold_nonleading_system_messages - #4010

Open
linj-glitch wants to merge 6 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/vllm-fold-system-messages
Open

linj-glitch wants to merge 6 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/vllm-fold-system-messages

Conversation

@linj-glitch

@linj-glitch linj-glitch commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Claude Code can insert a system notice after a tool turn, which causes Qwen-style chat templates to reject the request with System message must be at the beginning.
Enabling fold_nonleading_system_messages changes later system/developer messages to user messages while preserving their content, chronological position, and the leading system block, after applying replace_developer_role_with_system.
The option defaults to false and rejects the Responses-native path; enabling it deliberately changes those prompt roles.
Head 8ce785af39a50ca2e63cb025a8b424243b2ecd5c includes an additive DCO-signed merge of main f36e126547866601bee9c0671c9b59484ef83723 to pick up the existing mini-SWE dependency-isolation and terminal-test fixes, with the folding behavior unchanged.
Validation on Python 3.13.14 with imports verified from this checkout passed all 573 vLLM tests, pre-commit run --all-files, the private guardwords scan of this PR's diff and description, and git diff --check.
The author's historical GRPO rollout with Claude Code SWE agents and a Qwen-family policy reported that affected requests were served when enabled; this AI-assisted integration adds no new real-model rollout evidence, and normal GitHub review and current-head CI gates still apply.

@copy-pr-bot

copy-pr-bot Bot commented Oct 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:model Model servers, inference providers, and model adapters complexity:low Localized change in one scope with a small, straightforward review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer labels Oct 5, 2026
@linj-glitch
linj-glitch marked this pull request as draft October 5, 2026 06:12
@linj-glitch
linj-glitch force-pushed the linj/vllm-fold-system-messages branch from 9eae98e to a396650 Compare October 8, 2026 00:15
Some harnesses inject system messages mid-conversation; Claude Code, for
example, appends background-task notices with role=system after tool
turns. Chat templates that only admit a system message at the start
(e.g. "System message must be at the beginning") reject the whole
request, which the harness sees as a server error.

Add `fold_nonleading_system_messages` to VLLMModelConfig (default off).
When set, every system/developer message after the leading system block
is sent as a user message with identical content, so the model still
reads it in chronological position. It runs after
`replace_developer_role_with_system` and, like that option, raises
NotImplementedError on the Responses-native path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
@linj-glitch
linj-glitch force-pushed the linj/vllm-fold-system-messages branch from a396650 to d42f056 Compare October 8, 2026 00:20
@linj-glitch
linj-glitch marked this pull request as ready for review October 13, 2026 23:08
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Oct 14, 2026
@linj-glitch

Copy link
Copy Markdown
Collaborator Author

/ok to test 6c73d74

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

@linj-glitch

Copy link
Copy Markdown
Collaborator Author

/ok to test 8ce785a

Only responses_api_models/vllm_model/tests/test_app.py conflicted: both
sides added names to its import from responses_api_models.vllm_model.app.
The resolution keeps the names of both sides, so every test of both sides
stays.

Signed-off-by: alexandery <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
@alexandery-nvidia

Copy link
Copy Markdown
Contributor

I merged main into the branch (6cb5acbacf), so that the pull request can be reviewed again. The one conflict was the import list of responses_api_models/vllm_model/tests/test_app.py, where both sides added names; all three are kept. The branch differs from main by the 90 lines of this pull request.

On current main the setting still sends a later system or developer message as a user message, with its text and its place unchanged, on /v1/responses with and without stream, /v1/chat/completions and /v1/messages. The 1283 tests of responses_api_models/vllm_model pass, and ruff check and ruff format --check pass on the two Python files.

A sandbox session of Codex needs this setting for a chat template that accepts an instruction message only at the start: after Codex compacts its history in place, it can put an instruction message after other messages.

@alexandery-nvidia

Copy link
Copy Markdown
Contributor

/ok to test 6cb5acb

@nemo-pr-automation
nemo-pr-automation Bot requested a review from a team December 3, 2026 00:05
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the risk:mid Autobot PR risk: mid label Dec 3, 2026
@zyzhou5 zyzhou5 assigned zyzhou5 and unassigned zyzhou5 Dec 3, 2026
@zyzhou5
zyzhou5 self-requested a review December 3, 2026 18:31
@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Dec 3, 2026

@staticmethod
def _messages() -> list[dict[str, Any]]:
return [

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.

Nit: Codex and Claude Code requests reach the fold with a single leading system message, because the conversion merges the rest (chat_completions_converter.py#L564-L575 (

# The leading run of system/developer messages is folded into the same single system
# message: chat backends commonly admit only one system message, at position 0 (harnesses
# like the Codex CLI send instructions plus a leading developer message).
instructions = responses_create_params.pop("instructions", None)
if instructions:
leading_parts = [instructions]
while state.messages and state.messages[0]["role"] in ("system", "developer"):
leading_parts.append(message_content_to_text(state.messages.pop(0)["content"]))
state.messages.insert(
0,
NeMoGymChatCompletionSystemMessageParam(content="\n\n".join(leading_parts), role="system"),
)
)). This fixture starts with system, developer, so Qwen3.5 refuses the lists the tests expect. Suggest starting with one system message and putting the developer message after the tool result; L9282, L9296 and L9301 then expect["system", "user", "assistant", "tool", "user", "user"].

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.

I checked this in the conversion: with instructions set, it merges the leading system and developer messages into one system message (nemo_gym/models/chat_completions_converter.py:567-575), and the Messages route passes system on as instructions (nemo_gym/models/anthropic_converter.py:328-330). A request of Codex or of Claude Code therefore reaches the fold with one leading system message. Commit 28d9319 changes the fixture as you suggest: it starts with one system message and has the developer message after the tool result (responses_api_models/vllm_model/tests/test_app.py:9265-9279), and the three tests expect ["system", "user", "assistant", "tool", "user", "user"] (test_app.py:9284, 9307, 9312). The tests now cover the fold of a later developer message, and test_leading_system_and_developer_messages_are_kept (test_app.py:9291-9298) keeps one case with two leading messages, which a /v1/chat/completions request can still send because it is not converted. That case states the fold's rule and not what a chat template accepts: I did not render any list with the Qwen3.5 chat template.

Comment thread responses_api_models/vllm_model/app.py Outdated
raise NotImplementedError
if self.config.replace_developer_role_with_system:
raise NotImplementedError
if self.config.fold_nonleading_system_messages:

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.

To match the other chat-only settings, could this be refused at startup by adding it to _validate_chat_conversion_options (

for option in (
"forward_unvalidated_reasoning_effort",
"inline_tool_output_images",
"preserve_thinking_blocks",
"stream_keepalive_s",
):
) instead of raising on each request?

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.

Done in 8b63638: fold_nonleading_system_messages is in the list of _validate_chat_conversion_options (responses_api_models/vllm_model/app.py:950), so a configuration that also sets is_responses_native is refused when it loads, with the same message as the other chat-only settings. I removed the check from _responses_native, because it can no longer be reached: responses() is the only caller and calls it only under is_responses_native (app.py:1510-1511), and a server changes neither setting after its configuration has loaded. test_responses_native_rejects_fold constructs the refused configuration (responses_api_models/vllm_model/tests/test_app.py:9314-9316). One behavior changes: a server with both settings used to start and applied the fold to /v1/chat/completions requests, and it no longer starts. With use_completions_api: true the fold is skipped without a message (app.py:2119-2120); three of the other four settings in that list are refused there too (app.py:960-974), and I can add the same refusal for the fold here if you want it.

alexandery-nvidia and others added 2 commits December 3, 2026 16:50
The Responses conversion merges a request's `instructions` and its leading
system and developer messages into one system message, and the Messages
route passes `system` on as `instructions`. A request of the Codex CLI or of
Claude Code therefore reaches `fold_nonleading_system_messages` with a single
leading system message.

The fixture of the fold's tests has that form: one system message at the
start, and the developer message after the tool result, before the later
system message. The three tests that read the fixture expect the roles
system, user, assistant, tool, user, user, which hold one system message, at
the start. The later developer message makes the tests cover the fold of
that role too.

`test_leading_system_and_developer_messages_are_kept` covers a request with
two leading messages, which the fold leaves as they are: a Chat Completions
request is not converted, so it can reach the fold with them.

Signed-off-by: alexandery <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
… Responses route at startup

A configuration that sets `fold_nonleading_system_messages` together with
`is_responses_native` is refused when it loads, by
`_validate_chat_conversion_options`, like the other settings that apply to
the Chat Completions conversion. The native Responses route forwards a
request without the chat messages that the fold changes.

`_responses_native` no longer checks the setting on each request.
`responses()` is its only caller and calls it only when
`is_responses_native` is set, and a server changes neither setting after its
configuration has loaded, so with the startup check that request-time check
cannot be reached.

A server with both settings used to start. It applied the fold to requests
of `/v1/chat/completions` and failed each request of `/v1/responses` and
`/v1/messages` with `NotImplementedError`: the reply was HTTP 500 with the
body `"NotImplementedError()"`, or a `response.failed` event for a
`/v1/responses` request with `stream: true`. It no longer starts.

Signed-off-by: alexandery <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
@alexandery-nvidia

Copy link
Copy Markdown
Contributor

/ok to test 8b63638

@nemo-pr-automation
nemo-pr-automation Bot requested a review from a team December 4, 2026 05:41

This branch was previously deployed

1 inactive (outdated) deployment
public — 8ce785af Deployed Oct 24, 2026 by copy-pr-bot[bot] via release / finalize / notify #7063
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters complexity:low Localized change in one scope with a small, straightforward review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer risk:mid Autobot PR risk: mid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants