Python: fix compaction token count inflating non-ASCII text#7124
Open
he-yufeng wants to merge 1 commit into
Open
Python: fix compaction token count inflating non-ASCII text#7124he-yufeng wants to merge 1 commit into
he-yufeng wants to merge 1 commit into
Conversation
TokenBudgetComposedStrategy estimates tokens by feeding a JSON-serialized message to the tokenizer, but _serialize_message() used ensure_ascii=True. That escapes non-ASCII text into \uXXXX sequences, so CJK and other non-Latin content is token-counted as the escape sequences rather than the characters the model actually sees, inflating the estimate (~1.6x for mixed Japanese, more for pure CJK) and skewing compaction/token-budget decisions. Serialize with ensure_ascii=False, matching the ensure_ascii=False already used elsewhere in this module. Only affects token estimation; the serialized string is never stored or transmitted.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes token-estimation inflation for non-ASCII (e.g., CJK) messages in Python compaction by ensuring message serialization used for token counting does not escape Unicode into \uXXXX sequences, bringing estimates closer to real model tokenization behavior and addressing #7022.
Changes:
- Updated
_serialize_message()to usejson.dumps(..., ensure_ascii=False)so token counting operates on actual Unicode characters. - Added a regression test that verifies serialized payload retains non-ASCII characters and yields a lower token count than an escaped representation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/packages/core/agent_framework/_compaction.py |
Switches message serialization for token estimation to ensure_ascii=False to avoid inflated counts from escaped Unicode. |
python/packages/core/tests/core/test_compaction.py |
Adds a regression test covering non-ASCII serialization and confirming token count reduction vs an escaped form. |
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.
Motivation and Context
TokenBudgetComposedStrategyestimates a message's token count by feeding its JSON serialization to the tokenizer._serialize_message()built that JSON withensure_ascii=True, which escapes non-ASCII text into\uXXXXsequences. The tokenizer then counts the escape sequences instead of the characters the model actually tokenizes — e.g.こんにちはis counted asこんにちは. For non-Latin content this inflates the estimate (~1.6x for a mixed Japanese message with the built-inCharacterEstimatorTokenizer, and more for pure CJK), so compaction and token-budget decisions fire on numbers that diverge from real LLM usage.Description
Serialize with
ensure_ascii=Falseso non-ASCII text is token-counted as the characters the model sees. This matches theensure_ascii=Falsealready used elsewhere in the same module, and it only affects token estimation — the serialized string is never stored or transmitted. Added a regression test that asserts the serialized payload keeps the real characters (no\uXXXX) and that its token count is lower than the escaped form's.Related Issue
Fixes #7022
Contribution Checklist
pytest tests/core/test_compaction.py, ruff format + check clean)