-
Notifications
You must be signed in to change notification settings - Fork 36
fix: encode message content with split_special_tokens=True #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1819,6 +1819,7 @@ def attribute_text_segments( | |
| segments: "list[tuple[str, bool]]", | ||
| *, | ||
| overlap_is_content: bool = False, | ||
| split_special_tokens: bool = True, | ||
| ) -> "list[tuple[int, bool]]": | ||
| """Tokenize concatenated segments as a single BPE pass and return | ||
| ``(token_id, is_content)`` pairs. | ||
|
|
@@ -1867,6 +1868,7 @@ def attribute_text_segments( | |
| encoding = offset_tokenizer( | ||
| full_text, | ||
| add_special_tokens=False, | ||
| split_special_tokens=split_special_tokens, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This only closes the segmented helper path, but PrimeQwen3 is still registered for Useful? React with 👍 / 👎. |
||
| return_offsets_mapping=True, | ||
| ) | ||
| token_ids = list(encoding["input_ids"]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,7 +108,9 @@ def _token_id(self, token: str) -> int: | |
| def _encode(self, text: str) -> list[int]: | ||
| if not text: | ||
| return [] | ||
| return self._tokenizer.encode(text, add_special_tokens=False) | ||
| return self._tokenizer.encode( | ||
| text, add_special_tokens=False, split_special_tokens=True | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This helper is also used for renderer-owned scaffold strings, not just caller message text. In GLM5, for example, the Useful? React with 👍 / 👎. |
||
| ) | ||
|
|
||
| @staticmethod | ||
| def _visible_text(content: Any) -> str: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,7 +150,9 @@ def _token_id(self, token: str) -> int: | |
| def _encode(self, text: str) -> list[int]: | ||
| if not text: | ||
| return [] | ||
| return self._tokenizer.encode(text, add_special_tokens=False) | ||
| return self._tokenizer.encode( | ||
| text, add_special_tokens=False, split_special_tokens=True | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hy3 segment encode path unprotectedMedium Severity
Reviewed by Cursor Bugbot for commit 23a7029. Configure here. |
||
|
|
||
| @staticmethod | ||
| def _visible_text(content: Any) -> str: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,7 +138,9 @@ def _token_id(self, token: str) -> int: | |
| def _encode(self, text: str) -> list[int]: | ||
| if not text: | ||
| return [] | ||
| return self._tokenizer.encode(text, add_special_tokens=False) | ||
| return self._tokenizer.encode( | ||
| text, add_special_tokens=False, split_special_tokens=True | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| ) | ||
|
|
||
| @staticmethod | ||
| def _visible_text(content: Content | None) -> str: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,7 +85,9 @@ def _token_id(self, token: str) -> int: | |
| def _encode(self, text: str) -> list[int]: | ||
| if not text: | ||
| return [] | ||
| return self._tokenizer.encode(text, add_special_tokens=False) | ||
| return self._tokenizer.encode( | ||
| text, add_special_tokens=False, split_special_tokens=True | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minimax tool encode path unprotectedHigh Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 23a7029. Configure here. |
||
|
|
||
| @staticmethod | ||
| def _visible_text(content: Any) -> str: | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
attribute_text_segmentsis used for mixed scaffold/body runs, this new default also splits template-owned scaffold tokens, not just caller content. For example Qwen3 builds the tools system block through this helper and its scaffold contains literal<tool_call></tool_call>tags; with this default those no longer collapse to the tokenizer's tool-call special IDs, so any render withtools=diverges from the chat template/model wire format even though only user/body text needed protection.Useful? React with 👍 / 👎.