Skip to content

[Fix] Preserve inline instructions for Responses and Kimi K3 Messages - #40077

Merged
hnyls2002 merged 7 commits into
sgl-project:mainfrom
chromecast56:codex/responses-inline-system-upstream
Sep 30, 2026
Merged

hnyls2002 merged 7 commits into
sgl-project:mainfrom
chromecast56:codex/responses-inline-system-upstream

Conversation

@chromecast56

@chromecast56 chromecast56 commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

Related entrypoints PRs: #39711, #39716. Each is independent and they can merge in any order.

Motivation

Responses always moves an inline developer/system input into the leading system message. Appending an instruction to a conversation therefore rewrites the start of the prompt and throws away the whole cached prefix, which is common for agentic clients.

  • Controlled test on a production Kimi K3 deployment: a ~50K-token Responses conversation hit 50,176 cached tokens when repeated or extended with a user turn. Appending one developer instruction dropped the hit to 0. Replaying the original right afterwards hit 50,176 again, so the prefix was still cached.
  • Production: after deploying this change on a production Kimi K3 deployment, token-weighted prefix-cache hit rate for /v1/responses went from 25.5% to 90.7%, and from 12.9% to 92.2% for prompts ≥128K tokens. Chat Completions stayed flat (91.8% → 93.0%). This compares two 30-minute windows on 2026-09-18. They weren't matched, and the deploy included other changes, so treat it as supporting evidence rather than an A/B test.

Anthropic Messages already keeps inline system messages when the chat template renders them (#28906), but it probes the tokenizer's Jinja template. Kimi K3 renders with a native encoder and has no Jinja template, so K3 Messages requests are still hoisted.

Modifications

  • OpenAIServingChat.supports_inline_system is resolved once at startup from the renderer that actually builds the prompt: the native encoder allowlist (spec_supports_inline_system, currently kimi_k3) when a native encoder is active, False for a conversation --chat-template, and otherwise the existing Jinja probe (detect_inline_system_support).
  • Anthropic Messages and Responses both read that attribute. Responses skips the system-message merge when it is set.
  • Behavior change: Responses now keeps inline instructions in place for any HF Jinja template that renders mid-conversation system messages, the same rule fix(anthropic): detect-and-passthrough mid-conversation system messages #28906 applied to Messages. Messages now merges under a conversation --chat-template or a native encoder outside the allowlist, since the probed Jinja template is not what renders those prompts.
  • [dsv4.1] Preserve inline system messages with native chat encoders #39518 made the same fix for DeepSeek V4.1 Messages but targeted the dsv4.1 branch and was closed unmerged when that branch was deleted. Adding dsv41 to the allowlist is a one-line follow-up once its encoder is checked.

Tests

  • test_serving_responses.py::test_inline_instruction_keeps_history_prefix (4 cases): the K3 and inline-Jinja cases fail on main because the instruction is hoisted. The conversation-template and dropping-Jinja cases pin the merge fallback; the conversation-template case fails if the resolver ignores --chat-template.
  • anthropic/test_serving.py::test_inline_system_support_comes_from_the_prompt_renderer fails on main, where Messages probes a missing Jinja template and hoists.
  • test_serving_responses.py, test_serving_responses_stream.py, test_serving_chat.py, and anthropic/test_serving.py pass.

CI States

Latest PR Test (Base): ⏳ Run #36784288085
Latest PR Test (Extra): ❌ Run #36784287853
Latest PR Test (AMD ROCm 10): ⏳ Run #36784288006

@cyber-meow

Copy link
Copy Markdown

We independently reproduced the instruction-ordering issue described here.

In a CPU tokenization check with a synthetic 721k-token GLM conversation, appending a system reminder left only 10.45% of the original token prefix unchanged. Keeping the reminder in its original position preserved 100% of that prefix. Our fix still combines consecutive instructions at the start of the conversation, but leaves later instructions in place.

We then tested native Responses requests on GLM 5.3, Kimi K3 and DeepSeek V4.1. Each continuation included the previous assistant output, a tool result and a new system reminder. We kept request settings fixed and used the same worker so its KV cache remained available. Two continuations per model produced these results:

Model Initial input tokens Cached share of continuation input
GLM 5.3 724,190 99.9898–99.9956%
Kimi K3 775,142 99.9758–99.9896%
DeepSeek V4.1 740,955 99.9694–99.9785%

These measurements are from our backport on deployment-specific checkpoints; we have not tested this PR's latest commit. The DeepSeek tests used a tool schema with strict: true to enforce the expected tool arguments.

One additional regression test could cover top-level instructions, multiple initial system/developer messages, and a later reminder. Our fix still merges the initial instructions; this PR returns before that merge when inline instructions are supported. Testing this case would make the intended formatting of the initial prompt explicit.

This investigation and comment were prepared with Codex. All measurements above come from executed CPU/GPU tests.

@chromecast56
chromecast56 force-pushed the codex/responses-inline-system-upstream branch from f6fdfed to 4fdd158 Compare September 23, 2026 01:59
chromecast56 and others added 3 commits September 23, 2026 21:41
Resolve once on OpenAIServingChat whether the prompt renderer keeps a
mid-conversation system message in place (native encoder allowlist,
then conversation template, then the Jinja probe), and read it from both
Anthropic Messages and Responses. Drops the defensive getattrs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chromecast56
chromecast56 force-pushed the codex/responses-inline-system-upstream branch from 4fdd158 to 2722e7f Compare September 23, 2026 21:57
…e-system-upstream

# Conflicts:
#	python/sglang/srt/entrypoints/openai/serving_chat.py
@hnyls2002 hnyls2002 added the run-ci CI: run the baseline test suite on this PR label Sep 30, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/unit/entrypoints/openai/test_serving_responses.py test/registered/unit/entrypoints/anthropic/test_serving.py test/registered/unit/entrypoints/openai/test_serving_chat.py test/registered/unit/entrypoints/openai/test_responses_custom_tools.py

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/entrypoints/openai/test_serving_responses.py test/registered/unit/entrypoints/anthropic/test_serving.py test/registered/unit/entrypoints/openai/test_serving_chat.py test/registered/unit/entrypoints/openai/test_responses_custom_tools.py:

🚀 ubuntu-latest (4 tests): ✅ View workflow run

cd test/ && python3 registered/unit/entrypoints/openai/test_serving_responses.py
cd test/ && python3 registered/unit/entrypoints/anthropic/test_serving.py
cd test/ && python3 registered/unit/entrypoints/openai/test_serving_chat.py
cd test/ && python3 registered/unit/entrypoints/openai/test_responses_custom_tools.py

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

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants