Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_special_token():

text = "<|endoftext|> hello <|fim_prefix|>"
assert eot not in enc.encode(text, disallowed_special=())
with pytest.raises(ValueError):
with pytest.raises(tiktoken.DisallowedSpecialTokenError):
enc.encode(text)
with pytest.raises(ValueError):
enc.encode(text, disallowed_special="all")
Expand Down
1 change: 1 addition & 0 deletions tiktoken/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This is the public API of tiktoken
from .core import DisallowedSpecialTokenError as DisallowedSpecialTokenError
from .core import Encoding as Encoding
from .model import encoding_for_model as encoding_for_model
from .model import encoding_name_for_model as encoding_name_for_model
Expand Down
6 changes: 5 additions & 1 deletion tiktoken/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import numpy.typing as npt


class DisallowedSpecialTokenError(ValueError):
"""Raised when text contains a special token that was not allowed."""


class Encoding:
def __init__(
self,
Expand Down Expand Up @@ -439,7 +443,7 @@ def _special_token_regex(tokens: frozenset[str]) -> re.Pattern[str]:


def raise_disallowed_special_token(token: str) -> NoReturn:
raise ValueError(
raise DisallowedSpecialTokenError(
f"Encountered text corresponding to disallowed special token {token!r}.\n"
"If you want this text to be encoded as a special token, "
f"pass it to `allowed_special`, e.g. `allowed_special={{{token!r}, ...}}`.\n"
Expand Down