Add chat template kwargs to tokenizer API - #2421
Conversation
Preserve existing C and C++ callers while exposing typed template context values through the ORT Extensions stacked dependency. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9f5bae1-1b88-4311-8fac-6224769e0400
Build the native API against the ORT Extensions stack and run the focused chat-template C API test on a hosted Linux runner. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9f5bae1-1b88-4311-8fac-6224769e0400
Use the tested feature branch commit for the temporary stacked build dependency. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9f5bae1-1b88-4311-8fac-6224769e0400
The native stack compiled and CAPITests.ChatTemplate passed in fork run 32071927312. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9f5bae1-1b88-4311-8fac-6224769e0400
Keep the upstream dependency graph unchanged; the stacked ORT Extensions commit is used only by disposable validation, never production source. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9f5bae1-1b88-4311-8fac-6224769e0400
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR extends ONNX Runtime GenAI’s chat templating pipeline to accept additional template context (“kwargs”) as a JSON object, exposing the capability through the C API and the zero-overhead C++ wrapper, and validating behavior via C API tests.
Changes:
- Add
OgaTokenizerApplyChatTemplateWithOptionsto the C API to passtemplate_kwargs(JSON) into chat template rendering. - Update the C++ wrapper (
OgaTokenizer) and internalTokenizerimplementation to route templating through the new “with options” path (with legacy behavior preserved by passingnullptrkwargs). - Add/extend C API tests to validate kwargs rendering and parity with the legacy API when kwargs are
nullptr.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/c_api_tests.cpp | Adds coverage for kwargs-based template rendering and legacy/null-kwargs equivalence. |
| src/ort_genai.h | Adds C++ wrapper ApplyChatTemplateWithOptions and forwards legacy ApplyChatTemplate to it. |
| src/ort_genai_c.h | Introduces the new exported C API function and documentation for template_kwargs. |
| src/ort_genai_c.cpp | Implements the new C API entrypoint and forwards the legacy entrypoint to it. |
| src/models/model.h | Adds internal Tokenizer::ApplyChatTemplateWithOptions declaration. |
| src/models/model.cpp | Implements internal kwargs-aware chat templating via OrtxApplyChatTemplateWithOptions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Match the clang-format line wrapping required by the upstream lint workflow. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9f5bae1-1b88-4311-8fac-6224769e0400
microsoft/onnxruntime-extensions#1102 merged as bd0e21c11187e0b8a2385d1c61122a4d259a53a0 (Add chat template kwargs to C API). Point FetchContent GIT_TAG at that commit so OgaTokenizerApplyChatTemplateWithOptions can resolve OrtxApplyChatTemplateWithOptions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 023653cc-8b13-428d-84dc-92c3fa96cabb
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Ready for review — Extensions leaf landedmicrosoft/onnxruntime-extensions#1102 merged as This PR now:
Stack
Evidence
Follow-up (non-blocking, Extensions)Sayan noted a future direction: fold chat-template kwargs into cc anyone owning GenAI releases / packaging for Foundry Local. |
|
Looks like the red Windows CUDA x64 check is an infra flake, not a code failure:
It died mid Please Re-run failed jobs when you have a moment — no source change needed from our side. |
Address review feedback: the no-kwargs path does not need to route through WithOptions. Restore Tokenizer::ApplyChatTemplate and the public C/C++ wrappers to call OrtxApplyChatTemplate / ApplyChatTemplate directly. Keep ApplyChatTemplateWithOptions as the only path that uses the new Extensions API. ApplyChatTemplate remains in use (C API, C++ header, Python, tests, examples). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 023653cc-8b13-428d-84dc-92c3fa96cabb
|
/azp run macOS arm64 Build |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
283b807
into
microsoft:main
Summary
ApplyChatTemplatebehavior by delegating with null kwargsenable_thinkingandreasoning_effortto ONNX Runtime ExtensionsDependency
This PR depends on microsoft/onnxruntime-extensions#1102. The production dependency pin is intentionally unchanged; this PR should remain draft until #1102 merges and ORT GenAI updates its normal Extensions pin.
Motivation
Foundry Local issue microsoft/foundry-local#808 needs a request path for:
{"chat_template_kwargs":{"enable_thinking":false}}Qwen 3 defaults to reasoning and can exhaust bounded output budgets before producing an answer or tool call. Qwen 2.5 has no equivalent reasoning-template branch.
Validation
The code was stacked against the tested #1102 head only inside a disposable fork workflow. The native build compiled and
CAPITests.ChatTemplatepassed:https://github.com/jennyf19/onnxruntime-genai/actions/runs/32071927312
No personal-fork dependency appears in this PR's production diff.