fix(templating): default tojson ensure_ascii=False to prevent token bloat - #459
Open
Komeiji-Shiki wants to merge 1 commit into
Open
fix(templating): default tojson ensure_ascii=False to prevent token bloat#459Komeiji-Shiki wants to merge 1 commit into
Komeiji-Shiki wants to merge 1 commit into
Conversation
…loat Core issue: tojson filter defaulted to ensure_ascii=True, so non-ASCII characters (e.g. Chinese tool descriptions) were escaped to \uXXXX sequences. For Qwen3.5 templates rendering tool definitions, this roughly doubled prompt tokens (5845 vs ~3300 for equivalent input) and degraded model comprehension of tool descriptions. Match transformers' template environment by defaulting to ensure_ascii=False; explicit ensure_ascii=True callers still work.
Komeiji-Shiki
force-pushed
the
fix/template-tojson-ensure-ascii
branch
from
August 31, 2026 08:51
faac60e to
7abdcda
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The chat template
tojsonfilter (_tojson_compatincommon/templating.py)defaulted to
ensure_ascii=True. When a Qwen3.5-style template renders tooldefinitions via
{{- tool | tojson }}, all non-ASCII characters (e.g. Chinesetool descriptions) in the tool schemas get escaped to
\uXXXXsequences.This has two effects for users with Chinese-language MCP/function tools:
request: the same input (one user message + 10 tools with Chinese
descriptions) was ~3300 tokens with descriptions kept as UTF-8 text, but
5845 tokens after escaping. BPE tokenization of
\uXXXXescapes is farless efficient than the original characters.
readable tool descriptions, hurting function-calling behavior.
Fix
Default
ensure_ascii=Falsein_tojson_compat, matching the transformerschat template environment. Callers that explicitly pass
ensure_ascii=Truestill work.
Verification
tests/test_template_vars.pypasses (15/15).