From 8c42ebddf9825ad3b83064cc729913248aef2ae1 Mon Sep 17 00:00:00 2001 From: sepandhaghighi Date: Mon, 8 Jun 2026 16:58:00 +0330 Subject: [PATCH 1/5] feat : add errors.py --- tocount/errors.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tocount/errors.py diff --git a/tocount/errors.py b/tocount/errors.py new file mode 100644 index 0000000..d6029f2 --- /dev/null +++ b/tocount/errors.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +"""Tocount errors.""" + +class TocountError(Exception): + """Base exception for all Tocount errors.""" + + pass + +class TocountValidationError(TocountError, ValueError): + """Base class for validation errors in Tocount.""" + + pass From e99b26c3c8703b8ec0c439e0acc658ce34233e7a Mon Sep 17 00:00:00 2001 From: sepandhaghighi Date: Mon, 8 Jun 2026 16:59:27 +0330 Subject: [PATCH 2/5] fix : update estimate_text_tokens function --- tocount/functions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tocount/functions.py b/tocount/functions.py index 3f68487..bb69dcf 100644 --- a/tocount/functions.py +++ b/tocount/functions.py @@ -4,6 +4,7 @@ from .params import TextEstimator, _TextEstimatorRuleBased, _TextEstimatorTikTokenR50K from .params import _TextEstimatorTikTokenCL100K, _TextEstimatorTikTokenO200K from .params import _TextEstimatorDeepseekR1, _TextEstimatorQwenQwQ, _TextEstimatorLlama_3_1 +from .errors import TocountValidationError from .rule_based.functions import universal_tokens_estimator, openai_tokens_estimator_gpt_3_5, openai_tokens_estimator_gpt_4 from .tiktoken_r50k.functions import linear_tokens_estimator_all as r50k_linear_all from .tiktoken_r50k.functions import linear_tokens_estimator_english as r50k_linear_english @@ -47,7 +48,7 @@ def estimate_text_tokens(text: str, estimator: TextEstimator = TextEstimator.DEF :return: tokens number """ if not isinstance(text, str): - raise ValueError(INVALID_TEXT_MESSAGE) + raise TocountValidationError(INVALID_TEXT_MESSAGE) if not isinstance(estimator, (TextEstimator, _TextEstimatorRuleBased, _TextEstimatorTikTokenR50K, _TextEstimatorTikTokenCL100K, _TextEstimatorTikTokenO200K, _TextEstimatorDeepseekR1, _TextEstimatorQwenQwQ, _TextEstimatorLlama_3_1)): - raise ValueError(INVALID_TEXT_ESTIMATOR_MESSAGE) + raise TocountValidationError(INVALID_TEXT_ESTIMATOR_MESSAGE) return text_estimator_map[estimator](text) From 63a6f71d669f0dd863a6676f137030bad985681c Mon Sep 17 00:00:00 2001 From: sepandhaghighi Date: Mon, 8 Jun 2026 17:01:21 +0330 Subject: [PATCH 3/5] fix : update tests --- tests/test_rule_based.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_rule_based.py b/tests/test_rule_based.py index 93fa2f7..70f9d82 100644 --- a/tests/test_rule_based.py +++ b/tests/test_rule_based.py @@ -1,5 +1,6 @@ import pytest from tocount import estimate_text_tokens, TextEstimator +from tocount import TocountValidationError from tocount.params import INVALID_TEXT_MESSAGE, INVALID_TEXT_ESTIMATOR_MESSAGE @@ -135,12 +136,12 @@ def test_openai_gpt_4_text_with_non_english(): def test_raises_error_for_invalid_text(): invalid_text = 12345 - with pytest.raises(ValueError, match=INVALID_TEXT_MESSAGE): + with pytest.raises(TocountValidationError, match=INVALID_TEXT_MESSAGE): estimate_text_tokens(invalid_text) def test_raises_error_for_invalid_estimator(): valid_text = "sample prompt" invalid_estimator = "not a valid estimator" - with pytest.raises(ValueError, match=INVALID_TEXT_ESTIMATOR_MESSAGE): + with pytest.raises(TocountValidationError, match=INVALID_TEXT_ESTIMATOR_MESSAGE): estimate_text_tokens(valid_text, invalid_estimator) From ecfaba86f40a1d2c5d3f975ac52b83c282d1f5cf Mon Sep 17 00:00:00 2001 From: sepandhaghighi Date: Mon, 8 Jun 2026 17:03:37 +0330 Subject: [PATCH 4/5] doc : update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65d44f4..b1a56dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `TocountError` class +- `TocountValidationError` class ### Changed - `setup.py` updated ## [0.5] - 2026-01-02 From 692fab664603bd0ddc6ac1c602d67f7b74a70ac7 Mon Sep 17 00:00:00 2001 From: sepandhaghighi Date: Mon, 8 Jun 2026 17:05:16 +0330 Subject: [PATCH 5/5] fix : update __init__.py --- tocount/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tocount/__init__.py b/tocount/__init__.py index 50ef7d4..70382cd 100644 --- a/tocount/__init__.py +++ b/tocount/__init__.py @@ -2,7 +2,8 @@ """Tocount modules.""" from .params import TOCOUNT_VERSION, TextEstimator +from .errors import TocountError, TocountValidationError from .functions import estimate_text_tokens __version__ = TOCOUNT_VERSION -__all__ = ["TextEstimator", "estimate_text_tokens"] +__all__ = ["TextEstimator", "estimate_text_tokens", "TocountError", "TocountValidationError"]