[Rust Frontend] Fix macro-based content format detection - #49042
Conversation
Signed-off-by: reidliu41 <reid201711@gmail.com>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9a21df1386
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| && message_varnames | ||
| .iter() | ||
| .any(|varname| is_var_or_elems_access(&loop_ast.iter, varname, Some("content"))) | ||
| && (is_var_access(&loop_ast.iter, "content") |
There was a problem hiding this comment.
Restrict bare content loops to message-fed macro parameters
This condition treats every loop over a variable named content anywhere in the template as evidence that message content uses OpenAI parts, without checking that the variable is a macro parameter receiving message.content (or is related to messages at all). For example, an unused helper macro or {% set content = documents %}{% for item in content %} now switches auto detection to OpenAi; to_template_content then passes every message body as an array rather than the string the template expects, producing incorrect prompts or render failures. Track the macro call/argument relationship instead of matching the variable name globally.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I took another look at the Python implementation and confirmed that it does not perform additional detection or trace macro call/argument relationships.
While such tracking could make the detection more precise, it would require broader AST analysis
and additional coverage for argument mapping, scoping, and related edge cases to avoid introducing new false negatives.
Given the added complexity and the current Python behavior, I think keeping the Rust implementation aligned with Python may be the better trade-off for now.
BugenZhao
left a comment
There was a problem hiding this comment.
Thanks for the fix.
I think in a longer term, we probably want to switch a more robust and less heuristic approach for detecting the format. For example, instead of doing static analysis on the template content, we can probe the desired behavior by seeding different inputs, like https://github.com/ai-dynamo/dynamo/blob/fa4916d7e8a5b192f7d6db71b934bec0330812be/lib/llm/src/preprocessor/prompt/template/formatters.rs#L15-L38
…t#49042) Signed-off-by: reidliu41 <reid201711@gmail.com>
…t#49042) Signed-off-by: reidliu41 <reid201711@gmail.com> Signed-off-by: aarushjain29 <Aarushi.Jain2@amd.com>
…t#49042) Signed-off-by: reidliu41 <reid201711@gmail.com> Signed-off-by: Tejas-Raj01 <rajtejas.xyz@gmail.com>
…t#49042) Signed-off-by: reidliu41 <reid201711@gmail.com>
Purpose
The Rust chat template detector only recognized loops directly over
message.content. It missed templates that pass the content into a macro anditerate with
{% for item in content %}.Qwen3.5 uses the macro form, causing
automode to incorrectly select thestring content format. Multimodal messages consequently lost the
<|vision_start|>and<|vision_end|>wrappers around<|image_pad|>, whichcould produce incorrect prompt tokens and multimodal positions.
Recognize loops over a
contentparameter while preserving the existingdirect
message.contentdetection. Add regression coverage using both aminimal macro template and the repository's Qwen3.5 template.
The corrected auto-detection matches the explicit OpenAI content format and
the corresponding Python detector behavior.
Test Plan
Test Result
Essential Elements of an Effective PR Description Checklist
supported_models.mdandexamplesfor a new model.