You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TiktokenCounter.count() encoded the rendered conversation with a bare Encoding.encode(), which defaults to disallowed_special="all" and raises ValueError on any literal special-token string (e.g. <|endoftext|>) in message or tool content. Since allowed_special also defaults to empty, the strict check could never emit a real special token here — it only turned ordinary text into a crash.
This mirrors #12853, which fixed the same bug in DocumentSplitter/RecursiveDocumentSplitter: the counter now uses encode_ordinary(), so special-token-looking strings are counted as ordinary text. The class docstring, the tiktokencounter.mdx docs page, and a release note are updated accordingly.
How did you test it?
Reproduced the issue's exact snippet before the fix (raised ValueError) and verified it returns a count afterwards, for both the message path and the tool-schema path.
Added two regression tests in test/token_counters/test_tiktoken_counter.py.
Ran the full test/token_counters/ suite: 46 passed, 1 skipped (requires OPENAI_API_KEY); ruff check and ruff format --check are clean on the changed files.
Notes for the reviewer
The _FakeEncoder in the test module was renamed from encode to encode_ordinary to match the new call.
This contribution was prepared with the help of an AI coding assistant; all changes were reviewed and tested locally before submission.
I have updated the related issue with new insights and changes.
I have added unit tests and updated the docstrings.
I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
Hi @Naeemeh146, thanks for your interest in contributing to Haystack! 🙏
⚠️ Issue #12869 is already being addressed by open pull request(s) #12871, #12876, #12879, #12888. 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.
Quick note for triage: this PR is ready for review. The fix mirrors #12853 (encode_ordinary), with regression tests for both the message and tool-schema paths, a release note, and a docs update. Happy to address any feedback.
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
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.
Related Issues
<|endoftext|>#12869Proposed Changes:
TiktokenCounter.count()encoded the rendered conversation with a bareEncoding.encode(), which defaults todisallowed_special="all"and raisesValueErroron any literal special-token string (e.g.<|endoftext|>) in message or tool content. Sinceallowed_specialalso defaults to empty, the strict check could never emit a real special token here — it only turned ordinary text into a crash.This mirrors #12853, which fixed the same bug in
DocumentSplitter/RecursiveDocumentSplitter: the counter now usesencode_ordinary(), so special-token-looking strings are counted as ordinary text. The class docstring, thetiktokencounter.mdxdocs page, and a release note are updated accordingly.How did you test it?
ValueError) and verified it returns a count afterwards, for both the message path and the tool-schema path.test/token_counters/test_tiktoken_counter.py.test/token_counters/suite: 46 passed, 1 skipped (requiresOPENAI_API_KEY);ruff checkandruff format --checkare clean on the changed files.Notes for the reviewer
_FakeEncoderin the test module was renamed fromencodetoencode_ordinaryto match the new call.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.