Skip to content

Add to_jinja() to PromptFormatter for programmatic chat template generation#15616

Closed
DongjiGao wants to merge 1 commit into
NVIDIA-NeMo:speechlm2-with-nemo-automodel-mergefrom
DongjiGao:pr/prompt-formatters
Closed

Add to_jinja() to PromptFormatter for programmatic chat template generation#15616
DongjiGao wants to merge 1 commit into
NVIDIA-NeMo:speechlm2-with-nemo-automodel-mergefrom
DongjiGao:pr/prompt-formatters

Conversation

@DongjiGao
Copy link
Copy Markdown
Contributor

Summary

  • Add to_jinja() classmethod to PromptFormatter base class and two subclasses for programmatic Jinja2 chat template generation
  • Ensures consistency between NeMo training prompts and vLLM inference prompts — no manual template maintenance
  • Base PromptFormatter.to_jinja() raises NotImplementedError to enforce subclass implementation

Changes

nemo/collections/common/prompts/formatter.py

  • Add to_jinja(cls, audio_token, **kwargs) -> str classmethod (raises NotImplementedError)

nemo/collections/common/prompts/qwen.py

  • QwenPromptFormatter.to_jinja(): ChatML format (<|im_start|>/<|im_end|>), multimodal audio support, no system role

nemo/collections/common/prompts/nemotron_nano_v3.py

  • NemotronNanoV3PromptFormatter.to_jinja(): auto-inserts empty system turn if missing, enable_thinking toggle for <think> tags, multimodal support

tests/collections/speechlm2/test_chat_template.py

  • 14 pytest tests covering: text-only, multimodal, explicit system messages, no duplicate system turns, thinking toggle, audio token customization, base class error

Test plan

  • pytest tests/collections/speechlm2/test_chat_template.py — 14 tests pass
  • CI: add "Run CICD" label

Made with Cursor

…ration

Add classmethod to_jinja() that generates Jinja2 chat templates matching
the training prompt format. Ensures consistency between NeMo training and
vLLM inference prompts.

- Base PromptFormatter.to_jinja() raises NotImplementedError
- QwenPromptFormatter.to_jinja(): ChatML format, multimodal support
- NemotronNanoV3PromptFormatter.to_jinja(): auto empty system turn,
  enable_thinking toggle, multimodal support
- 14 unit tests validating template output

Signed-off-by: Dongji Gao <dongjig@nvidia.com>
@pzelasko pzelasko deleted the branch NVIDIA-NeMo:speechlm2-with-nemo-automodel-merge April 17, 2026 14:08
@pzelasko pzelasko closed this Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants