Skip to content

[Fix] openai: Reject developer messages silently omitted by chat templates - #40103

Open
yuchenwang3 wants to merge 2 commits into
sgl-project:mainfrom
yuchenwang3:fix/developer-role-40082
Open

yuchenwang3 wants to merge 2 commits into
sgl-project:mainfrom
yuchenwang3:fix/developer-role-40082

Conversation

@yuchenwang3

@yuchenwang3 yuchenwang3 commented Sep 18, 2026 •

Copy link
Copy Markdown

Motivation

Fixes #40082. A chat template can silently omit developer messages while /v1/chat/completions accepts the request. With the Qwen3-0.6B tokenizer, the resulting prompt contains the user question but loses the developer instruction.

Return a 400 response before generation when the selected Jinja template does not render a developer message. Preserve the role, contents, and ordering for templates that support it; do not implicitly promote developer instructions to system instructions.

Modifications

  • Before encoding an uncached Jinja prompt containing developer messages, render a separate probe with a distinct sentinel at each developer message's actual conversation position. Reject a missing sentinel with the offending message index.
  • Use the same tools, template options, and string/content-parts representation as the request. The probe is never tokenized or sent to generation, and does not mutate request data.
  • Requests without developer messages keep one render. Supported developer requests add one probe render on a cache miss; existing cache hits avoid both renders. Native custom encoders are unchanged.
  • Cover silently ignored roles, content parts, multiple developer messages, original-prompt preservation, cache reuse, and the serving handler's 400 response before generation.

I checked existing work: #40077 addresses inline instruction ordering in Responses/Kimi Messages, not unsupported developer roles in this generic Chat Completions path. No direct fix for #40082 was found at submission.

Latest validation (November 19)

Merged current main 17a9168f1f99 into 9de27b0f8f5d, retaining both developer-role regressions and upstream's output-limit validation tests. The chat-serving suite passed 217 tests and the prompt-round-trip suite passed 9 tests with normal imports in the restored macOS Python 3.12 environment, using the suite's existing maybe_stub_sgl_kernel helper. All applicable changed-file pre-commit hooks passed. No GPU or live server run was performed.

Previous validation (October 26)

Merged main e732f5d41e9d279c1be7e5818ce9fd8bd425c0b2 into head 732aaba18a2a3525da4a38aebba9d0087984d287. The upstream GLM47 tokenizer fixture now supports the non-tokenized developer-message probe used by this change.

The chat-serving and prompt-round-trip CPU suites passed all 188 tests with normal imports and an isolated tml-renderers==0.1.0 dependency overlay. All applicable changed-file pre-commit hooks passed. No GPU or full-server run was performed. These are the saved October 26 results; they were not rerun on November 5.

The current CI gate reports Missing required label 'run-ci'; model/server tests have not run on this head.

Latest validation (September 21)

Rebased onto main (00986c81b) at 03e9a1bfd2953741c00ffa3a83bfe7d3b8aa6e95. Resolved the overlap with the new direct-token encoding path, preserving its control tokens. Added coverage for supported and dropped developer messages on that path, including cache reuse.

The chat-serving and prompt-round-trip CPU suites pass: 155 tests. All applicable changed-file pre-commit hooks pass. No GPU/full-server run was performed for this revision.

Earlier validation below describes the original revision unless stated otherwise.

Accuracy Tests

CPU validation on macOS, Python 3.12, Torch 2.13.0, Transformers 5.12.1:

PYTHONPATH=python .venv/bin/python -m unittest \
  test.registered.unit.entrypoints.openai.test_serving_chat -q
# Ran 144 tests: OK

The suite uses its existing mocked tokenizer manager/generation and maybe_stub_sgl_kernel CPU helper. The added template tests use Transformers' real Jinja renderer. A baseline control with the new tests and unmodified serving_chat.py fails the three new rejection checks; the supported-role/cache control passes.

Separately loaded the actual Qwen/Qwen3-0.6B tokenizer at c1899de289a04d12100db370d81485cdf75e47ca: the original template omits the developer instruction; the patched serving handler returns 400 before calling generation; a system-role control retains the instruction. This checks the tokenizer and handler, not a live GPU server or model accuracy benchmark. No model weights were loaded.

Speed Tests and Profiling

No end-to-end throughput benchmark. There is one additional CPU template render for an uncached request containing developer messages; there is no additional model invocation. Requests without developer messages and existing template-cache hits retain their render counts.

Checklist

  • Changed-file pre-commit hooks pass, including formatting and CI registration checks.
  • Added regression tests to the existing registered CPU suite.
  • Documentation update (no new option; the error explains the unsupported role).
  • GPU accuracy/throughput benchmarks (not run; validation boundary above).
  • Followed existing code style.

AI assistance: developed and tested with OpenAI Codex.


CI States

Latest PR Test (Base): ⊘ Skipped -- add the run-ci label to run CI tests.
Latest PR Test (Extra): ⊘ Skipped -- add the run-ci label to run CI tests.
Latest PR Test (AMD ROCm 10): ⊘ Skipped -- add the run-ci label to run CI tests.

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@yuchenwang3

Copy link
Copy Markdown
Author

Rebased onto main (00986c81b) at 03e9a1bfd2953741c00ffa3a83bfe7d3b8aa6e95. Resolved the overlap with the new direct-token encoding path, preserving its control tokens. Added coverage for supported and dropped developer messages on that path, including cache reuse.

The chat-serving and prompt-round-trip CPU suites pass: 155 tests. All applicable changed-file pre-commit hooks pass. No GPU/full-server run was performed for this revision.

@yuchenwang3

yuchenwang3 commented Oct 3, 2026 •

Copy link
Copy Markdown
Author

@JustinTong0323 Could you review the developer-message handling here? It returns 400 before generation when the selected template silently omits those instructions. Rebased onto 10edb05e72 at b49304c3c9; the chat-serving and prompt-round-trip CPU suites pass 165 tests, and changed-file pre-commit checks pass. The minimum-base requirement is satisfied; could you authorize CI?

@yuchenwang3
yuchenwang3 force-pushed the fix/developer-role-40082 branch 2 times, most recently from b49304c to 55589f4 Compare October 8, 2026 22:54

Copy link
Copy Markdown
Author

@JustinTong0323 Rebased on main and fixed the conflict. All 170 chat-serving and prompt-round-trip CPU tests pass, and the branch includes the updated CI minimum base. Could you review the developer-message check and enable CI?

Assisted-by: OpenAI Codex
Signed-off-by: Yuchen Wang <[email protected]>
@yuchenwang3 yuchenwang3 changed the title [Fix] Reject developer messages silently omitted by chat templates [Fix] openai: Reject developer messages silently omitted by chat templates Oct 26, 2026
@yuchenwang3
yuchenwang3 force-pushed the fix/developer-role-40082 branch from 55589f4 to 732aaba Compare October 26, 2026 23:58

Copy link
Copy Markdown
Author

Head 732aaba18 includes main e732f5d41. The chat-serving and prompt-round-trip CPU suites passed 188 tests on October 26, and changed-file pre-commit checks passed. The GLM47 tokenizer fixture now supports the developer-message probe. Current CI stops at the missing run-ci label before model/server tests; a maintainer needs to authorize CI. No new GPU/server result is claimed.

Assisted-by: OpenAI Codex
Signed-off-by: Yuchen Wang <[email protected]>

Copy link
Copy Markdown
Author

Merged current main in 9de27b0 and resolved the test conflict by keeping both the developer-role and output-limit cases. The chat-serving and prompt-round-trip suites pass all 226 CPU tests; changed-file pre-commit hooks pass. Could a maintainer enable run-ci for this head? The new pr-test-finish check confirms the test jobs were skipped because that label is missing.

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.

[Bug] developer messages are silently dropped by chat templates without role support

1 participant