Skip to content

fix: count literal special-token strings in TiktokenCounter - #12879

Closed
burakeyler wants to merge 1 commit into
deepset-ai:mainfrom
burakeyler:fix/tiktoken-counter-special-tokens
Closed

burakeyler wants to merge 1 commit into
deepset-ai:mainfrom
burakeyler:fix/tiktoken-counter-special-tokens

Conversation

@burakeyler

Copy link
Copy Markdown

Related Issues

Fixes #12869

Proposed Changes

TiktokenCounter.count() called Encoding.encode(), which defaults to disallowed_special="all" and therefore raises ValueError whenever a message or tool schema merely mentions a string like <|endoftext|>. Since allowed_special defaults to the empty set, no special tokens could ever be produced anyway, so the strict check only turned ordinary user text into a crash.

Switched to encode_ordinary(), the same fix applied to the document splitters in #12853, so special-token strings are counted as literal text.

How did you test it?

  • Updated the existing _FakeEncoder in the unit tests to the encode_ordinary entry point.
  • Added an integration test that counts a message containing <|endoftext|> with the real o200k_base encoding; it raises on main and passes here.
  • pytest test/token_counters/test_tiktoken_counter.py → 13 passed.

Checklist

🤖 Generated with Claude Code

@burakeyler
burakeyler requested a review from a team as a code owner September 23, 2026 12:27
@burakeyler
burakeyler requested review from bogdankostic and removed request for a team September 23, 2026 12:27
@vercel

vercel Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@burakeyler is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Signed-off-by: Burak Eyler <burakeyler@gmail.com>

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Hi @burakeyler, thanks for your interest in contributing to Haystack! 🙏

⚠️ Issue #12869 is already being addressed by open pull request(s) #12871, #12873, #12876. Before opening a PR for an issue, please check whether a PR is already linked to it, and consider contributing to the existing PR instead. We may close duplicate PRs to keep the review queue manageable.

This is an automated message to help us keep the review queue healthy.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @burakeyler, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from bogdankostic September 23, 2026 13:28
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Sep 23, 2026
@HaystackBot
HaystackBot marked this pull request as draft September 23, 2026 13:28
@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @burakeyler, just a friendly reminder: this PR is still in draft because the Contributor License Agreement (CLA) hasn't been signed yet. We'd love to review your contribution! Please sign the CLA via the link in the license/cla check, and this PR will automatically be marked ready for review.

@julian-risch

Copy link
Copy Markdown
Member

Thank you for your efforts! We're closing this PR because it duplicates #13009, which makes the same one-line fix to TiktokenCounter. This PR is also still a draft with the CLA unsigned, and its release note fails our backtick check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-pending PR is in draft until the contributor signs the CLA topic:tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants