Skip to content

Support default chat template kwargs - #1363

Closed
sufubao wants to merge 2 commits into
ModelTC:mainfrom
sufubao:preserve_thinking
Closed

sufubao wants to merge 2 commits into
ModelTC:mainfrom
sufubao:preserve_thinking

Conversation

@sufubao

@sufubao sufubao commented Jun 18, 2026 •

Copy link
Copy Markdown
Collaborator

新增 --default_chat_template_kwargs(别名 --default-chat-template-kwargs),支持为所有聊天请求设置默认模板参数,例如 --default-chat-template-kwargs '{"preserve_thinking": true}'。CLI 仅接受 JSON 对象,请求中的 chat_template_kwargs 按键覆盖服务默认值。

默认参数会同步 thinking / enable_thinking 别名,并由提示词构建和 reasoning parser 共用,避免思考模式不一致。reasoning_effort 的优先级为:请求 chat_template_kwargs > 请求顶层字段 > 服务默认值。

已合入最新 main 并解决冲突,保留当前启动参数默认值。验证:

  • python -m pytest unit_tests/server/test_default_chat_template_kwargs.py unit_tests/server/test_reasoning_token_usage.py unit_tests/server/test_api_responses.py -q:56 passed;覆盖 JSON 校验、参数覆盖、输入不变性、思考别名及 reasoning_effort 优先级。
  • 5 个变更文件的 pre-commit(Black、flake8)全部通过,git diff --check 通过。
  • 使用 CPU 单元测试和 tokenizer mock 验证参数传递,未运行 GPU 模型端到端测试。

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a new CLI argument --default_chat_template_kwargs to specify default JSON keyword arguments for the chat template tokenizer, which are merged with request-specific arguments when building prompts and determining reasoning modes. Unit tests are also added to verify this functionality. The reviewer suggests making the get_effective_chat_template_kwargs helper function more robust by defensively checking types and using getattr on the request object to prevent potential runtime errors.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +130 to +137
def get_effective_chat_template_kwargs(request) -> dict:
kwargs = {}
default_kwargs = getattr(get_env_start_args(), "default_chat_template_kwargs", None)
if default_kwargs:
kwargs.update(default_kwargs)
if request.chat_template_kwargs:
kwargs.update(request.chat_template_kwargs)
return kwargs

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.

medium

To prevent potential runtime errors (such as AttributeError or TypeError), it is safer to defensively check that default_chat_template_kwargs and request.chat_template_kwargs are indeed dictionaries before calling .update(). Additionally, using getattr on request ensures compatibility if a request object without the chat_template_kwargs attribute is passed (e.g., in certain testing scenarios or alternative request flows).

Suggested change
def get_effective_chat_template_kwargs(request) -> dict:
kwargs = {}
default_kwargs = getattr(get_env_start_args(), "default_chat_template_kwargs", None)
if default_kwargs:
kwargs.update(default_kwargs)
if request.chat_template_kwargs:
kwargs.update(request.chat_template_kwargs)
return kwargs
def get_effective_chat_template_kwargs(request) -> dict:
kwargs = {}
default_kwargs = getattr(get_env_start_args(), "default_chat_template_kwargs", None)
if isinstance(default_kwargs, dict):
kwargs.update(default_kwargs)
req_kwargs = getattr(request, "chat_template_kwargs", None)
if isinstance(req_kwargs, dict):
kwargs.update(req_kwargs)
return kwargs

@sufubao
sufubao force-pushed the preserve_thinking branch from 8963ad5 to cce09d1 Compare June 18, 2026 03:32
@sufubao sufubao closed this Sep 10, 2026
@sufubao sufubao reopened this Sep 11, 2026
@sufubao sufubao closed this Sep 11, 2026
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.

1 participant