Repository navigation
feat(prompt_templates): opt-in LITELLM_DROP_MIDTURN_SYSTEM removes mid-conversation system turns - #40969
feat(prompt_templates): opt-in LITELLM_DROP_MIDTURN_SYSTEM removes mid-conversation system turns#40969dkrisman wants to merge 1 commit into
Conversation
8f497c9 to
3608e27
Compare
|
@greptileai reworked to drop-only on top of #41493's default conversion; hosted_vllm chat path covered, live proof in the body. |
|
| kept_messages: Final = ( | ||
| list(drop_mid_conversation_system(messages)) # mutable-ok: super()._transform_messages takes a list | ||
| if drops_mid_conversation_system() | ||
| else messages | ||
| ) |
There was a problem hiding this comment.
Logged prompt differs from request
When LITELLM_DROP_MIDTURN_SYSTEM=true, this creates a filtered list for the hosted_vllm request but leaves the original messages list intact. The HTTP handler passes that original list to pre-call logging, so logs and observability callbacks can show mid-turn system reminders that were never sent to the model. This makes the recorded prompt unreliable when checking what the model received.
There was a problem hiding this comment.
Intentional: the logged input stays the client's request, same as the bridge path, which also leaves its input list untouched.
There was a problem hiding this comment.
That’s fair. If pre-call logging is intentionally defined as the client’s raw request—as it is on the bridge path—retaining messages here is consistent and does not make the log inaccurate under that contract. The filtered list is still passed to super()._transform_messages, so the model request omits the mid-turn system rows. I’m withdrawing this finding.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance
|
3608e27 to
e3e0432
Compare
e3e0432 to
5190682
Compare
|
Automatic reviews are disabled for this repository. |
5190682 to
d61f131
Compare
| """ | ||
| for message in messages: | ||
| kept_messages: Final = ( | ||
| list(drop_mid_conversation_system(messages)) |
There was a problem hiding this comment.
Low: Guardrail advisories are silently discarded
With LITELLM_DROP_MIDTURN_SYSTEM=true and Lakera configured with on_flagged="inject_system_message", an attacker can submit a prompt that Lakera flags and have it forwarded without the configured advisory. CustomGuardrail.inject_advisory_message() appends a trailing system message and reports delivery, but this filter—and the equivalent new filter in the Anthropic adapter—then deletes it.
Preserve server-injected advisories in the retained system prefix, or block when they cannot be delivered, rather than treating them as disposable conversation reminders.
PR overviewThis PR adds the opt-in LITELLM_DROP_MIDTURN_SYSTEM setting to remove mid-conversation system messages in the hosted vLLM and Anthropic adapters. One issue remains open: when this setting is enabled and Lakera is configured to inject advisories, the filters also remove server-injected guardrail messages. An attacker can therefore have a flagged prompt forwarded without the configured advisory, even though advisory delivery is reported as successful. The impact is conditional on this configuration and concerns advisory delivery rather than a demonstrated bypass of blocking enforcement. Open issues (1)
Fixed/addressed: 0 · PR risk: 5/10 Reviews |
d8be529 to
7a23771
Compare
7a23771 to
23b418e
Compare
23b418e to
37e855d
Compare
…stem turns after the leading run Strict chat templates (Qwen3 on vLLM) reject a system row that is not first. The Anthropic bridge now converts such turns to user turns with an operator note; operators who would rather not deliver the reminder at all can set LITELLM_DROP_MIDTURN_SYSTEM=true to remove them before placement, on the /v1/messages bridge and on the hosted_vllm chat completions path. Callers that pass preserve_midturn_system=True (guardrails, compaction) still see the turns. Off by default.
37e855d to
75661cd
Compare
Reworked 2026-09-26: main now converts mid-conversation system turns to user turns on the bridge by default (#41493), which is what this PR's former
demotemode did. The PR is reduced to the one thing main still lacks: an opt-in that removes those turns instead of delivering them, on the bridge and on the hosted_vllm chat completions path. Env var renamed accordingly.TLDR
Problem this solves:
/v1/chat/completionspath a mid-turn system row still 400s on strict templatesHow it solves it:
LITELLM_DROP_MIDTURN_SYSTEM=true/v1/messagesbridge and tohosted_vllmchat completionspreserve_midturn_system=True(guardrails, compaction) still see the rowsUser Flow
Before: a developer whose proxy fronts a strict-template vLLM model gets the harness reminder injected as a user turn, and an OpenAI SDK client sending the same shape gets a 400
local-qwentohosted_vllm/qwen3and the developer points Claude Code (or an OpenAI SDK) at the proxysystemreminder ending "End your reply with the word PINEAPPLE", and another user turninput_tokens: 114and textHi again PINEAPPLE: the reminder reached the model as a user turn carrying the operator noteSystem message must be at the beginning.After: the admin opts out of delivering the reminders, and both surfaces succeed without them
LITELLM_DROP_MIDTURN_SYSTEM=true, restarts the proxy, and the developer sends the same requestsinput_tokens: 71and textHi: no PINEAPPLE, the input is exactly the reminder smallerprompt_tokens: 71and textHi.Relevant issues
Related: #26879 covers the same failure class on the Responses API surface
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Shared setup. A live vLLM worker serving Qwen3.8-27B-FP8 (the strict-template backend), one proxy config, one Claude Code-shaped payload per surface. The mid-turn reminder carries a marker so the two modes are distinguishable from the response text alone. "Before" is the same tip with the flag unset, which is byte-for-byte main's code path.
{"model": "local-qwen", "max_tokens": 200, "system": "You are a terse assistant.", "messages": [ {"role": "user", "content": "Say hi."}, {"role": "system", "content": [{"type": "text", "text": "<system-reminder>End your reply with the word PINEAPPLE.</system-reminder>"}]}, {"role": "user", "content": "Say hi again."}]}The chat completions payload is the same conversation with the system prompt as the first message.
Before (flag unset)
litellm --config config.yaml --port 4010curl -sS http://127.0.0.1:4010/v1/messages -H "Authorization: Bearer sk-proof-local" -H "Content-Type: application/json" -d @payload.json, observed textHi again PINEAPPLE, usage{"input_tokens": 114, "output_tokens": 160}: 200, the reminder was delivered as a user turncurl -sS http://127.0.0.1:4010/v1/chat/completions ... -d @payload_chat.json, observed:litellm.BadRequestError: Hosted_vllmException - {"error":{"message":"System message must be at the beginning.","type":"BadRequestError","param":null,"code":400}}After (
LITELLM_DROP_MIDTURN_SYSTEM=true)LITELLM_DROP_MIDTURN_SYSTEM=true litellm --config config.yaml --port 4011/v1/messagescurl, observed textHi, usage{"input_tokens": 71, "output_tokens": 128}: 200, no PINEAPPLE, 43 input tokens smaller (the operator note plus the reminder)/v1/chat/completionscurl, observed textHi., usage{"prompt_tokens": 71, "completion_tokens": 75}: 200ANTHROPIC_BASE_URL=http://127.0.0.1:4011 claude -p "Reply with exactly: drop mode online" --model local-qwenreturnsdrop mode online; the proxy log shows 3POST /v1/messagesand 0 template errorsType
🆕 New Feature
Caveats (if any)
Final Attestation