Skip to content

Add chat template kwargs to tokenizer API - #2421

Merged
Baiju Meswani (baijumeswani) merged 9 commits into
microsoft:mainfrom
jennyf19:feature/chat-template-kwargs
Aug 22, 2026
Merged

Add chat template kwargs to tokenizer API#2421
Baiju Meswani (baijumeswani) merged 9 commits into
microsoft:mainfrom
jennyf19:feature/chat-template-kwargs

Conversation

@jennyf19

@jennyf19 jennyf19 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add backward-compatible C and C++ tokenizer APIs for typed chat-template kwargs
  • preserve the existing ApplyChatTemplate behavior by delegating with null kwargs
  • pass model-specific context such as enable_thinking and reasoning_effort to ONNX Runtime Extensions
  • add focused C API coverage for typed values and legacy equivalence

Dependency

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.ChatTemplate passed:
https://github.com/jennyf19/onnxruntime-genai/actions/runs/32071927312

No personal-fork dependency appears in this PR's production diff.

Jenny Ferries added 5 commits August 17, 2026 14:32
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
@jennyf19
jennyf19 requested a review from a team as a code owner August 17, 2026 21:46
Copilot AI lite review requested due to automatic review settings August 17, 2026 21:46
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 OgaTokenizerApplyChatTemplateWithOptions to the C API to pass template_kwargs (JSON) into chat template rendering.
  • Update the C++ wrapper (OgaTokenizer) and internal Tokenizer implementation to route templating through the new “with options” path (with legacy behavior preserved by passing nullptr kwargs).
  • 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.

@jennyf19 jennyf19 changed the title Feature/chat template kwargs Add chat template kwargs to tokenizer API Aug 17, 2026
@jennyf19
jennyf19 marked this pull request as draft August 17, 2026 22:52
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
Jenny Ferries and others added 2 commits August 21, 2026 11:38
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
@jennyf19
jennyf19 marked this pull request as ready for review August 21, 2026 18:41
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@jennyf19

Copy link
Copy Markdown
Contributor Author

Ready for review — Extensions leaf landed

microsoft/onnxruntime-extensions#1102 merged as bd0e21c11187e0b8a2385d1c61122a4d259a53a0 (merged by Sayan Shaw (@sayanshaw24)).

This PR now:

  • Keeps the backward-compatible OgaTokenizerApplyChatTemplateWithOptions / C++ parallel API
  • Pins cmake/deps.txt onnxruntime_extensions GIT_TAG to that merge commit (no personal-fork pin)
  • Includes latest main merge

Stack

  1. ✅ Extensions pip install onnxruntime-genai does not work #1102 merged
  2. 👉 This PR (GenAI) — please review / undraft path
  3. Foundry Local update version #1009 stays draft until a GenAI NuGet (Microsoft.ML.OnnxRuntimeGenAI.Foundry) includes this API (Baiju release train)

Evidence

Follow-up (non-blocking, Extensions)

Sayan noted a future direction: fold chat-template kwargs into OrtxUpdateTokenizerOptions and eventually remove the per-call WithOptions surface. Dependents should stay thin adapters.

cc anyone owning GenAI releases / packaging for Foundry Local.

@jennyf19

Copy link
Copy Markdown
Contributor Author

Looks like the red Windows CUDA x64 check is an infra flake, not a code failure:

The self-hosted runner lost communication with the server…

It died mid Build with build.py with no compile error. The twin windows-cuda-x64-build job on this same SHA is green, as are linux-cuda-x64, Windows CPU, lint, etc.

Please Re-run failed jobs when you have a moment — no source change needed from our side.

Comment thread src/models/model.cpp Outdated
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
@baijumeswani

Copy link
Copy Markdown
Collaborator

/azp run macOS arm64 Build

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@baijumeswani
Baiju Meswani (baijumeswani) merged commit 283b807 into microsoft:main Aug 22, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants