[Bug] `chat_template.py` system_prompt extraction returns empty list when both tokenized sequences have equal length (`[:-0]` bug)
System Info
Discovered via static code analysis of verl/utils/chat_template.py. Affects multi-turn SFT and agent loop workflows.
Information
- [x] The official example scripts
Tasks
- [x] My own task or dataset (give details below)
Reproduction
In verl/utils/chat_template.py lines 38 and 57:
system_prompt = token1[: -(len(token2) - len(token1))]
When len(token2) == len(token1) (which happens with certain chat templates that produce equal-length encodings), this becomes:
system_prompt = token1[:-0] # Python: token1[:0] == []
x[:-0] in Python does not mean "keep all elements" — it is equivalent to x[:0], which returns an empty list. The system prompt is silently lost.
Expected behavior
diff = len(token2) - len(token1)
system_prompt = token1[:-diff] if diff > 0 else []
# or, if diff==0 means no system prompt was injected:
system_prompt = token1[:-diff] if diff > 0 else token1
Impact
- Silent bug in multi-turn SFT and agent-loop tokenization.
- System prompt tokens are silently dropped, causing the model to be trained/prompted without the intended system context.
- Affects
initialize_system_prompt(line 38) andextract_system_prompt_and_generation(line 57).
https://github.com/verl-project/verl/blob/main/verl/utils/chat_template.py#L42-L55
token1 = [{"role": "user", "content": ""}]
token2 = [{"role": "user", "content": ""}, {"role": "user", "content": ""}]
Since token1 and token2 use different messages, why they produce same length token?
A small follow-up: #6500 and #6501 (filed today by @gxlvera) give a self-contained repro on microsoft/Phi-3-mini-4k-instruct showing exactly the case you flagged — different messages producing equal-length probes. The Phi-3 chat template appends a single conversation-level <|endoftext|> after the message loop, so:
[user]: [<|user|>, ..., <|endoftext|>]
[user, user]: [<|user|>, ..., <|user|>, ..., <|endoftext|>]
Both end with the same trailing <|endoftext|>, the diff is len(user_block) * 1, and the [:-diff] slice strips the leading <|user|> of the first probe — which is what triggers the empty/incorrect prefix on this template family. Posting this only as cross-reference in case it's useful for closing this thread one way or the other.