Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#12869·haystack

TiktokenCounter raises ValueError on literal special-token strings like `<|endoftext|>`

Author: bogdankosticCreated Sep 23, 2026Updated Sep 30, 2026
Labelsgood first issueContributions wanted!P2

Describe the bug

TiktokenCounter.count() raises ValueError when the conversation or tool schemas contain a literal tiktoken special-token string such as <|endoftext|>.

haystack/token_counters/tiktoken_counter.py:77 encodes the rendered conversation with Encoding.encode():

python
text_tokens = len(self._encoder.encode(_rendered_conversation(messages) + _rendered_tools(tools)))

encode() defaults to disallowed_special="all", so it raises on any text containing a special-token marker. Since allowed_special also defaults to set(), encode() can never actually emit a special token here — it can only raise. The strict check buys nothing and only turns ordinary source text into a crash.

This is the same bug that #12853 fixed for DocumentSplitter and RecursiveDocumentSplitter. TiktokenCounter is the one remaining place in haystack/ that encodes user-supplied text with a bare .encode().

Error message

ValueError: Encountered text corresponding to disallowed special token '<|endoftext|>'.
If you want this text to be encoded as a special token, pass it to `allowed_special`, e.g. `allowed_special={'<|endoftext|>', ...}`.
If you want this text to be encoded as normal text, disable the check for this token by passing `disallowed_special=(enc.special_tokens_set - {'<|endoftext|>'})`.
To disable this check for all special tokens, pass `disallowed_special=()`.

Expected behavior

The marker is counted as the ordinary text it is (<, |, endoftext, … as regular tokens) and count() returns a number, consistent with how the splitters now behave after #12853.

To Reproduce

python
from haystack.dataclasses import ChatMessage
from haystack.token_counters import TiktokenCounter

counter = TiktokenCounter()
counter.count([ChatMessage.from_user("The manual documents <|endoftext|> as a literal marker.")])

Raises the ValueError above. The tools path fails the same way (verified separately) — a Tool whose description contains the marker makes count(messages, tools=[tool]) raise even when no message contains one, because _rendered_conversation and _rendered_tools are concatenated into a single encode() call.

Additional context

Why this matters beyond the toy example. TiktokenCounter feeds the Agent conversation-compaction hooks (haystack/hooks/compaction/), which call token_counter.count(...) while deciding what to compact. The text being counted therefore includes tool results — web pages, retrieved documents, file contents — which the developer does not control. An agent that fetches a page of OpenAI/tiktoken documentation, or retrieves a document quoting <|endoftext|>, will crash inside compaction rather than during any step the developer wrote.

TiktokenCounter is opt-in (the compaction hooks default to ApproximateTokenCounter, which does not use tiktoken and is unaffected), but it is exactly the counter users select when they want accurate counts for a real context-window budget — i.e. long conversations, which is where compaction runs.

Suggested fix. One line, mirroring #12853:

python
text_tokens = len(self._encoder.encode_ordinary(_rendered_conversation(messages) + _rendered_tools(tools)))

encode_ordinary() is equivalent to encode(text, disallowed_special=()) and skips the special-token regex scan, so it is also marginally faster. No behavior changes for any input that works today, since encode() never emitted special tokens.

Worth adding a regression test alongside the existing TiktokenCounter tests, covering both a message body and a tool description containing a marker.

FAQ Check

  • Have you had a look at our new FAQ page?

System:

  • OS: macOS (reproduced), platform-independent
  • Haystack version: main @ 34960c9ea / 3.2.0-rc0

Source: deepset-ai/haystack

View original on GitHubView discussion on GitHub