Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat(tokenizer): preserve Python defaults with opt-in Rust dispatch (#42174)

devin-ai-integration[bot]0abd9267c106
diff --git a/.github/actions/cache-cargo-build/action.yml b/.github/actions/cache-cargo-build/action.yml
index c3b8ce22c68..222fad637fb 100644
--- a/.github/actions/cache-cargo-build/action.yml
+++ b/.github/actions/cache-cargo-build/action.yml
@@ -15,6 +15,12 @@ description: >-
cache the same directory for different workloads, and a shared key would let
whichever ran first deny the others a save.
+inputs:
+ profile:
+ description: "Cargo profile the build uses (dev or release)"
+ required: false
+ default: "dev"
+
runs:
using: composite
steps:
@@ -25,6 +31,6 @@ runs:
~/.cargo/registry
~/.cargo/git
litellm-rust/target
- key: ${{ runner.os }}-maturin-dev-${{ hashFiles('litellm-rust/Cargo.lock') }}
+ key: ${{ runner.os }}-maturin-${{ inputs.profile }}-${{ hashFiles('litellm-rust/Cargo.lock') }}
restore-keys: |
- ${{ runner.os }}-maturin-dev-
+ ${{ runner.os }}-maturin-${{ inputs.profile }}-
diff --git a/.github/scripts/verify_linux_native_wheel.py b/.github/scripts/verify_linux_native_wheel.py
index ea6d2401084..f2b82f86b47 100644
--- a/.github/scripts/verify_linux_native_wheel.py
+++ b/.github/scripts/verify_linux_native_wheel.py
@@ -134,7 +134,16 @@ def main(
uncompressed_wheel_size: Final = sum(member.file_size for member in wheel_members)
native_path: Final = wheel.parent / "native" / Path(native_member.filename).name
native_path.parent.mkdir(parents=True, exist_ok=True)
- native_path.write_bytes(archive.read(native_member))
+ native_bytes: Final = archive.read(native_member)
+ native_path.write_bytes(native_bytes)
+ duplicated_vocabularies: Final = tuple(
+ member.filename
+ for member in wheel_members
+ if member.filename.startswith("litellm/litellm_core_utils/tokenizers/")
+ and re.fullmatch(r"[0-9a-f]{40}", PurePosixPath(member.filename).name)
+ and member.file_size > 0
+ and archive.read(member) in native_bytes
+ )
wheel_metadata_tags_match: Final = (
len(wheel_metadata_tags) == len(expanded_filename_tags)
@@ -205,7 +214,7 @@ def main(
native_module: Final = load_native_module(native_path)
native_module_loads: Final = native_module is not None
panic_test_hook_absent: Final = native_module is not None and not hasattr(native_module, "_panic_for_test")
- native_size_limit: Final = 40_000_000
+ native_size_limit: Final = 35_000_000
native_size_within_limit: Final = native_member.file_size <= native_size_limit
validations: Final = (
(f"Python tag is {EXPECTED_PYTHON_TAG}", python_tag == EXPECTED_PYTHON_TAG),
@@ -223,6 +232,7 @@ def main(
("Native module loads", native_module_loads),
("Production module omits the panic test hook", panic_test_hook_absent),
(f"Native extension does not exceed {native_size_limit / 1_000_000:.0f} MB", native_size_within_limit),
+ ("Tokenizer vocabularies are not duplicated in the native extension", not duplicated_vocabularies),
("Wheel contents are valid", not unexpected_members),
)
diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml
index fd7513a3937..ec7e211faa1 100644
--- a/.github/workflows/codspeed.yml
+++ b/.github/workflows/codspeed.yml
@@ -13,6 +13,7 @@ on:
- ".github/workflows/codspeed.yml"
- ".github/actions/setup-uv-with-retries/**"
- ".github/actions/cache-cargo-build/**"
+ - ".github/scripts/uv_sync_with_retries.sh"
pull_request:
branches:
- main
@@ -25,6 +26,7 @@ on:
- ".github/workflows/codspeed.yml"
- ".github/actions/setup-uv-with-retries/**"
- ".github/actions/cache-cargo-build/**"
+ - ".github/scripts/uv_sync_with_retries.sh"
# Allow CodSpeed to trigger backtest performance analysis
# in order to generate initial data
workflow_dispatch:
@@ -59,19 +61,27 @@ jobs:
- name: Cache the Rust build
uses: ./.github/actions/cache-cargo-build
+ with:
+ profile: release
# Build the wheel and resolve every dependency outside the CodSpeed
# runner: the same maturin build took 42 minutes inside `codspeed run`
# versus under 3 minutes as a plain step (LIT-6183)
- - name: Build environment
+ - name: Build the release wheel
+ run: uv build --wheel --out-dir dist
+
+ - name: Install the wheel into the benchmark environment
+ run: |
+ UV_PROJECT_ENVIRONMENT="${RUNNER_TEMP}/benchmark-venv" .github/scripts/uv_sync_with_retries.sh --frozen --no-default-groups --group benchmarks --no-install-project --python 3.12
+ uv pip install --python "${RUNNER_TEMP}/benchmark-venv/bin/python" --no-deps dist/*.whl
+
+ - name: Collect benchmarks
+ env:
+ PYTEST_DISABLE_PLUGIN_AUTOLOAD: "1"
+ LITELLM_REQUIRE_INSTALLED_WHEEL: "1"
run: >
- env PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
- uv run --frozen --no-default-groups
- --with pytest==8.3.5
- --with pytest-codspeed==4.3.0
- --with "mcp>=2.2.0,<3.0"
- --with "a2a-sdk>=1.1.0,<2.0"
- pytest
+ "${RUNNER_TEMP}/benchmark-venv/bin/python" -I -m pytest
+ --import-mode=importlib
-p pytest_codspeed.plugin
tests/benchmarks/
--codspeed
@@ -82,13 +92,9 @@ jobs:
with:
mode: simulation
run: >
- env PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
- uv run --frozen --no-default-groups
- --with pytest==8.3.5
- --with pytest-codspeed==4.3.0
- --with "mcp>=2.2.0,<3.0"
- --with "a2a-sdk>=1.1.0,<2.0"
- pytest
+ env PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 LITELLM_REQUIRE_INSTALLED_WHEEL=1
+ "${RUNNER_TEMP}/benchmark-venv/bin/python" -I -m pytest
+ --import-mode=importlib
-p pytest_codspeed.plugin
tests/benchmarks/
--codspeed
diff --git a/backend/Dockerfile b/backend/Dockerfile
index 622fedcd70d..57e0a43a98d 100644
--- a/backend/Dockerfile
+++ b/backend/Dockerfile
@@ -61,6 +61,8 @@ RUN --mount=type=cache,target=/root/.cache/uv \
--extra saml \
--python python3.13
+RUN cp "$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"/litellm/rust_bridge/_native*.so litellm/rust_bridge/
+
RUN HOME=/opt/prisma XDG_CACHE_HOME=/opt/prisma/.cache PRISMA_BINARY_CACHE_DIR=/opt/prisma/binaries \
npm_config_cache=/root/.npm \
prisma generate --schema=./schema.prisma
diff --git a/litellm-rust/Cargo.lock b/litellm-rust/Cargo.lock
index 03e0dabbc17..37384cbfa53 100644
--- a/litellm-rust/Cargo.lock
+++ b/litellm-rust/Cargo.lock
@@ -3253,6 +3253,7 @@ dependencies = [
name = "litellm-token-counter-huggingface"
version = "0.1.0"
dependencies = [
+ "serde_json",
"thiserror 2.0.19",
"tokenizers",
]
@@ -3261,6 +3262,9 @@ dependencies = [
name = "litellm-token-counter-tiktoken"
version = "0.1.0"
dependencies = [
+ "base64 0.22.1",
+ "once_cell",
+ "rustc-hash",
"thiserror 2.0.19",
"tiktoken-rs",
]
diff --git a/litellm-rust/Cargo.toml b/litellm-rust/Cargo.toml
index 0e8941cb8e6..813d0713128 100644
--- a/litellm-rust/Cargo.toml
+++ b/litellm-rust/Cargo.toml
@@ -89,7 +89,7 @@ veil = "0.3.0"
[profile.release]
opt-level = 3
-lto = "thin"
+lto = "fat"
codegen-units = 1
panic = "unwind"
debug = false
diff --git a/litellm-rust/crates/host-python/src/execution.rs b/litellm-rust/crates/host-python/src/execution.rs
index 083c184e37e..b435bf241d2 100644
--- a/litellm-rust/crates/host-python/src/execution.rs
+++ b/litellm-rust/crates/host-python/src/execution.rs
@@ -29,7 +29,7 @@ pyo3::create_exception!(
static FORK_GATE: ForkGate = ForkGate::new();
-/// Whether this process has started the Tokio runtime.
+/// Whether this process has entered process-bound native execution.
pub fn runtime_started() -> bool {
FORK_GATE.started(std::process::id())
}
@@ -40,9 +40,8 @@ pub fn reserve_process_for_forking() -> Result<(), RuntimeAlreadyStarted> {
FORK_GATE.reserve(std::process::id())
}
-/// The only door to the Tokio runtime: every route reaches it through this module, which is
-/// what lets the gate speak for the whole extension. `clippy.toml` disallows going around it.
-fn enter_runtime() -> PyResult<()> {
+/// Claims process-bound native state before runtime startup or tokenizer execution.
+pub fn enter_native() -> PyResult<()> {
FORK_GATE
.enter(std::process::id())
.map_err(|refused| match refused {
@@ -60,7 +59,7 @@ fn enter_runtime() -> PyResult<()> {
#[expect(clippy::disallowed_methods, reason = "this is the gated door")]
fn runtime() -> PyResult<&'static Runtime> {
- enter_runtime()?;
+ enter_native()?;
Ok(pyo3_async_runtimes::tokio::get_runtime())
}
@@ -70,7 +69,7 @@ where
F: Future<Output = PyResult<T>> + Send + 'static,
T: for<'py> IntoPyObject<'py> + Send + 'static,
{
- enter_runtime()?;
+ enter_native()?;
pyo3_async_runtimes::tokio::future_into_py(py, future)
}
diff --git a/litellm-rust/crates/host-python/src/lib.rs b/litellm-rust/crates/host-python/src/lib.rs
index 7d164ab7535..4a33975a918 100644
--- a/litellm-rust/crates/host-python/src/lib.rs
+++ b/litellm-rust/crates/host-python/src/lib.rs
@@ -20,7 +20,7 @@ pub use argument::lookup;
pub use callable::wrap_failure;
pub use driver::run_call;
pub use execution::{
- ForkedAfterNativeRuntimeStarted, ProcessReservedForForking, poll_async_value,
+ ForkedAfterNativeRuntimeStarted, ProcessReservedForForking, enter_native, poll_async_value,
reserve_process_for_forking, run_async, run_async_value, run_sync, run_sync_value,
runtime_started,
};
diff --git a/litellm-rust/crates/python-bridge/Cargo.toml b/litellm-rust/crates/python-bridge/Cargo.toml
index 4e6c510d104..7846beef28a 100644
--- a/litellm-rust/crates/python-bridge/Cargo.toml
+++ b/litellm-rust/crates/python-bridge/Cargo.toml
@@ -10,7 +10,7 @@ name = "_native"
crate-type = ["cdylib"]
[features]
-default = ["abi3", "fast"]
+default = ["abi3", "fast", "huggingface", "tiktoken"]
abi3 = ["pyo3/abi3-py310"]
extension-module = ["pyo3/extension-module"]
panic-test = []
diff --git a/litellm-rust/crates/python-bridge/src/lib.rs b/litellm-rust/crates/python-bridge/src/lib.rs
index ed9bc90f650..b1fc5244d6f 100644
--- a/litellm-rust/crates/python-bridge/src/lib.rs
+++ b/litellm-rust/crates/python-bridge/src/lib.rs
@@ -12,7 +12,7 @@ mod routes;
reason = "secret-manager foundations await rollout activation"
)]
mod secrets;
-mod token_counter;
+mod tokenizer;
#[pymodule(gil_used = true)]
mod _native {
@@ -37,7 +37,12 @@ mod _native {
#[pymodule_export]
use crate::routes::responses::ResponsesWebSocketConnection;
#[pymodule_export]
- use crate::token_counter::TokenCounter;
+ use crate::routes::token_counter::TokenCounter;
+ #[cfg(feature = "huggingface")]
+ #[pymodule_export]
+ use crate::tokenizer::HuggingFaceEncoding;
+ #[pymodule_export]
+ use crate::tokenizer::Tokenizer;
#[pymodule_export]
use litellm_host_python::{ForkedAfterNativeRuntimeStarted, ProcessReservedForForking};
use pyo3::{prelude::*, types::PyModule};
@@ -83,10 +88,13 @@ mod tests {
"achat_completions",
"ResponsesWebSocketConnection",
"TokenCounter",
+ "Tokenizer",
"gil_stats",
"process_state_started",
"reserve_process_for_forking",
];
+ #[cfg(feature = "huggingface")]
+ expected.push("HuggingFaceEncoding");
expected.sort_unstable();
let mut public_names: Vec<String> = native_module(py)
diff --git a/litellm-rust/crates/python-bridge/src/routes/mod.rs b/litellm-rust/crates/python-bridge/src/routes/mod.rs
index 2d6b849a6b1..8a78a26423d 100644
--- a/litellm-rust/crates/python-bridge/src/routes/mod.rs
+++ b/litellm-rust/crates/python-bridge/src/routes/mod.rs
@@ -3,6 +3,7 @@ pub(crate) mod chat_completions;
pub(crate) mod messages;
pub(crate) mod ocr;
pub(crate) mod responses;
+pub(crate) mod token_counter;
#[cfg(test)]
mod tests {
diff --git a/litellm-rust/crates/python-bridge/src/routes/token_counter.rs b/litellm-rust/crates/python-bridge/src/routes/token_counter.rs
new file mode 100644
index 00000000000..168c4883b0b
--- /dev/null
+++ b/litellm-rust/crates/python-bridge/src/routes/token_counter.rs
@@ -0,0 +1,87 @@
+use std::sync::Arc;
+use std::{num::NonZero, thread::available_parallelism};
+
+use litellm_host_python::{enter_native, run_async};
+use litellm_token_counter::{
+ CountableRequest, Error, InputTokenCount, TokenCounter as CoreTokenCounter,
+};
+use pyo3::{
+ exceptions::{PyRuntimeError, PyValueError},
+ prelude::*,
+ types::PyAny,
+};
+use tokio::sync::Semaphore;
+
+use crate::errors::RustBridgeDeclined;
+use crate::tokenizer::Tokenizer;
+
+/// Counts the input tokens of a raw request body off the Python event loop with
+/// the GIL released. Python owns which requests get here and what to do with
+/// the count. At most one encode per core runs at a time; the rest wait in the
+/// async task, where a cancelled Python awaiter drops them before any blocking
+/// work is scheduled.
+#[pyclass(frozen)]
+pub(crate) struct TokenCounter {
+ inner: Arc<CoreTokenCounter>,
+ encode_slots: Arc<Semaphore>,
+}
+
+#[pymethods]
+impl TokenCounter {
+ #[staticmethod]
+ #[pyo3(signature = (tokenizer, fast = false))]
+ fn from_tokenizer(py: Python<'_>, tokenizer: &Tokenizer, fast: bool) -> PyResult<Self> {
+ enter_native()?;
+ let inner = CoreTokenCounter::new(tokenizer.counter(py, fast));
+ Ok(Self {
+ inner: Arc::new(inner),
+ encode_slots: Arc::new(Semaphore::new(encode_parallelism())),
+ })
+ }
+
+ fn acount_request<'py>(&self, py: Python<'py>, body: &[u8]) -> PyResult<Bound<'py, PyAny>> {
+ let counter = Arc::clone(&self.inner);
+ let encode_slots = Arc::clone(&self.encode_slots);
+ let body = body.to_vec();
+ run_async(
+ py,
+ async move {
+ let _slot = encode_slots
+ .acquire_owned()
+ .await
+ .map_err(|error| Error::Task(error.to_string()))?;
+ tokio::task::spawn_blocking(move || count_body(&counter, &body))
+ .await
+ .map_err(|error| Error::Task(error.to_string()))?
+ },
+ token_count_error_to_pyerr,
+ )
+ }
+}
+
+fn encode_parallelism() -> usize {
+ available_parallelism().map_or(1, NonZero::get)
+}
+
+fn count_body(counter: &CoreTokenCounter, body: &[u8]) -> Result<InputTokenCount, Error> {
+ let request = CountableRequest::parse(body)?;
+ counter.count_request(&request)
+}
+
+pub(crate) fn token_count_error_to_pyerr(error: Error) -> PyErr {
+ let message = error.to_string();
+ match error {
+ Error::Load(_)
+ | Error::Ranks(_)
+ | Error::UnicodeClasses
+ | Error::UnsupportedTokenizer(_) => PyValueError::new_err(message),
+ Error::RequestParse(_)
+ | Error::MissingInput
+ | Error::FloatText
+ | Error::ContentBlock
+ | Error::ArrayItems
+ | Error::JsonSerialization(_)
+ | Error::JsonUtf8(_) => RustBridgeDeclined::new_err(message),
+ Error::Encode(_) | Error::Decode(_) | Error::Task(_) => PyRuntimeError::new_err(message),
+ }
+}
diff --git a/litellm-rust/crates/python-bridge/src/token_counter.rs b/litellm-rust/crates/python-bridge/src/token_counter.rs
deleted file mode 100644
index 244401e6696..00000000000
--- a/litellm-rust/crates/python-bridge/src/token_counter.rs
+++ /dev/null
@@ -1,158 +0,0 @@
-use std::sync::Arc;
-
-#[cfg(any(feature = "fast", feature = "huggingface", feature = "tiktoken"))]
-use std::{num::NonZero, thread::available_parallelism};
-
-#[cfg(any(feature = "fast", feature = "huggingface", feature = "tiktoken"))]
-use litellm_host_python::release_gil;
-use litellm_host_python::run_async;
-use litellm_token_counter::{
- CountableRequest, Error, InputTokenCount, TokenCounter as CoreTokenCounter,
-};
-use pyo3::{
- exceptions::{PyRuntimeError, PyValueError},
- prelude::*,
- types::PyAny,
-};
-use tokio::sync::Semaphore;
-
-use crate::errors::RustBridgeDeclined;
-
-/// Counts the input tokens of a raw request body off the Python event loop with
-/// the GIL released. Python owns which requests get here and what to do with
-/// the count. At most one encode per core runs at a time; the rest wait in the
-/// async task, where a cancelled Python awaiter drops them before any blocking
-/// work is scheduled.
-#[pyclass(frozen)]
-pub(crate) struct TokenCounter {
- inner: Arc<CoreTokenCounter>,
- encode_slots: Arc<Semaphore>,
-}
-
-#[pymethods]
-impl TokenCounter {
- #[new]
- fn new(py: Python<'_>, tokenizer_json: &str) -> PyResult<Self> {
- #[cfg(feature = "fast")]
- {
- Self::load(py, || CoreTokenCounter::from_json_fast(tokenizer_json))
- }
- #[cfg(all(not(feature = "fast"), feature = "huggingface"))]
- {
- Self::load(py, || CoreTokenCounter::from_json(tokenizer_json))
- }
- #[cfg(not(any(feature = "fast", feature = "huggingface")))]
- {
- let _ = (py, tokenizer_json);
- Err(RustBridgeDeclined::new_err(
- "tokenizer backend requires the fast or huggingface feature",
- ))
- }
- }
-
- #[staticmethod]
- fn from_cl100k_ranks(py: Python<'_>, rank_file: &str) -> PyResult<Self> {
- #[cfg(feature = "fast")]
- {
- Self::load(py, || CoreTokenCounter::from_cl100k_ranks(rank_file))
- }
- #[cfg(not(feature = "fast"))]
- {
- let _ = (py, rank_file);
- Err(RustBridgeDeclined::new_err(
- "tokenizer backend requires the fast feature",
- ))
- }
- }
-
- #[staticmethod]
- fn from_o200k_ranks(py: Python<'_>, rank_file: &str) -> PyResult<Self> {
- #[cfg(feature = "fast")]
- {
- Self::load(py, || CoreTokenCounter::from_o200k_ranks(rank_file))
- }
- #[cfg(not(feature = "fast"))]
- {
- let _ = (py, rank_file);
- Err(RustBridgeDeclined::new_err(
- "tokenizer backend requires the fast feature",
- ))
- }
- }
-
- #[staticmethod]
- fn from_tiktoken(py: Python<'_>, encoding: &str) -> PyResult<Self> {
- #[cfg(feature = "tiktoken")]
- {
- Self::load(py, || CoreTokenCounter::from_tiktoken(encoding))
- }
- #[cfg(not(feature = "tiktoken"))]
- {
- let _ = (py, encoding);
- Err(RustBridgeDeclined::new_err(
- "tokenizer backend requires the tiktoken feature",
- ))
- }
- }
-
- fn acount_request<'py>(&self, py: Python<'py>, body: &[u8]) -> PyResult<Bound<'py, PyAny>> {
- let counter = Arc::clone(&self.inner);
- let encode_slots = Arc::clone(&self.encode_slots);
- let body = body.to_vec();
- run_async(
- py,
- async move {
- let _slot = encode_slots
- .acquire_owned()
- .await
- .map_err(|error| Error::Task(error.to_string()))?;
- tokio::task::spawn_blocking(move || count_body(&counter, &body))
- .await
- .map_err(|error| Error::Task(error.to_string()))?
- },
- token_count_error_to_pyerr,
- )
- }
-}
-
-impl TokenCounter {
- #[cfg(any(feature = "fast", feature = "huggingface", feature = "tiktoken"))]
- fn load(
- py: Python<'_>,
- load: impl FnOnce() -> Result<CoreTokenCounter, Error> + Send,
- ) -> PyResult<Self> {
- let inner = release_gil(py, load).map_err(token_count_error_to_pyerr)?;
- Ok(Self {
- inner: Arc::new(inner),
- encode_slots: Arc::new(Semaphore::new(encode_parallelism())),
- })
- }
-}
-
-#[cfg(any(feature = "fast", feature = "huggingface", feature = "tiktoken"))]
-fn encode_parallelism() -> usize {
- available_parallelism().map_or(1, NonZero::get)
-}
-
-fn count_body(counter: &CoreTokenCounter, body: &[u8]) -> Result<InputTokenCount, Error> {
- let request = CountableRequest::parse(body)?;
- counter.count_request(&request)
-}
-
-fn token_count_error_to_pyerr(error: Error) -> PyErr {
- let message = error.to_string();
- match error {
- Error::Load(_)
- | Error::Ranks(_)
- | Error::UnicodeClasses
- | Error::UnsupportedTokenizer(_) => PyValueError::new_err(message),
- Error::RequestParse(_)
- | Error::MissingInput
- | Error::FloatText
- | Error::ContentBlock
- | Error::ArrayItems
- | Error::JsonSerialization(_)
- | Error::JsonUtf8(_) => RustBridgeDeclined::new_err(message),
- Error::Encode(_) | Error::Task(_) => PyRuntimeError::new_err(message),
- }
-}
diff --git a/litellm-rust/crates/python-bridge/src/tokenizer.rs b/litellm-rust/crates/python-bridge/src/tokenizer.rs
new file mode 100644
index 00000000000..df219d55eb6
--- /dev/null
+++ b/litellm-rust/crates/python-bridge/src/tokenizer.rs
@@ -0,0 +1,713 @@
+//! The Python face of the text codecs: one `Tokenizer` class over the tiktoken and Hugging
+//! Face backends, carrying the read-only surface of `tiktoken.Encoding` and
+//! `tokenizers.Tokenizer` that `litellm/litellm_core_utils/tokenizer.py` wraps.
+use std::borrow::Cow;
+#[cfg(any(feature = "tiktoken", feature = "huggingface"))]
+use std::collections::HashMap;
+use std::sync::Arc;
+#[cfg(feature = "fast")]
+use std::sync::OnceLock;
+
+use litellm_host_python::{enter_native, release_gil};
+#[cfg(feature = "fast")]
+use litellm_token_counter::fast::{FastCounter, FastTokenizer};
+use litellm_token_counter::{Error, TextCodec};
+use pyo3::{exceptions::PyUnicodeEncodeError, prelude::*, types::PyString};
+
+#[cfg(any(feature = "tiktoken", feature = "huggingface"))]
+use pyo3::exceptions::PyValueError;
+#[cfg(feature = "huggingface")]
+use pyo3::{exceptions::PyIOError, types::PyDict};
+#[cfg(feature = "tiktoken")]
+use pyo3::{
+ exceptions::{PyKeyError, PyRuntimeError},
+ types::PyBytes,
+};
+
+#[cfg(not(all(feature = "tiktoken", feature = "huggingface")))]
+use crate::errors::RustBridgeDeclined;
+use crate::routes::token_counter::token_count_error_to_pyerr;
+
+#[cfg(feature = "huggingface")]
+use litellm_token_counter::huggingface::{
+ EncodeInput, Encoding, HuggingFaceTokenizer, InputSequence, PaddingDirection, PaddingStrategy,
+ TruncationDirection, encoding_from_json, encoding_to_json,
+};
+#[cfg(feature = "tiktoken")]
+use litellm_token_counter::tiktoken::{TiktokenTokenizer, Vocabulary};
+
+#[cfg(feature = "tiktoken")]
+pub(crate) fn load_tiktoken(py: Python<'_>, encoding: &str) -> PyResult<TiktokenTokenizer> {
+ enter_native()?;
+ let resource: std::path::PathBuf =
+ PyModule::import(py, "litellm.litellm_core_utils.tokenizers")?
+ .getattr("__file__")?
+ .extract()?;
+ release_gil(py, || {
+ TiktokenTokenizer::from_cached_ranks(encoding, |file| {
+ std::fs::read_to_string(resource.with_file_name(file))
+ })
+ })
+ .map_err(|error| token_count_error_to_pyerr(error.into()))
+}
+
+pub(crate) enum Codec {
+ #[cfg(feature = "tiktoken")]
+ Tiktoken(TiktokenTokenizer),
+ #[cfg(feature = "huggingface")]
+ HuggingFace(HuggingFaceTokenizer),
+}
+
+impl Codec {
+ pub(crate) fn codec(&self) -> &dyn TextCodec {
+ match *self {
+ #[cfg(feature = "tiktoken")]
+ Self::Tiktoken(ref tokenizer) => tokenizer,
+ #[cfg(feature = "huggingface")]
+ Self::HuggingFace(ref tokenizer) => tokenizer,
+ }
+ }
+
+ #[cfg(feature = "fast")]
+ fn fast_counter(&self) -> Option<FastTokenizer> {
+ match *self {
+ #[cfg(feature = "tiktoken")]
+ Self::Tiktoken(ref tokenizer) => tokenizer.fast_counter(),
+ #[cfg(feature = "huggingface")]
+ Self::HuggingFace(ref tokenizer) => tokenizer.fast_counter(),
+ }
+ }
+}
+
+/// The loaded model is shared: `TokenCounter::from_tokenizer` counts with the same parse,
+/// and the opt-in count-only counter is derived from it once, on first use.
+#[pyclass(frozen, module = "litellm.rust_bridge._native")]
+pub(crate) struct Tokenizer {
+ inner: Arc<Codec>,
+ #[cfg(feature = "fast")]
+ fast: OnceLock<Option<Arc<FastTokenizer>>>,
+}
+
+#[pymethods]
+impl Tokenizer {
+ #[staticmethod]
+ fn from_tiktoken(py: Python<'_>, encoding: &str) -> PyResult<Self> {
+ #[cfg(feature = "tiktoken")]
+ {
+ let tokenizer = load_tiktoken(py, encoding)?;
+ Ok(Self::new(Codec::Tiktoken(tokenizer)))
+ }
+ #[cfg(not(feature = "tiktoken"))]
+ {
+ let _ = (py, encoding);
+ Err(RustBridgeDeclined::new_err(
+ "tokenizer backend requires the tiktoken feature",
+ ))
+ }
+ }
+
+ #[staticmethod]
+ fn from_json(py: Python<'_>, tokenizer_json: &str) -> PyResult<Self> {
+ #[cfg(feature = "huggingface")]
+ {
+ enter_native()?;
+ let tokenizer = release_gil(py, || HuggingFaceTokenizer::from_json(tokenizer_json))
+ .map_err(|error| token_count_error_to_pyerr(error.into()))?;
+ Ok(Self::new(Codec::HuggingFace(tokenizer)))
+ }
+ #[cfg(not(feature = "huggingface"))]
+ {
+ let _ = (py, tokenizer_json);
+ Err(RustBridgeDeclined::new_err(
+ "tokenizer backend requires the huggingface feature",
+ ))
+ }
+ }
+
+ #[staticmethod]
+ #[pyo3(signature = (identifier, revision = "main", token = None))]
+ fn from_pretrained(
+ py: Python<'_>,
+ identifier: &str,
+ revision: &str,
+ token: Option<&str>,
+ ) -> PyResult<Self> {
+ #[cfg(feature = "huggingface")]
+ {
+ enter_native()?;
+ let kwargs = PyDict::new(py);
+ kwargs.set_item("repo_id", identifier)?;
+ kwargs.set_item("filename", "tokenizer.json")?;
+ kwargs.set_item("revision", revision)?;
+ kwargs.set_item("token", token)?;
+ let path: String = PyModule::import(py, "huggingface_hub")?
+ .getattr("hf_hub_download")?
+ .call((), Some(&kwargs))?
+ .extract()?;
+ let json =
+ release_gil(py, || std::fs::read_to_string(path)).map_err(PyIOError::new_err)?;
+ Self::from_json(py, &json)
+ }
+ #[cfg(not(feature = "huggingface"))]
+ {
+ let _ = (py, identifier, revision, token);
+ Err(RustBridgeDeclined::new_err(
+ "tokenizer backend requires the huggingface feature",
+ ))
+ }
+ }
+
+ fn encode(&self, py: Python<'_>, text: &Bound<'_, PyString>) -> PyResult<Vec<u32>> {
+ enter_native()?;
+ let text = self.text(text)?;
+ release_gil(py, || self.inner.codec().encode(&text)).map_err(token_count_error_to_pyerr)
+ }
+
+ #[pyo3(signature = (ids, skip_special_tokens = true))]
+ fn decode(&self, py: Python<'_>, ids: Vec<u32>, skip_special_tokens: bool) -> PyResult<String> {
+ enter_native()?;
+ release_gil(py, || self.inner.codec().decode(&ids, skip_special_tokens))
+ .map_err(token_count_error_to_pyerr)
+ }
+
+ #[pyo3(signature = (text, fast = false))]
+ fn count(&self, py: Python<'_>, text: &Bound<'_, PyString>, fast: bool) -> PyResult<usize> {
+ enter_native()?;
+ let text = self.text(text)?;
+ let counter = self.counter(py, fast);
+ release_gil(py, || {
+ litellm_token_counter::Tokenizer::count_tokens(&counter, &text)
+ })
+ .map_err(token_count_error_to_pyerr)
+ }
+
+ #[getter]
+ fn name(&self) -> &str {
+ self.inner.codec().name()
+ }
+
+ // ---- tiktoken: the `tiktoken.Encoding` surface ------------------------------------------
+
+ #[cfg(feature = "tiktoken")]
+ fn encode_special(
+ &self,
+ py: Python<'_>,
+ text: &Bound<'_, PyString>,
+ allowed: Vec<String>,
+ ) -> PyResult<Vec<u32>> {
+ enter_native()?;
+ let tokenizer = self.tiktoken()?;
+ let text = self.text(text)?;
+ release_gil(py, || tokenizer.encode_special(&text, &allowed))
+ .map_err(PyRuntimeError::new_err)
+ }
+
+ /// tiktoken's `encode_with_unstable`: `(stable_tokens, completions)`.
+ #[cfg(feature = "tiktoken")]
+ fn encode_with_unstable(
+ &self,
+ py: Python<'_>,
+ text: &Bound<'_, PyString>,
+ allowed: Vec<String>,
+ ) -> PyResult<(Vec<u32>, Vec<Vec<u32>>)> {
+ enter_native()?;
+ let tokenizer = self.tiktoken()?;
+ let text = self.text(text)?;
+ Ok(release_gil(py, || {
+ tokenizer.encode_with_unstable(&text, &allowed)
+ }))
+ }
+
+ /// The special tokens by text: tiktoken's `_special_tokens`.
+ #[cfg(feature = "tiktoken")]
+ fn special_tokens(&self) -> PyResult<HashMap<String, u32>> {
+ Ok(self
+ .vocabulary()?
+ .special_tokens()
+ .map(|(token, rank)| (token.to_owned(), rank))
+ .collect())
+ }
+
+ #[cfg(feature = "tiktoken")]
+ fn max_token_value(&self) -> PyResult<u32> {
+ Ok(self.vocabulary()?.max_token_value())
+ }
+
+ #[cfg(feature = "tiktoken")]
+ fn is_special_token(&self, token: u32) -> PyResult<bool> {
+ Ok(self.vocabulary()?.is_special_token(token))
+ }
+
+ /// Every mergeable token's bytes, sorted bytewise like tiktoken's `token_byte_values`.
+ #[cfg(feature = "tiktoken")]
+ fn token_byte_values<'py>(&self, py: Python<'py>) -> PyResult<Vec<Bound<'py, PyBytes>>> {
+ let vocabulary = self.vocabulary()?;
+ let values = release_gil(py, || vocabulary.token_byte_values());
+ Ok(values.iter().map(|value| PyBytes::new(py, value)).collect())
+ }
+
+ /// The token of one whole piece; `KeyError` when it is not in the vocabulary.
+ #[cfg(feature = "tiktoken")]
+ fn encode_single_token(&self, py: Python<'_>, piece: Vec<u8>) -> PyResult<u32> {
+ self.vocabulary()?
+ .encode_single_token(&piece)
+ .ok_or_else(|| PyKeyError::new_err(PyBytes::new(py, &piece).unbind()))
+ }
+
+ #[cfg(feature = "tiktoken")]
+ fn decode_bytes<'py>(&self, py: Python<'py>, ids: Vec<u32>) -> PyResult<Bound<'py, PyBytes>> {
+ enter_native()?;
+ let tokenizer = self.tiktoken()?;
+ let bytes =
+ release_gil(py, || tokenizer.decode_bytes(&ids)).map_err(PyKeyError::new_err)?;
+ Ok(PyBytes::new(py, &bytes))
+ }
+
+ // ---- Hugging Face: the `tokenizers.Tokenizer` surface -----------------------------------
+
+ #[cfg(feature = "huggingface")]
+ #[pyo3(signature = (sequence, pair = None, is_pretokenized = false, add_special_tokens = true, fast = false))]
+ fn encode_huggingface(
+ &self,
+ py: Python<'_>,
+ sequence: Sequence,
+ pair: Option<Sequence>,
+ is_pretokenized: bool,
+ add_special_tokens: bool,
+ fast: bool,
+ ) -> PyResult<HuggingFaceEncoding> {
+ enter_native()?;
+ let tokenizer = self.huggingface()?;
+ let sequence = sequence.input(is_pretokenized)?;
+ let input = match pair {
+ Some(pair) => EncodeInput::Dual(sequence, pair.input(is_pretokenized)?),
+ None => EncodeInput::Single(sequence),
+ };
+ release_gil(py, || {
+ tokenizer.encode_result(input, add_special_tokens, fast)
+ })
+ .map(|inner| HuggingFaceEncoding { inner })
+ .map_err(|error| token_count_error_to_pyerr(Error::from(error)))
+ }
+
+ #[cfg(feature = "huggingface")]
+ #[pyo3(signature = (inputs, is_pretokenized = false, add_special_tokens = true, fast = false))]
+ fn encode_batch_huggingface(
+ &self,
+ py: Python<'_>,
+ inputs: Vec<(Sequence, Option<Sequence>)>,
+ is_pretokenized: bool,
+ add_special_tokens: bool,
+ fast: bool,
+ ) -> PyResult<Vec<HuggingFaceEncoding>> {
+ enter_native()?;
+ let tokenizer = self.huggingface()?;
+ let inputs = inputs
+ .into_iter()
+ .map(|(sequence, pair)| {
+ let sequence = sequence.input(is_pretokenized)?;
+ match pair {
+ Some(pair) => Ok(EncodeInput::Dual(sequence, pair.input(is_pretokenized)?)),
+ None => Ok(EncodeInput::Single(sequence)),
+ }
+ })
+ .collect::<PyResult<Vec<_>>>()?;
+ release_gil(py, || {
+ tokenizer.encode_batch_result(inputs, add_special_tokens, fast)
+ })
+ .map(|encodings| {
+ encodings
+ .into_iter()
+ .map(|inner| HuggingFaceEncoding { inner })
+ .collect()
+ })
+ .map_err(|error| token_count_error_to_pyerr(Error::from(error)))
+ }
+
+ #[cfg(feature = "huggingface")]
+ #[pyo3(signature = (pretty = false))]
+ fn to_json(&self, py: Python<'_>, pretty: bool) -> PyResult<String> {
+ enter_native()?;
+ let tokenizer = self.huggingface()?;
+ release_gil(py, || tokenizer.to_json(pretty))
+ .map_err(|error| token_count_error_to_pyerr(Error::from(error)))
+ }
+
+ #[cfg(feature = "huggingface")]
+ fn token_to_id(&self, token: &str) -> PyResult<Option<u32>> {
+ Ok(self.huggingface()?.token_to_id(token))
+ }
+
+ #[cfg(feature = "huggingface")]
+ fn id_to_token(&self, id: u32) -> PyResult<Option<String>> {
+ Ok(self.huggingface()?.id_to_token(id))
+ }
+
+ #[cfg(feature = "huggingface")]
+ #[pyo3(signature = (with_added_tokens = true))]
+ fn get_vocab(&self, py: Python<'_>, with_added_tokens: bool) -> PyResult<HashMap<String, u32>> {
+ let tokenizer = self.huggingface()?;
+ Ok(release_gil(py, || tokenizer.vocab(with_added_tokens)))
+ }
+
+ #[cfg(feature = "huggingface")]
+ #[pyo3(signature = (with_added_tokens = true))]
+ fn get_vocab_size(&self, with_added_tokens: bool) -> PyResult<usize> {
+ Ok(self.huggingface()?.vocab_size(with_added_tokens))
+ }
+
+ /// The added tokens by id as `(id, (content, single_word, lstrip, rstrip, normalized,
+ /// special))`, for Python to rebuild as `tokenizers.AddedToken`.
+ #[cfg(feature = "huggingface")]
+ fn added_tokens_decoder(&self) -> PyResult<Vec<(u32, AddedTokenFields)>> {
+ Ok(self
+ .huggingface()?
+ .added_tokens_decoder()
+ .into_iter()
+ .map(|(id, token)| {
+ (
+ id,
+ (
+ token.content,
+ token.single_word,
+ token.lstrip,
+ token.rstrip,
+ token.normalized,
+ token.special,
+ ),
+ )
+ })
+ .collect())
+ }
+
+ /// The padding parameters as `tokenizers.Tokenizer.padding` reports them.
+ #[cfg(feature = "huggingface")]
+ fn padding<'py>(&self, py: Python<'py>) -> PyResult<Option<Bound<'py, PyDict>>> {
+ let Some(params) = self.huggingface()?.padding() else {
+ return Ok(None);
+ };
+ let padding = PyDict::new(py);
+ padding.set_item(
+ "length",
+ match params.strategy {
+ PaddingStrategy::BatchLongest => None,
+ PaddingStrategy::Fixed(length) => Some(length),
+ },
+ )?;
+ padding.set_item("pad_to_multiple_of", params.pad_to_multiple_of)?;
+ padding.set_item("pad_id", params.pad_id)?;
+ padding.set_item("pad_type_id", params.pad_type_id)?;
+ padding.set_item("pad_token", &params.pad_token)?;
+ padding.set_item("direction", params.direction.as_ref())?;
+ Ok(Some(padding))
+ }
+
+ /// The truncation parameters as `tokenizers.Tokenizer.truncation` reports them.
+ #[cfg(feature = "huggingface")]
+ fn truncation<'py>(&self, py: Python<'py>) -> PyResult<Option<Bound<'py, PyDict>>> {
+ let Some(params) = self.huggingface()?.truncation() else {
+ return Ok(None);
+ };
+ let truncation = PyDict::new(py);
+ truncation.set_item("max_length", params.max_length)?;
+ truncation.set_item("stride", params.stride)?;
+ truncation.set_item("strategy", params.strategy.as_ref())?;
+ truncation.set_item("direction", params.direction.as_ref())?;
+ Ok(Some(truncation))
+ }
+
+ #[cfg(feature = "huggingface")]
+ fn num_special_tokens_to_add(&self, is_pair: bool) -> PyResult<usize> {
+ Ok(self.huggingface()?.num_special_tokens_to_add(is_pair))
+ }
+
+ #[cfg(feature = "huggingface")]
+ fn encode_special_tokens(&self) -> PyResult<bool> {
+ Ok(self.huggingface()?.encode_special_tokens())
+ }
+}
+
+#[cfg(feature = "huggingface")]
+type AddedTokenFields = (String, bool, bool, bool, bool, bool);
+
+impl Tokenizer {
+ fn new(inner: Codec) -> Self {
+ Self {
+ inner: Arc::new(inner),
+ #[cfg(feature = "fast")]
+ fast: OnceLock::new(),
+ }
+ }
+
+ pub(crate) fn counter(&self, py: Python<'_>, fast: bool) -> SharedCounter {
+ #[cfg(feature = "fast")]
+ if fast {
+ let counter = self.fast.get().unwrap_or_else(|| {
+ release_gil(py, || {
+ self.fast
+ .get_or_init(|| self.inner.fast_counter().map(Arc::new))
+ })
+ });
+ if let Some(counter) = counter {
+ return SharedCounter::Fast(Arc::clone(counter));
+ }
+ }
+ #[cfg(not(feature = "fast"))]
+ let _ = (py, fast);
+ SharedCounter::Codec(Arc::clone(&self.inner))
+ }
+
+ /// A Python `str` as UTF-8. tiktoken replaces lone surrogates the way its Python `encode`
+ /// does; `tokenizers` rejects them, so that backend keeps the encode error.
+ fn text<'a>(&self, text: &'a Bound<'_, PyString>) -> PyResult<Cow<'a, str>> {
+ match text.to_cow() {
+ Ok(text) => Ok(text),
+ Err(error) => match *self.inner {
+ #[cfg(feature = "tiktoken")]
+ Codec::Tiktoken(_) if error.is_instance_of::<PyUnicodeEncodeError>(text.py()) => {
+ text.call_method1("encode", ("utf-16", "surrogatepass"))?
+ .call_method1("decode", ("utf-16", "replace"))?
+ .extract::<String>()
+ .map(Cow::Owned)
+ }
+ _ => Err(error),
+ },
+ }
+ }
+
+ #[cfg(feature = "tiktoken")]
+ fn tiktoken(&self) -> PyResult<&TiktokenTokenizer> {
+ match *self.inner {
+ Codec::Tiktoken(ref tokenizer) => Ok(tokenizer),
+ #[cfg(feature = "huggingface")]
+ Codec::HuggingFace(_) => Err(PyValueError::new_err("requires a tiktoken encoding")),
+ }
+ }
+
+ #[cfg(feature = "tiktoken")]
+ fn vocabulary(&self) -> PyResult<&Vocabulary> {
+ self.tiktoken()?.vocabulary().ok_or_else(|| {
+ PyRuntimeError::new_err("this encoding was built without its vocabulary")
+ })
+ }
+
+ #[cfg(feature = "huggingface")]
+ fn huggingface(&self) -> PyResult<&HuggingFaceTokenizer> {
+ match *self.inner {
+ Codec::HuggingFace(ref tokenizer) => Ok(tokenizer),
+ #[cfg(feature = "tiktoken")]
+ Codec::Tiktoken(_) => Err(PyValueError::new_err("requires a Hugging Face tokenizer")),
+ }
+ }
+}
+
+pub(crate) enum SharedCounter {
+ Codec(Arc<Codec>),
+ #[cfg(feature = "fast")]
+ Fast(Arc<FastTokenizer>),
+}
+
+impl litellm_token_counter::Tokenizer for SharedCounter {
+ fn count_tokens(&self, text: &str) -> Result<usize, Error> {
+ match self {
+ Self::Codec(codec) => codec.codec().count_tokens(text),
+ #[cfg(feature = "fast")]
+ Self::Fast(counter) => counter.count_tokens(text).map_err(Error::from),
+ }
+ }
+}
+
+#[cfg(feature = "huggingface")]
+#[derive(FromPyObject)]
+pub(crate) enum Sequence {
+ Text(String),
+ Words(Vec<String>),
+}
+
+#[cfg(feature = "huggingface")]
+impl Sequence {
+ fn input(self, is_pretokenized: bool) -> PyResult<InputSequence<'static>> {
+ match (self, is_pretokenized) {
+ (Self::Text(text), false) => Ok(text.into()),
+ (Self::Words(words), true) => Ok(words.into()),
+ _ => Err(pyo3::exceptions::PyTypeError::new_err(
+ "input must match is_pretokenized",
+ )),
+ }
+ }
+}
+
+#[cfg(feature = "huggingface")]
+fn direction<T>(value: &str, left: T, right: T, what: &str) -> PyResult<T> {
+ match value {
+ "left" => Ok(left),
+ "right" => Ok(right),
+ other => Err(PyValueError::new_err(format!(
+ "invalid {what} direction {other:?}: expected 'left' or 'right'"
+ ))),
+ }
+}
+
+/// `tokenizers.Encoding`, mutable like the original: `pad`, `truncate` and `set_sequence_id`
+/// change it in place.
+#[cfg(feature = "huggingface")]
+#[pyclass(module = "litellm.rust_bridge._native")]
+pub(crate) struct HuggingFaceEncoding {
+ inner: Encoding,
+}
+
+#[cfg(feature = "huggingface")]
+#[pymethods]
+impl HuggingFaceEncoding {
+ #[new]
+ #[pyo3(signature = (json = None))]
+ fn new(json: Option<&str>) -> PyResult<Self> {
+ let inner = match json {
+ Some(json) => encoding_from_json(json)
+ .map_err(|error| PyValueError::new_err(error.to_string()))?,
+ None => Encoding::default(),
+ };
+ Ok(Self { inner })
+ }
+
+ #[staticmethod]
+ #[pyo3(signature = (encodings, growing_offsets = true))]
+ fn merge(encodings: Vec<PyRef<'_, Self>>, growing_offsets: bool) -> Self {
+ Self {
+ inner: Encoding::merge(
+ encodings.iter().map(|encoding| encoding.inner.clone()),
+ growing_offsets,
+ ),
+ }
+ }
+
+ fn __reduce__<'py>(
+ &self,
+ py: Python<'py>,
+ ) -> PyResult<(Bound<'py, pyo3::types::PyType>, (String,))> {
+ let json = encoding_to_json(&self.inner)
+ .map_err(|error| PyValueError::new_err(error.to_string()))?;
+ Ok((py.get_type::<Self>(), (json,)))
+ }
+
+ fn __repr__(&self) -> String {
+ format!(
+ "Encoding(num_tokens={}, attributes=[ids, type_ids, tokens, offsets, \
+ attention_mask, special_tokens_mask, overflowing])",
+ self.inner.len()
+ )
+ }
+
+ fn __len__(&self) -> usize {
+ self.inner.len()
+ }
+ #[getter]
+ fn ids(&self) -> Vec<u32> {
+ self.inner.get_ids().to_vec()
+ }
+ #[getter]
+ fn tokens(&self) -> Vec<String> {
+ self.inner.get_tokens().to_vec()
+ }
+ #[getter]
+ fn offsets(&self) -> Vec<(usize, usize)> {
+ self.inner.get_offsets().to_vec()
+ }
+ #[getter]
+ fn type_ids(&self) -> Vec<u32> {
+ self.inner.get_type_ids().to_vec()
+ }
+ #[getter]
+ fn attention_mask(&self) -> Vec<u32> {
+ self.inner.get_attention_mask().to_vec()
+ }
+ #[getter]
+ fn special_tokens_mask(&self) -> Vec<u32> {
+ self.inner.get_special_tokens_mask().to_vec()
+ }
+ #[getter]
+ fn word_ids(&self) -> Vec<Option<u32>> {
+ self.inner.get_word_ids().to_vec()
+ }
+ #[getter]
+ fn sequence_ids(&self) -> Vec<Option<usize>> {
+ self.inner.get_sequence_ids()
+ }
+ #[getter]
+ fn overflowing(&self) -> Vec<Self> {
+ self.inner
+ .get_overflowing()
+ .iter()
+ .cloned()
+ .map(|inner| Self { inner })
+ .collect()
+ }
+ #[getter]
+ fn n_sequences(&self) -> usize {
+ self.inner.n_sequences()
+ }
+
+ #[pyo3(signature = (word_index, sequence_index = 0))]
+ fn word_to_tokens(&self, word_index: u32, sequence_index: usize) -> Option<(usize, usize)> {
+ self.inner.word_to_tokens(word_index, sequence_index)
+ }
+ #[pyo3(signature = (word_index, sequence_index = 0))]
+ fn word_to_chars(&self, word_index: u32, sequence_index: usize) -> Option<(usize, usize)> {
+ self.inner.word_to_chars(word_index, sequence_index)
+ }
+ fn token_to_sequence(&self, token_index: usize) -> Option<usize> {
+ self.inner.token_to_sequence(token_index)
+ }
+ fn token_to_chars(&self, token_index: usize) -> Option<(usize, usize)> {
+ self.inner
+ .token_to_chars(token_index)
+ .map(|(_, offsets)| offsets)
+ }
+ fn token_to_word(&self, token_index: usize) -> Option<u32> {
+ self.inner.token_to_word(token_index).map(|(_, word)| word)
+ }
+ #[pyo3(signature = (char_pos, sequence_index = 0))]
+ fn char_to_token(&self, char_pos: usize, sequence_index: usize) -> Option<usize> {
+ self.inner.char_to_token(char_pos, sequence_index)
+ }
+ #[pyo3(signature = (char_pos, sequence_index = 0))]
+ fn char_to_word(&self, char_pos: usize, sequence_index: usize) -> Option<u32> {
+ self.inner.char_to_word(char_pos, sequence_index)
+ }
+
+ fn set_sequence_id(&mut self, sequence_id: usize) {
+ self.inner.set_sequence_id(sequence_id);
+ }
+
+ #[pyo3(signature = (length, direction = "right", pad_id = 0, pad_type_id = 0, pad_token = "[PAD]"))]
+ fn pad(
+ &mut self,
+ length: usize,
+ direction: &str,
+ pad_id: u32,
+ pad_type_id: u32,
+ pad_token: &str,
+ ) -> PyResult<()> {
+ let direction = self::direction(
+ direction,
+ PaddingDirection::Left,
+ PaddingDirection::Right,
+ "padding",
+ )?;
+ self.inner
+ .pad(length, pad_id, pad_type_id, pad_token, direction);
+ Ok(())
+ }
+
+ #[pyo3(signature = (max_length, stride = 0, direction = "right"))]
+ fn truncate(&mut self, max_length: usize, stride: usize, direction: &str) -> PyResult<()> {
+ let direction = self::direction(
+ direction,
+ TruncationDirection::Left,
+ TruncationDirection::Right,
+ "truncation",
+ )?;
+ self.inner.truncate(max_length, stride, direction);
+ Ok(())
+ }
+}
diff --git a/litellm-rust/crates/token-counter-fast/src/lib.rs b/litellm-rust/crates/token-counter-fast/src/lib.rs
index ce91af642ea..157ee847658 100644
--- a/litellm-rust/crates/token-counter-fast/src/lib.rs
+++ b/litellm-rust/crates/token-counter-fast/src/lib.rs
@@ -8,6 +8,8 @@ mod scanner;
mod tiktoken;
mod unicode_classes;
+use std::sync::Arc;
+
use byte_level::ByteLevelCounter;
use scanner::{SplitPattern, TiktokenCounter};
@@ -15,22 +17,29 @@ pub use error::Error;
enum Encoder {
HuggingFace {
- tokenizer: Box<tokenizers::Tokenizer>,
+ tokenizer: Arc<tokenizers::Tokenizer>,
byte_level: Option<ByteLevelCounter>,
},
Tiktoken(TiktokenCounter),
}
+/// A count-only tokenizer. Its model tables are immutable, so one built from an already
+/// loaded model (`from_shared`, `from_*_pairs`) adds only the count-specific tables.
pub struct FastTokenizer(Encoder);
impl FastTokenizer {
pub fn from_json(json: &str) -> Result<Self, Error> {
let tokenizer = json.parse::<tokenizers::Tokenizer>().map_err(Error::Load)?;
+ Ok(Self::from_shared(Arc::new(tokenizer)))
+ }
+
+ /// Counts with a Hugging Face model another codec already holds; nothing is re-parsed.
+ pub fn from_shared(tokenizer: Arc<tokenizers::Tokenizer>) -> Self {
let byte_level = ByteLevelCounter::detect(&tokenizer);
- Ok(Self(Encoder::HuggingFace {
- tokenizer: Box::new(tokenizer),
+ Self(Encoder::HuggingFace {
+ tokenizer,
byte_level,
- }))
+ })
}
pub fn from_cl100k_ranks(ranks: &str) -> Result<Self, Error> {
@@ -41,12 +50,36 @@ impl FastTokenizer {
Self::from_ranks(SplitPattern::O200k, ranks)
}
+ /// `cl100k_base` from ranks another loader already parsed.
+ pub fn from_cl100k_pairs<'a>(
+ pairs: impl IntoIterator<Item = (&'a [u8], u32)>,
+ ) -> Result<Self, Error> {
+ Self::from_pairs(SplitPattern::Cl100k, pairs)
+ }
+
+ /// `o200k_base` (and `o200k_harmony`, whose ordinary tokens are the same) from ranks
+ /// another loader already parsed.
+ pub fn from_o200k_pairs<'a>(
+ pairs: impl IntoIterator<Item = (&'a [u8], u32)>,
+ ) -> Result<Self, Error> {
+ Self::from_pairs(SplitPattern::O200k, pairs)
+ }
+
fn from_ranks(split: SplitPattern, ranks: &str) -> Result<Self, Error> {
TiktokenCounter::from_ranks(split, ranks)
.map(Encoder::Tiktoken)
.map(Self)
}
+ fn from_pairs<'a>(
+ split: SplitPattern,
+ pairs: impl IntoIterator<Item = (&'a [u8], u32)>,
+ ) -> Result<Self, Error> {
+ TiktokenCounter::from_pairs(split, pairs)
+ .map(Encoder::Tiktoken)
+ .map(Self)
+ }
+
pub fn count_tokens(&self, text: &str) -> Result<usize, Error> {
match &self.0 {
Encoder::Tiktoken(counter) => Ok(counter.count(text)),
diff --git a/litellm-rust/crates/token-counter-fast/src/scanner.rs b/litellm-rust/crates/token-counter-fast/src/scanner.rs
index c2c3057aeeb..882ea91db81 100644
--- a/litellm-rust/crates/token-counter-fast/src/scanner.rs
+++ b/litellm-rust/crates/token-counter-fast/src/scanner.rs
@@ -39,8 +39,19 @@ pub(super) struct TiktokenCounter {
impl TiktokenCounter {
pub(super) fn from_ranks(split: SplitPattern, rank_file: &str) -> Result<Self, Error> {
+ Self::new(split, MergeRanks::parse(rank_file)?)
+ }
+
+ pub(super) fn from_pairs<'a>(
+ split: SplitPattern,
+ pairs: impl IntoIterator<Item = (&'a [u8], u32)>,
+ ) -> Result<Self, Error> {
+ Self::new(split, MergeRanks::from_pairs(pairs)?)
+ }
+
+ fn new(split: SplitPattern, ranks: MergeRanks) -> Result<Self, Error> {
Ok(Self {
- ranks: MergeRanks::parse(rank_file)?,
+ ranks,
piece_len: split.piece_len(),
unicode_classes: UnicodeClasses::get().ok_or(Error::UnicodeClasses)?,
})
diff --git a/litellm-rust/crates/token-counter-fast/src/tiktoken.rs b/litellm-rust/crates/token-counter-fast/src/tiktoken.rs
index 16172b7a688..68f09b14a25 100644
--- a/litellm-rust/crates/token-counter-fast/src/tiktoken.rs
+++ b/litellm-rust/crates/token-counter-fast/src/tiktoken.rs
@@ -22,11 +22,25 @@ pub(super) struct MergeRanks(FxHashMap<Box<[u8]>, Rank>);
impl MergeRanks {
pub(super) fn parse(text: &str) -> Result<Self, Error> {
- let ranks = text
- .lines()
- .filter(|line| !line.is_empty())
- .map(parse_line)
- .collect::<Result<FxHashMap<_, _>, _>>()?;
+ Self::from_entries(text.lines().filter(|line| !line.is_empty()).map(parse_line))
+ }
+
+ /// The same table from ranks another loader already parsed.
+ pub(super) fn from_pairs<'a>(
+ pairs: impl IntoIterator<Item = (&'a [u8], Rank)>,
+ ) -> Result<Self, Error> {
+ Self::from_entries(pairs.into_iter().map(|(bytes, rank)| {
+ if rank == NO_RANK {
+ return Err(Error::Ranks(format!("rank {rank} is reserved")));
+ }
+ Ok((Box::from(bytes), rank))
+ }))
+ }
+
+ fn from_entries(
+ entries: impl Iterator<Item = Result<(Box<[u8]>, Rank), Error>>,
+ ) -> Result<Self, Error> {
+ let ranks = entries.collect::<Result<FxHashMap<_, _>, _>>()?;
if let Some(byte) = (0..=u8::MAX).find(|byte| !ranks.contains_key(&[*byte][..])) {
return Err(Error::Ranks(format!("byte 0x{byte:02X} has no token")));
}
diff --git a/litellm-rust/crates/token-counter-huggingface/Cargo.toml b/litellm-rust/crates/token-counter-huggingface/Cargo.toml
index 6d8cb85e524..a5c2b1bb160 100644
--- a/litellm-rust/crates/token-counter-huggingface/Cargo.toml
+++ b/litellm-rust/crates/token-counter-huggingface/Cargo.toml
@@ -6,5 +6,6 @@ license.workspace = true
repository.workspace = true
[dependencies]
+serde_json.workspace = true
thiserror.workspace = true
tokenizers.workspace = true
diff --git a/litellm-rust/crates/token-counter-huggingface/src/error.rs b/litellm-rust/crates/token-counter-huggingface/src/error.rs
index adc4551886f..e7f6260321b 100644
--- a/litellm-rust/crates/token-counter-huggingface/src/error.rs
+++ b/litellm-rust/crates/token-counter-huggingface/src/error.rs
@@ -6,4 +6,6 @@ pub enum Error {
Load(#[source] tokenizers::Error),
#[error("tokenization failed: {0}")]
Encode(#[source] tokenizers::Error),
+ #[error("token decoding failed: {0}")]
+ Decode(#[source] tokenizers::Error),
}
diff --git a/litellm-rust/crates/token-counter-huggingface/src/lib.rs b/litellm-rust/crates/token-counter-huggingface/src/lib.rs
index 8e05c2cca46..170a36aea05 100644
--- a/litellm-rust/crates/token-counter-huggingface/src/lib.rs
+++ b/litellm-rust/crates/token-counter-huggingface/src/lib.rs
@@ -2,22 +2,243 @@
mod error;
+use std::collections::{HashMap, HashSet};
+use std::sync::Arc;
+
pub use error::Error;
+use tokenizers::PostProcessor;
+pub use tokenizers::{
+ AddedToken, EncodeInput, Encoding, InputSequence, PaddingDirection, PaddingParams,
+ PaddingStrategy, TruncationDirection, TruncationParams,
+};
+
+pub fn encoding_from_json(json: &str) -> Result<Encoding, Error> {
+ serde_json::from_str(json).map_err(|error| Error::Load(error.into()))
+}
-pub struct HuggingFaceTokenizer(Box<tokenizers::Tokenizer>);
+pub fn encoding_to_json(encoding: &Encoding) -> Result<String, Error> {
+ serde_json::to_string(encoding).map_err(|error| Error::Load(error.into()))
+}
+
+pub struct HuggingFaceTokenizer {
+ tokenizer: Arc<tokenizers::Tokenizer>,
+ special_token_ids: HashSet<u32>,
+}
impl HuggingFaceTokenizer {
pub fn from_json(json: &str) -> Result<Self, Error> {
json.parse::<tokenizers::Tokenizer>()
- .map(Box::new)
- .map(Self)
+ .map(Self::new)
.map_err(Error::Load)
}
+ fn new(tokenizer: tokenizers::Tokenizer) -> Self {
+ let special_token_ids: HashSet<u32> = tokenizer
+ .get_added_tokens_decoder()
+ .into_iter()
+ .filter_map(|(id, token)| token.special.then_some(id))
+ .collect();
+ Self {
+ tokenizer: Arc::new(tokenizer),
+ special_token_ids,
+ }
+ }
+
+ /// The parsed model, for a count-only counter to share instead of parsing it again.
+ pub fn shared(&self) -> Arc<tokenizers::Tokenizer> {
+ Arc::clone(&self.tokenizer)
+ }
+
pub fn count_tokens(&self, text: &str) -> Result<usize, Error> {
- self.0
+ self.tokenizer
.encode_fast(text, true)
.map(|encoding| encoding.len())
.map_err(Error::Encode)
}
+
+ pub fn encode(&self, text: &str) -> Result<Vec<u32>, Error> {
+ self.tokenizer
+ .encode_fast(text, true)
+ .map(|encoding| encoding.get_ids().to_vec())
+ .map_err(Error::Encode)
+ }
+
+ pub fn encode_result<'a>(
+ &self,
+ input: EncodeInput<'a>,
+ add_special_tokens: bool,
+ fast: bool,
+ ) -> Result<Encoding, Error> {
+ if fast {
+ return self
+ .tokenizer
+ .encode_fast(input, add_special_tokens)
+ .map_err(Error::Encode);
+ }
+ self.tokenizer
+ .encode_char_offsets(input, add_special_tokens)
+ .map_err(Error::Encode)
+ }
+
+ pub fn encode_batch_result<'a>(
+ &self,
+ inputs: Vec<EncodeInput<'a>>,
+ add_special_tokens: bool,
+ fast: bool,
+ ) -> Result<Vec<Encoding>, Error> {
+ if fast {
+ return self
+ .tokenizer
+ .encode_batch_fast(inputs, add_special_tokens)
+ .map_err(Error::Encode);
+ }
+ self.tokenizer
+ .encode_batch_char_offsets(inputs, add_special_tokens)
+ .map_err(Error::Encode)
+ }
+
+ pub fn to_json(&self, pretty: bool) -> Result<String, Error> {
+ self.tokenizer.to_string(pretty).map_err(Error::Load)
+ }
+
+ pub fn token_to_id(&self, token: &str) -> Option<u32> {
+ self.tokenizer.token_to_id(token)
+ }
+
+ pub fn id_to_token(&self, id: u32) -> Option<String> {
+ self.tokenizer.id_to_token(id)
+ }
+
+ pub fn vocab(&self, with_added_tokens: bool) -> HashMap<String, u32> {
+ self.tokenizer.get_vocab(with_added_tokens)
+ }
+
+ pub fn vocab_size(&self, with_added_tokens: bool) -> usize {
+ self.tokenizer.get_vocab_size(with_added_tokens)
+ }
+
+ /// The added tokens by id, in id order.
+ pub fn added_tokens_decoder(&self) -> Vec<(u32, AddedToken)> {
+ let mut added: Vec<(u32, AddedToken)> = self
+ .tokenizer
+ .get_added_tokens_decoder()
+ .into_iter()
+ .collect();
+ added.sort_unstable_by_key(|(id, _)| *id);
+ added
+ }
+
+ pub fn padding(&self) -> Option<&PaddingParams> {
+ self.tokenizer.get_padding()
+ }
+
+ pub fn truncation(&self) -> Option<&TruncationParams> {
+ self.tokenizer.get_truncation()
+ }
+
+ /// How many special tokens the post-processor adds to a single sequence or a pair.
+ pub fn num_special_tokens_to_add(&self, is_pair: bool) -> usize {
+ self.tokenizer
+ .get_post_processor()
+ .map_or(0, |processor| processor.added_tokens(is_pair))
+ }
+
+ pub fn encode_special_tokens(&self) -> bool {
+ self.tokenizer.get_encode_special_tokens()
+ }
+
+ pub fn decode(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String, Error> {
+ if !skip_special_tokens {
+ return self.tokenizer.decode(ids, false).map_err(Error::Decode);
+ }
+ let filtered_ids: Vec<u32> = ids
+ .iter()
+ .copied()
+ .filter(|id| !self.special_token_ids.contains(id))
+ .collect();
+ self.tokenizer
+ .decode(&filtered_ids, true)
+ .map_err(Error::Decode)
+ }
+
+ pub fn name(&self) -> &str {
+ "huggingface"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn codecs_round_trip_and_skip_special_tokens() {
+ let json = include_str!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../../../litellm/litellm_core_utils/tokenizers/anthropic_tokenizer.json"
+ ));
+ let tokenizer = HuggingFaceTokenizer::from_json(json).unwrap();
+ let ids = tokenizer.encode("<SOS>hello<EOT>").unwrap();
+
+ assert!(tokenizer.decode(&ids, false).unwrap().contains("<SOS>"));
+ assert_eq!(tokenizer.decode(&ids, true).unwrap(), "hello");
+ }
+
+ #[test]
+ fn decode_filters_special_added_tokens() {
+ let json = r#"{
+ "version": "1.0",
+ "truncation": null,
+ "padding": null,
+ "added_tokens": [
+ {
+ "id": 1,
+ "content": "<s>",
+ "single_word": false,
+ "lstrip": false,
+ "rstrip": false,
+ "normalized": false,
+ "special": true
+ }
+ ],
+ "normalizer": null,
+ "pre_tokenizer": {"type": "Whitespace"},
+ "post_processor": null,
+ "decoder": null,
+ "model": {
+ "type": "WordLevel",
+ "vocab": {"<unk>": 0, "<s>": 1, "hello": 2},
+ "unk_token": "<unk>"
+ }
+ }"#;
+ let tokenizer = HuggingFaceTokenizer::from_json(json).unwrap();
+
+ assert!(!tokenizer.decode(&[1, 2], true).unwrap().contains("<s>"));
+ assert!(tokenizer.decode(&[1, 2], false).unwrap().contains("<s>"));
+ }
+
+ #[test]
+ fn vocabulary_lookups_mirror_the_tokenizers_api() {
+ let json = include_str!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../../../litellm/litellm_core_utils/tokenizers/anthropic_tokenizer.json"
+ ));
+ let tokenizer = HuggingFaceTokenizer::from_json(json).unwrap();
+ let ids = tokenizer.encode("hello").unwrap();
+
+ let token = tokenizer.id_to_token(ids[0]).unwrap();
+ assert_eq!(tokenizer.token_to_id(&token), Some(ids[0]));
+ assert_eq!(tokenizer.id_to_token(u32::MAX), None);
+ assert_eq!(tokenizer.vocab(true).len(), tokenizer.vocab_size(true));
+ assert!(tokenizer.vocab_size(true) >= tokenizer.vocab_size(false));
+ let added = tokenizer.added_tokens_decoder();
+ assert!(added.windows(2).all(|pair| pair[0].0 < pair[1].0));
+ assert!(added.iter().any(|(_, token)| token.special));
+ assert!(tokenizer.padding().is_none());
+ assert!(tokenizer.truncation().is_none());
+ assert!(!tokenizer.encode_special_tokens());
+ assert_eq!(
+ tokenizer.num_special_tokens_to_add(false),
+ tokenizer.encode("").unwrap().len()
+ );
+ }
}
diff --git a/litellm-rust/crates/token-counter-tiktoken/Cargo.toml b/litellm-rust/crates/token-counter-tiktoken/Cargo.toml
index 494a9233e69..2fb3103e0c8 100644
--- a/litellm-rust/crates/token-counter-tiktoken/Cargo.toml
+++ b/litellm-rust/crates/token-counter-tiktoken/Cargo.toml
@@ -6,5 +6,8 @@ license.workspace = true
repository.workspace = true
[dependencies]
+base64.workspace = true
+once_cell = "1.21.3"
+rustc-hash = "2.1.3"
thiserror.workspace = true
tiktoken-rs.workspace = true
diff --git a/litellm-rust/crates/token-counter-tiktoken/src/lib.rs b/litellm-rust/crates/token-counter-tiktoken/src/lib.rs
index ecdb3946eee..f049e90a1cb 100644
--- a/litellm-rust/crates/token-counter-tiktoken/src/lib.rs
+++ b/litellm-rust/crates/token-counter-tiktoken/src/lib.rs
@@ -1,27 +1,125 @@
#![forbid(unsafe_code)]
mod error;
+mod ranks;
+
+use std::collections::HashSet;
pub use error::UnsupportedTokenizer;
+pub use ranks::{LoadError, Vocabulary};
-pub struct TiktokenTokenizer(&'static tiktoken_rs::CoreBPE);
+pub struct TiktokenTokenizer {
+ encoder: &'static tiktoken_rs::CoreBPE,
+ /// Present for encodings built from a rank file; the embedded tiktoken-rs singletons
+ /// behind [`from_name`](Self::from_name) keep their ranks private.
+ vocabulary: Option<&'static Vocabulary>,
+ name: &'static str,
+}
impl TiktokenTokenizer {
+ /// Builds `name` from its packaged rank file (read through `load`), once per process.
+ /// The tokenizer reports the requested name, so `gpt2` stays `gpt2` like tiktoken does.
+ pub fn from_cached_ranks(
+ name: &str,
+ load: impl FnOnce(&str) -> std::io::Result<String>,
+ ) -> Result<Self, LoadError> {
+ let (loaded, name) = ranks::load(name, load)?;
+ Ok(Self {
+ encoder: &loaded.bpe,
+ vocabulary: Some(&loaded.vocabulary),
+ name,
+ })
+ }
+
+ /// The encodings tiktoken-rs embeds, for hosts without the packaged rank files.
pub fn from_name(name: &str) -> Result<Self, UnsupportedTokenizer> {
- let tokenizer = match name {
- "cl100k_base" => tiktoken_rs::cl100k_base_singleton(),
- "o200k_base" => tiktoken_rs::o200k_base_singleton(),
- "o200k_harmony" => tiktoken_rs::o200k_harmony_singleton(),
- "p50k_base" => tiktoken_rs::p50k_base_singleton(),
- "p50k_edit" => tiktoken_rs::p50k_edit_singleton(),
- "r50k_base" | "gpt2" => tiktoken_rs::r50k_base_singleton(),
+ let (encoder, name) = match name {
+ "cl100k_base" => (tiktoken_rs::cl100k_base_singleton(), "cl100k_base"),
+ "o200k_base" => (tiktoken_rs::o200k_base_singleton(), "o200k_base"),
+ "o200k_harmony" => (tiktoken_rs::o200k_harmony_singleton(), "o200k_harmony"),
+ "p50k_base" => (tiktoken_rs::p50k_base_singleton(), "p50k_base"),
+ "p50k_edit" => (tiktoken_rs::p50k_edit_singleton(), "p50k_edit"),
+ "r50k_base" => (tiktoken_rs::r50k_base_singleton(), "r50k_base"),
+ "gpt2" => (tiktoken_rs::r50k_base_singleton(), "gpt2"),
_ => return Err(UnsupportedTokenizer(name.to_owned())),
};
- Ok(Self(tokenizer))
+ Ok(Self {
+ encoder,
+ vocabulary: None,
+ name,
+ })
+ }
+
+ pub fn vocabulary(&self) -> Option<&Vocabulary> {
+ self.vocabulary
}
pub fn count_tokens(&self, text: &str) -> usize {
- self.0.count_ordinary(text)
+ self.encoder.count_ordinary(text)
+ }
+
+ pub fn encode(&self, text: &str) -> Vec<u32> {
+ self.encoder.encode_ordinary(text)
+ }
+
+ pub fn encode_special(&self, text: &str, allowed: &[String]) -> Result<Vec<u32>, String> {
+ let allowed = allowed.iter().map(String::as_str).collect();
+ self.encoder
+ .encode(text, &allowed)
+ .map(|(ids, _)| ids)
+ .map_err(|error| error.to_string())
+ }
+
+ pub fn special_tokens(&self) -> HashSet<String> {
+ self.encoder
+ .special_tokens()
+ .into_iter()
+ .map(str::to_owned)
+ .collect()
+ }
+
+ /// tiktoken's `encode_with_unstable`: the stable prefix of `text`'s tokens and every
+ /// token sequence the unstable tail could still become, sorted for a stable order.
+ pub fn encode_with_unstable(
+ &self,
+ text: &str,
+ allowed: &[String],
+ ) -> (Vec<u32>, Vec<Vec<u32>>) {
+ let allowed = allowed.iter().map(String::as_str).collect();
+ let (stable, completions) = self.encoder._encode_unstable_native(text, &allowed);
+ let mut completions: Vec<Vec<u32>> = completions.into_iter().collect();
+ completions.sort_unstable();
+ (stable, completions)
+ }
+
+ pub fn decode_bytes(&self, ids: &[u32]) -> Result<Vec<u8>, String> {
+ self.encoder
+ .decode_bytes(ids)
+ .map_err(|error| error.to_string())
+ }
+
+ pub fn decode(&self, ids: &[u32]) -> Result<String, String> {
+ self.encoder
+ .decode_bytes(ids)
+ .map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
+ .map_err(|error| error.to_string())
+ }
+
+ pub fn name(&self) -> &str {
+ self.name
+ }
+}
+
+pub fn encoding_for_model(model: &str) -> Option<&'static str> {
+ match tiktoken_rs::tokenizer::get_tokenizer(model)? {
+ tiktoken_rs::tokenizer::Tokenizer::Cl100kBase => Some("cl100k_base"),
+ tiktoken_rs::tokenizer::Tokenizer::O200kBase => Some("o200k_base"),
+ tiktoken_rs::tokenizer::Tokenizer::O200kHarmony => Some("o200k_harmony"),
+ tiktoken_rs::tokenizer::Tokenizer::P50kBase => Some("p50k_base"),
+ tiktoken_rs::tokenizer::Tokenizer::P50kEdit => Some("p50k_edit"),
+ tiktoken_rs::tokenizer::Tokenizer::R50kBase | tiktoken_rs::tokenizer::Tokenizer::Gpt2 => {
+ Some("r50k_base")
+ }
}
}
@@ -66,5 +164,75 @@ mod tests {
panic!("unknown encoding must be rejected");
};
assert_eq!(name, "unknown-encoding");
+ assert_eq!(TiktokenTokenizer::from_name("gpt2").unwrap().name(), "gpt2");
+ }
+
+ #[test]
+ fn codecs_round_trip_named_encodings() {
+ let encodings = [
+ "cl100k_base",
+ "o200k_base",
+ "o200k_harmony",
+ "p50k_base",
+ "p50k_edit",
+ "r50k_base",
+ "gpt2",
+ ];
+ let texts = ["hello world", "café 漢字 مرحبا 🙂", "line one\nline two"];
+ for name in encodings {
+ let tokenizer = TiktokenTokenizer::from_name(name).unwrap();
+ for text in texts {
+ assert_eq!(
+ tokenizer.decode(&tokenizer.encode(text)).unwrap(),
+ text,
+ "{name}: {text:?}",
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn decoding_token_prefixes_replaces_incomplete_utf8() {
+ let tokenizer = TiktokenTokenizer::from_name("cl100k_base").unwrap();
+ let reference = tiktoken_rs::cl100k_base_singleton();
+ let ids = tokenizer.encode("🙂漢字");
+ for end in 1..=ids.len() {
+ let bytes = reference.decode_bytes(&ids[..end]).unwrap();
+ assert_eq!(
+ tokenizer.decode(&ids[..end]).unwrap(),
+ String::from_utf8_lossy(&bytes),
+ );
+ }
+ assert!(tokenizer.decode(&[u32::MAX]).is_err());
+ }
+
+ #[test]
+ fn unstable_encoding_prefixes_stay_consistent_with_full_encoding() {
+ let tokenizer = TiktokenTokenizer::from_name("cl100k_base").unwrap();
+ let text = "hello fanta";
+ let (stable, completions) = tokenizer.encode_with_unstable(text, &[]);
+ assert!(
+ text.as_bytes()
+ .starts_with(&tokenizer.decode_bytes(&stable).unwrap())
+ );
+ assert!(!completions.is_empty());
+ for completion in &completions {
+ let mut ids = stable.clone();
+ ids.extend(completion);
+ assert!(
+ tokenizer
+ .decode_bytes(&ids)
+ .unwrap()
+ .starts_with(text.as_bytes())
+ );
+ }
+ assert!(completions.windows(2).all(|pair| pair[0] < pair[1]));
+ }
+
+ #[test]
+ fn encoding_for_model_maps_known_models() {
+ assert_eq!(encoding_for_model("gpt-4o"), Some("o200k_base"));
+ assert_eq!(encoding_for_model("text-davinci-003"), Some("p50k_base"));
+ assert_eq!(encoding_for_model("unknown-model"), None);
}
}
diff --git a/litellm-rust/crates/token-counter-tiktoken/src/ranks.rs b/litellm-rust/crates/token-counter-tiktoken/src/ranks.rs
new file mode 100644
index 00000000000..1f5e5262de5
--- /dev/null
+++ b/litellm-rust/crates/token-counter-tiktoken/src/ranks.rs
@@ -0,0 +1,340 @@
+use base64::{Engine, engine::general_purpose::STANDARD};
+use once_cell::sync::OnceCell;
+use rustc_hash::FxHashMap;
+use thiserror::Error;
+use tiktoken_rs::{CoreBPE, O200K_BASE_PAT_STR, Rank};
+
+use crate::UnsupportedTokenizer;
+
+const CL100K: &str = "9b5ad71b2ce5302211f9c61530b329a4922fc6a4";
+const O200K: &str = "fb374d419588a4632f3f557e76b4b70aebbca790";
+const P50K: &str = "ec7223a39ce59f226a68acc30dc1af2788490e15";
+const LEGACY_PATTERN: &str =
+ r"'(?:[sdmt]|ll|ve|re)| ?\p{L}++| ?\p{N}++| ?[^\s\p{L}\p{N}]++|\s++$|\s+(?!\S)|\s";
+const CL100K_PATTERN: &str = r"'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}++|\p{N}{1,3}+| ?[^\s\p{L}\p{N}]++[\r\n]*+|\s++$|\s*[\r\n]|\s+(?!\S)|\s";
+
+static CL100K_ENCODER: OnceCell<Loaded> = OnceCell::new();
+static O200K_ENCODER: OnceCell<Loaded> = OnceCell::new();
+static HARMONY_ENCODER: OnceCell<Loaded> = OnceCell::new();
+static P50K_ENCODER: OnceCell<Loaded> = OnceCell::new();
+static EDIT_ENCODER: OnceCell<Loaded> = OnceCell::new();
+static R50K_ENCODER: OnceCell<Loaded> = OnceCell::new();
+
+/// One encoding built from a rank file: the BPE engine plus the vocabulary it was built
+/// from, kept because `CoreBPE` does not expose its ranks and tiktoken's Python API does
+/// (`token_byte_values`, `encode_single_token`, `max_token_value`, `_special_tokens`).
+pub(super) struct Loaded {
+ pub(super) bpe: CoreBPE,
+ pub(super) vocabulary: Vocabulary,
+}
+
+/// The byte-level vocabulary of a tiktoken encoding.
+pub struct Vocabulary {
+ ranks: FxHashMap<Vec<u8>, Rank>,
+ special_tokens: FxHashMap<String, Rank>,
+ max_token_value: Rank,
+}
+
+impl Vocabulary {
+ /// Every mergeable token's bytes, sorted bytewise like tiktoken's `token_byte_values`.
+ pub fn token_byte_values(&self) -> Vec<Vec<u8>> {
+ let mut values: Vec<Vec<u8>> = self.ranks.keys().cloned().collect();
+ values.sort_unstable();
+ values
+ }
+
+ /// The rank of one whole token: a mergeable piece first, then a special token's text.
+ pub fn encode_single_token(&self, piece: &[u8]) -> Option<Rank> {
+ if let Some(rank) = self.ranks.get(piece) {
+ return Some(*rank);
+ }
+ std::str::from_utf8(piece)
+ .ok()
+ .and_then(|text| self.special_tokens.get(text).copied())
+ }
+
+ pub fn max_token_value(&self) -> Rank {
+ self.max_token_value
+ }
+
+ /// Every mergeable token with its rank, for building other tables from one parse.
+ pub fn ranks(&self) -> impl Iterator<Item = (&[u8], Rank)> + '_ {
+ self.ranks
+ .iter()
+ .map(|(bytes, rank)| (bytes.as_slice(), *rank))
+ }
+
+ /// The special tokens with their ranks, tiktoken's `_special_tokens`.
+ pub fn special_tokens(&self) -> impl Iterator<Item = (&str, Rank)> + '_ {
+ self.special_tokens
+ .iter()
+ .map(|(token, rank)| (token.as_str(), *rank))
+ }
+
+ pub fn is_special_token(&self, rank: Rank) -> bool {
+ self.special_tokens.values().any(|special| *special == rank)
+ }
+}
+
+#[derive(Debug, Error)]
+pub enum LoadError {
+ #[error(transparent)]
+ Unsupported(#[from] UnsupportedTokenizer),
+ #[error("failed to load tiktoken ranks: {0}")]
+ Ranks(String),
+}
+
+/// Loads `name` once per process. The returned name is the one requested (`gpt2` stays
+/// `gpt2`, as `tiktoken.get_encoding("gpt2").name` does), while `gpt2` and `r50k_base` share
+/// one cached encoder.
+pub(super) fn load(
+ name: &str,
+ load_file: impl FnOnce(&str) -> std::io::Result<String>,
+) -> Result<(&'static Loaded, &'static str), LoadError> {
+ let (requested, canonical, file, cache) = match name {
+ "cl100k_base" => ("cl100k_base", "cl100k_base", CL100K, &CL100K_ENCODER),
+ "o200k_base" => ("o200k_base", "o200k_base", O200K, &O200K_ENCODER),
+ "o200k_harmony" => ("o200k_harmony", "o200k_harmony", O200K, &HARMONY_ENCODER),
+ "p50k_base" => ("p50k_base", "p50k_base", P50K, &P50K_ENCODER),
+ "p50k_edit" => ("p50k_edit", "p50k_edit", P50K, &EDIT_ENCODER),
+ "r50k_base" => ("r50k_base", "r50k_base", P50K, &R50K_ENCODER),
+ "gpt2" => ("gpt2", "r50k_base", P50K, &R50K_ENCODER),
+ _ => return Err(UnsupportedTokenizer(name.to_owned()).into()),
+ };
+ let loaded = cache.get_or_try_init(|| {
+ let ranks = load_file(file).map_err(|error| LoadError::Ranks(error.to_string()))?;
+ build(canonical, &ranks)
+ })?;
+ Ok((loaded, requested))
+}
+
+fn build(name: &str, ranks: &str) -> Result<Loaded, LoadError> {
+ let parsed = ranks
+ .lines()
+ .map(parse_rank)
+ .collect::<Result<Vec<_>, _>>()?;
+ let encoder: FxHashMap<_, _> = parsed
+ .into_iter()
+ .filter(|(_, rank)| name != "r50k_base" || *rank < 50256)
+ .collect();
+ if encoder
+ .values()
+ .collect::<std::collections::HashSet<_>>()
+ .len()
+ != encoder.len()
+ || (0..=u8::MAX).any(|byte| !encoder.contains_key(&[byte][..]))
+ {
+ return Err(LoadError::Ranks("invalid vocabulary ranks".into()));
+ }
+ let (pattern, specials): (&str, &[(&str, Rank)]) = match name {
+ "cl100k_base" => (
+ CL100K_PATTERN,
+ &[
+ ("<|endoftext|>", 100257),
+ ("<|fim_prefix|>", 100258),
+ ("<|fim_middle|>", 100259),
+ ("<|fim_suffix|>", 100260),
+ ("<|endofprompt|>", 100276),
+ ],
+ ),
+ "o200k_base" => (
+ O200K_BASE_PAT_STR,
+ &[("<|endoftext|>", 199999), ("<|endofprompt|>", 200018)],
+ ),
+ "o200k_harmony" => (
+ O200K_BASE_PAT_STR,
+ &[
+ ("<|startoftext|>", 199998),
+ ("<|endoftext|>", 199999),
+ ("<|reserved_200000|>", 200000),
+ ("<|reserved_200001|>", 200001),
+ ("<|return|>", 200002),
+ ("<|constrain|>", 200003),
+ ("<|reserved_200004|>", 200004),
+ ("<|channel|>", 200005),
+ ("<|start|>", 200006),
+ ("<|end|>", 200007),
+ ("<|message|>", 200008),
+ ("<|reserved_200009|>", 200009),
+ ("<|reserved_200010|>", 200010),
+ ("<|reserved_200011|>", 200011),
+ ("<|call|>", 200012),
+ ],
+ ),
+ "p50k_edit" => (
+ LEGACY_PATTERN,
+ &[
+ ("<|endoftext|>", 50256),
+ ("<|fim_prefix|>", 50281),
+ ("<|fim_middle|>", 50282),
+ ("<|fim_suffix|>", 50283),
+ ],
+ ),
+ _ => (LEGACY_PATTERN, &[("<|endoftext|>", 50256)]),
+ };
+ let reserved = (200013..=201087)
+ .filter(|_| name == "o200k_harmony")
+ .map(|rank| (format!("<|reserved_{rank}|>"), rank));
+ let special_tokens: FxHashMap<String, Rank> = specials
+ .iter()
+ .map(|(token, rank)| ((*token).to_owned(), *rank))
+ .chain(reserved)
+ .collect();
+ let max_token_value = encoder
+ .values()
+ .chain(special_tokens.values())
+ .copied()
+ .max()
+ .ok_or_else(|| LoadError::Ranks("empty vocabulary".into()))?;
+ let bpe = CoreBPE::new(encoder.clone(), special_tokens.clone(), pattern)
+ .map_err(|error| LoadError::Ranks(error.to_string()))?;
+ Ok(Loaded {
+ bpe,
+ vocabulary: Vocabulary {
+ ranks: encoder,
+ special_tokens,
+ max_token_value,
+ },
+ })
+}
+
+fn parse_rank(line: &str) -> Result<(Vec<u8>, Rank), LoadError> {
+ let (token, rank) = line
+ .split_once(' ')
+ .ok_or_else(|| LoadError::Ranks("missing rank".into()))?;
+ let bytes = STANDARD
+ .decode(token)
+ .map_err(|error| LoadError::Ranks(error.to_string()))?;
+ let rank = rank
+ .parse()
+ .map_err(|error: std::num::ParseIntError| LoadError::Ranks(error.to_string()))?;
+ Ok((bytes, rank))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::TiktokenTokenizer;
+
+ fn read_packaged_ranks(file: &str) -> std::io::Result<String> {
+ std::fs::read_to_string(
+ std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
+ .join("../../../litellm/litellm_core_utils/tokenizers")
+ .join(file),
+ )
+ }
+
+ #[test]
+ fn packaged_encodings_match_embedded_encodings_and_reuse_successful_loads() {
+ for name in [
+ "cl100k_base",
+ "o200k_base",
+ "o200k_harmony",
+ "p50k_base",
+ "p50k_edit",
+ "r50k_base",
+ "gpt2",
+ ] {
+ if name != "gpt2" {
+ assert!(
+ TiktokenTokenizer::from_cached_ranks(name, |_| {
+ Err(std::io::Error::other("unreadable vocabulary"))
+ })
+ .is_err()
+ );
+ }
+ let loads = std::sync::atomic::AtomicUsize::new(0);
+ let barrier = std::sync::Barrier::new(4);
+ let encoders = std::thread::scope(|scope| {
+ let tasks: Vec<_> = (0..4)
+ .map(|_| {
+ scope.spawn(|| {
+ barrier.wait();
+ TiktokenTokenizer::from_cached_ranks(name, |file| {
+ loads.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ read_packaged_ranks(file)
+ })
+ .unwrap()
+ })
+ })
+ .collect();
+ tasks
+ .into_iter()
+ .map(|task| task.join().unwrap())
+ .collect::<Vec<_>>()
+ });
+ assert_eq!(loads.into_inner(), usize::from(name != "gpt2"));
+ let actual = &encoders[0];
+ let expected = TiktokenTokenizer::from_name(name).unwrap();
+ assert_eq!(actual.special_tokens(), expected.special_tokens());
+ let specials: Vec<_> = expected.special_tokens().into_iter().collect();
+ let special_text = specials.join(" ");
+ assert_eq!(
+ actual.encode_special(&special_text, &specials).unwrap(),
+ expected.encode_special(&special_text, &specials).unwrap()
+ );
+ for text in [
+ "",
+ "café 漢字 ع 🙂",
+ "a\r\nb\t ",
+ " hello 123456789",
+ &special_text,
+ ] {
+ let ids = expected.encode(text);
+ assert_eq!(actual.encode(text), ids, "{name}: {text:?}");
+ assert_eq!(actual.count_tokens(text), ids.len(), "{name}: {text:?}");
+ assert_eq!(
+ actual.decode_bytes(&ids).unwrap(),
+ expected.decode_bytes(&ids).unwrap()
+ );
+ }
+ let cached = TiktokenTokenizer::from_cached_ranks(name, |_| {
+ panic!("reloaded cached vocabulary")
+ })
+ .unwrap();
+ assert_eq!(cached.encode("cached"), expected.encode("cached"));
+ assert_eq!(cached.name(), name);
+ assert!(expected.vocabulary().is_none());
+ assert_vocabulary_lookups(name, actual);
+ }
+ }
+
+ /// The token-level lookups tiktoken's Python `Encoding` exposes, checked against the
+ /// encoder itself and against the known vocabulary sizes.
+ fn assert_vocabulary_lookups(name: &str, tokenizer: &TiktokenTokenizer) {
+ let max_token_value = match name {
+ "cl100k_base" => 100_276,
+ "o200k_base" => 200_018,
+ "o200k_harmony" => 201_087,
+ "p50k_base" => 50_280,
+ "p50k_edit" => 50_283,
+ "r50k_base" | "gpt2" => 50_256,
+ _ => unreachable!("{name}"),
+ };
+ let vocabulary = tokenizer.vocabulary().unwrap();
+ assert_eq!(vocabulary.max_token_value(), max_token_value, "{name}");
+ let values = vocabulary.token_byte_values();
+ assert!(values.windows(2).all(|pair| pair[0] < pair[1]), "{name}");
+ for piece in values.iter().step_by(997) {
+ let rank = vocabulary.encode_single_token(piece).unwrap();
+ assert_eq!(tokenizer.decode_bytes(&[rank]).unwrap(), *piece, "{name}");
+ assert!(!vocabulary.is_special_token(rank), "{name}");
+ }
+ for (token, rank) in vocabulary.special_tokens() {
+ assert_eq!(vocabulary.encode_single_token(token.as_bytes()), Some(rank));
+ assert!(vocabulary.is_special_token(rank), "{name}: {token}");
+ }
+ assert_eq!(vocabulary.encode_single_token(b"<|not-a-token|>"), None);
+ }
+
+ #[test]
+ fn malformed_ranks_return_errors_instead_of_panicking() {
+ for ranks in ["", "IQ==", "IQ== x", "!!! 1", "IQ== 1"] {
+ assert!(build("cl100k_base", ranks).is_err());
+ }
+ let repeated_rank = (0..=u8::MAX)
+ .map(|byte| format!("{} 0\n", STANDARD.encode([byte])))
+ .collect::<String>();
+ assert!(build("cl100k_base", &repeated_rank).is_err());
+ }
+}
diff --git a/litellm-rust/crates/token-counter/README.md b/litellm-rust/crates/token-counter/README.md
index a6b2b50aac0..3c6381fcea7 100644
--- a/litellm-rust/crates/token-counter/README.md
+++ b/litellm-rust/crates/token-counter/README.md
@@ -1,6 +1,10 @@
# Token counting
-`Tokenizer` is the text-counting interface. `TokenCounter` applies LiteLLM request, message, and tool accounting using any implementation of that interface
+`Tokenizer` is the text-counting interface. `TextCodec` adds encoding, decoding, and a name. `TokenCounter` applies LiteLLM request, message, and tool accounting using any `Tokenizer`
+
+Counts follow the codec: tiktoken treats special-token spellings as ordinary text, while Hugging Face applies its added tokens, post-processing, padding, and truncation. `fast=True` preserves those semantics and requests acceleration where available. Unsupported configurations use the normal codec, including tiktoken encodings without a scanner and builds without the `fast` feature. Invalid input and process-guard errors still propagate. Runtime request counting currently uses the normal codec; the custom accelerator is retained for explicit use and testing
+
+`FastCounter: TextCodec` exposes an optional accelerator over a loaded codec. `None` means callers should use that codec. The Python bridge caches this selection per immutable tokenizer, shares it with request counters, and initializes it with the GIL released. Hugging Face can also choose the full encoder per input when added tokens require it
The `fast` feature provides `fast::FastTokenizer` from `litellm-token-counter-fast`. `TokenCounter::from_json_fast` uses this implementation
@@ -8,7 +12,9 @@ The `huggingface` feature provides `huggingface::HuggingFaceTokenizer` through t
The `tiktoken` feature provides `tiktoken::TiktokenTokenizer` through `tiktoken-rs`. Select an encoding with `TokenCounter::from_tiktoken`. The supported names are `cl100k_base`, `o200k_base`, `o200k_harmony`, `p50k_base`, `p50k_edit`, `r50k_base`, and `gpt2`
-All three backends are enabled by default. The Python extension builds with `fast` only, which keeps the wheel at the size it had before the split. With `default-features = false`, callers can supply their own `Tokenizer` to `TokenCounter::new` without compiling a built-in backend
+All three backends are enabled by default in this crate and the Python extension. With `default-features = false`, Rust callers can supply their own `Tokenizer` to `TokenCounter::new` without compiling a built-in backend
+
+Python `tiktoken` and `tokenizers` remain runtime dependencies and the default implementations. The catalog independently selects the tokenizer and request-counting routes. Enabling Rust changes factory dispatch; existing tokenizer objects keep their backend. Native Hugging Face wrappers provide an immutable encoding and decoding API, while training and mutable configuration remain available through the Python backend
Budget checks, cost calculation, and the `max_tokens` adjustment policy belong to `litellm-core-utils`. The counter does not own prices, budgets, or request limits
diff --git a/litellm-rust/crates/token-counter/src/error.rs b/litellm-rust/crates/token-counter/src/error.rs
index b05ce007e46..6a94b0e39b8 100644
--- a/litellm-rust/crates/token-counter/src/error.rs
+++ b/litellm-rust/crates/token-counter/src/error.rs
@@ -32,6 +32,8 @@ pub enum Error {
JsonUtf8(#[source] FromUtf8Error),
#[error("tokenization failed: {0}")]
Encode(#[source] Box<dyn std::error::Error + Send + Sync>),
+ #[error("token decoding failed: {0}")]
+ Decode(String),
#[error("token counting task failed: {0}")]
Task(String),
}
diff --git a/litellm-rust/crates/token-counter/src/fast.rs b/litellm-rust/crates/token-counter/src/fast.rs
index de3f86abd68..f601c3cbcca 100644
--- a/litellm-rust/crates/token-counter/src/fast.rs
+++ b/litellm-rust/crates/token-counter/src/fast.rs
@@ -1,7 +1,31 @@
use litellm_token_counter_fast::Error as BackendError;
pub use litellm_token_counter_fast::FastTokenizer;
-use crate::{Error, TokenCounter, Tokenizer};
+use crate::{Error, TextCodec, TokenCounter, Tokenizer};
+
+pub trait FastCounter: TextCodec {
+ fn fast_counter(&self) -> Option<FastTokenizer>;
+}
+
+#[cfg(feature = "huggingface")]
+impl FastCounter for crate::huggingface::HuggingFaceTokenizer {
+ fn fast_counter(&self) -> Option<FastTokenizer> {
+ Some(FastTokenizer::from_shared(self.shared()))
+ }
+}
+
+#[cfg(feature = "tiktoken")]
+impl FastCounter for crate::tiktoken::TiktokenTokenizer {
+ fn fast_counter(&self) -> Option<FastTokenizer> {
+ let vocabulary = self.vocabulary()?;
+ match self.name() {
+ "cl100k_base" => FastTokenizer::from_cl100k_pairs(vocabulary.ranks()),
+ "o200k_base" | "o200k_harmony" => FastTokenizer::from_o200k_pairs(vocabulary.ranks()),
+ _ => return None,
+ }
+ .ok()
+ }
+}
impl TokenCounter {
pub fn from_json_fast(tokenizer_json: &str) -> Result<Self, Error> {
@@ -39,3 +63,65 @@ impl From<BackendError> for Error {
}
}
}
+
+#[cfg(all(test, feature = "huggingface", feature = "tiktoken"))]
+mod tests {
+ use super::*;
+ use crate::huggingface::HuggingFaceTokenizer;
+ use crate::tiktoken::TiktokenTokenizer;
+
+ const TEXTS: [&str; 4] = [
+ "",
+ "hello world <|endoftext|>",
+ "café 漢字 ع 🙂 line\r\n indented 123456789",
+ "<SOS>system<EOT> a\u{301} fi",
+ ];
+
+ fn packaged(file: &str) -> String {
+ std::fs::read_to_string(format!(
+ "{}/../../../litellm/litellm_core_utils/tokenizers/{file}",
+ env!("CARGO_MANIFEST_DIR")
+ ))
+ .unwrap()
+ }
+
+ #[test]
+ fn fast_counters_derived_from_codecs_count_like_the_codecs() {
+ let huggingface =
+ HuggingFaceTokenizer::from_json(&packaged("anthropic_tokenizer.json")).unwrap();
+ let fast = huggingface.fast_counter().unwrap();
+ for text in TEXTS {
+ assert_eq!(
+ fast.count_tokens(text).unwrap(),
+ Tokenizer::count_tokens(&huggingface, text).unwrap(),
+ "{text:?}"
+ );
+ }
+
+ for name in ["cl100k_base", "o200k_base", "o200k_harmony"] {
+ let tiktoken =
+ TiktokenTokenizer::from_cached_ranks(name, |file| Ok(packaged(file))).unwrap();
+ let fast = tiktoken.fast_counter().unwrap();
+ for text in TEXTS {
+ assert_eq!(
+ fast.count_tokens(text).unwrap(),
+ tiktoken.count_tokens(text),
+ "{name}: {text:?}"
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn encodings_without_a_fast_scanner_keep_the_codec() {
+ let tiktoken =
+ TiktokenTokenizer::from_cached_ranks("p50k_base", |file| Ok(packaged(file))).unwrap();
+ assert!(tiktoken.fast_counter().is_none());
+ assert!(
+ TiktokenTokenizer::from_name("cl100k_base")
+ .unwrap()
+ .fast_counter()
+ .is_none()
+ );
+ }
+}
diff --git a/litellm-rust/crates/token-counter/src/huggingface.rs b/litellm-rust/crates/token-counter/src/huggingface.rs
index fb7683b373e..43613fac8ea 100644
--- a/litellm-rust/crates/token-counter/src/huggingface.rs
+++ b/litellm-rust/crates/token-counter/src/huggingface.rs
@@ -1,7 +1,11 @@
use litellm_token_counter_huggingface::Error as BackendError;
-pub use litellm_token_counter_huggingface::HuggingFaceTokenizer;
+pub use litellm_token_counter_huggingface::{
+ AddedToken, EncodeInput, Encoding, HuggingFaceTokenizer, InputSequence, PaddingDirection,
+ PaddingParams, PaddingStrategy, TruncationDirection, TruncationParams, encoding_from_json,
+ encoding_to_json,
+};
-use crate::{Error, TokenCounter, Tokenizer};
+use crate::{Error, TextCodec, TokenCounter, Tokenizer};
impl TokenCounter {
pub fn from_json(tokenizer_json: &str) -> Result<Self, Error> {
@@ -17,11 +21,26 @@ impl Tokenizer for HuggingFaceTokenizer {
}
}
+impl TextCodec for HuggingFaceTokenizer {
+ fn encode(&self, text: &str) -> Result<Vec<u32>, Error> {
+ HuggingFaceTokenizer::encode(self, text).map_err(Error::from)
+ }
+
+ fn decode(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String, Error> {
+ HuggingFaceTokenizer::decode(self, ids, skip_special_tokens).map_err(Error::from)
+ }
+
+ fn name(&self) -> &str {
+ HuggingFaceTokenizer::name(self)
+ }
+}
+
impl From<BackendError> for Error {
fn from(error: BackendError) -> Self {
match error {
BackendError::Load(source) => Self::Load(source),
BackendError::Encode(source) => Self::Encode(source),
+ BackendError::Decode(source) => Self::Decode(source.to_string()),
}
}
}
diff --git a/litellm-rust/crates/token-counter/src/lib.rs b/litellm-rust/crates/token-counter/src/lib.rs
index 446c91049de..84bf35ca2ba 100644
--- a/litellm-rust/crates/token-counter/src/lib.rs
+++ b/litellm-rust/crates/token-counter/src/lib.rs
@@ -20,5 +20,5 @@ pub mod tiktoken;
pub use counter::{InputTokenCount, TokenCounter};
pub use error::Error;
-pub use tokenizer::Tokenizer;
+pub use tokenizer::{TextCodec, Tokenizer};
pub use types::CountableRequest;
diff --git a/litellm-rust/crates/token-counter/src/tiktoken.rs b/litellm-rust/crates/token-counter/src/tiktoken.rs
index 07c1c9f5b73..5883a629ea5 100644
--- a/litellm-rust/crates/token-counter/src/tiktoken.rs
+++ b/litellm-rust/crates/token-counter/src/tiktoken.rs
@@ -1,7 +1,7 @@
-pub use litellm_token_counter_tiktoken::TiktokenTokenizer;
-use litellm_token_counter_tiktoken::UnsupportedTokenizer;
+use litellm_token_counter_tiktoken::{LoadError, UnsupportedTokenizer};
+pub use litellm_token_counter_tiktoken::{TiktokenTokenizer, Vocabulary, encoding_for_model};
-use crate::{Error, TokenCounter, Tokenizer};
+use crate::{Error, TextCodec, TokenCounter, Tokenizer};
impl TokenCounter {
pub fn from_tiktoken(encoding: &str) -> Result<Self, Error> {
@@ -17,8 +17,31 @@ impl Tokenizer for TiktokenTokenizer {
}
}
+impl TextCodec for TiktokenTokenizer {
+ fn encode(&self, text: &str) -> Result<Vec<u32>, Error> {
+ Ok(TiktokenTokenizer::encode(self, text))
+ }
+
+ fn decode(&self, ids: &[u32], _skip_special_tokens: bool) -> Result<String, Error> {
+ TiktokenTokenizer::decode(self, ids).map_err(|error| Error::Decode(error.to_string()))
+ }
+
+ fn name(&self) -> &str {
+ TiktokenTokenizer::name(self)
+ }
+}
+
impl From<UnsupportedTokenizer> for Error {
fn from(error: UnsupportedTokenizer) -> Self {
Self::UnsupportedTokenizer(error.0)
}
}
+
+impl From<LoadError> for Error {
+ fn from(error: LoadError) -> Self {
+ match error {
+ LoadError::Unsupported(error) => error.into(),
+ LoadError::Ranks(message) => Self::Ranks(message),
+ }
+ }
+}
diff --git a/litellm-rust/crates/token-counter/src/tokenizer.rs b/litellm-rust/crates/token-counter/src/tokenizer.rs
index 88c29c672a7..146ac5b4d0d 100644
--- a/litellm-rust/crates/token-counter/src/tokenizer.rs
+++ b/litellm-rust/crates/token-counter/src/tokenizer.rs
@@ -4,6 +4,12 @@ pub trait Tokenizer: Send + Sync {
fn count_tokens(&self, text: &str) -> Result<usize, Error>;
}
+pub trait TextCodec: Tokenizer {
+ fn encode(&self, text: &str) -> Result<Vec<u32>, Error>;
+ fn decode(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String, Error>;
+ fn name(&self) -> &str;
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/litellm/_lazy_imports.py b/litellm/_lazy_imports.py
index fe3c7c264ee..29fb46fa125 100644
--- a/litellm/_lazy_imports.py
+++ b/litellm/_lazy_imports.py
@@ -58,7 +58,8 @@ from ._lazy_imports_registry import (
if TYPE_CHECKING:
import httpx
- from tiktoken import Encoding
+
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
def get_litellm_globals() -> dict[str, object]:
@@ -89,26 +90,11 @@ def _get_module_level_client_timeout(litellm_globals: Mapping[str, Any]) -> "flo
# These are special lazy loaders for things that are used internally
# They're separate from the main lazy import system because they have specific use cases
-# Lazy loader for default encoding - avoids importing heavy tiktoken library at startup
-_default_encoding: "Encoding | None" = None
-
-
-def _get_default_encoding() -> "Encoding":
- """
- Lazily load and cache the default OpenAI encoding.
- This avoids importing `litellm.litellm_core_utils.default_encoding` (and thus tiktoken)
- at `litellm` import time. The encoding is cached after the first import.
-
- This is used internally by utils.py functions that need the encoding but shouldn't
- trigger its import during module load.
- """
- global _default_encoding
- if _default_encoding is None:
- from litellm.litellm_core_utils.default_encoding import encoding
+def _get_default_encoding() -> "Tokenizer":
+ from litellm.rust_bridge.tokenizer import get_encoding
- _default_encoding = encoding
- return _default_encoding
+ return get_encoding("cl100k_base")
# Lazy loader for get_modified_max_tokens to avoid importing token_counter at module import time
diff --git a/litellm/litellm_core_utils/README.md b/litellm/litellm_core_utils/README.md
index b61c8982762..a5f5e8326b9 100644
--- a/litellm/litellm_core_utils/README.md
+++ b/litellm/litellm_core_utils/README.md
@@ -6,8 +6,9 @@ Core files:
- `streaming_handler.py`: The core streaming logic + streaming related helper utils
- `core_helpers.py`: code used in `types/` - e.g. `map_finish_reason`.
- `exception_mapping_utils.py`: utils for mapping exceptions to openai-compatible error types.
-- `default_encoding.py`: code for loading the default encoding (tiktoken)
+- `default_encoding.py`: code for loading the default Python tokenizer and bundled cache
- `get_llm_provider_logic.py`: code for inferring the LLM provider from a given model name.
- `duration_parser.py`: code for parsing durations - e.g. "1d", "1mo", "10s"
- `api_route_to_call_types.py`: mapping of API routes to their corresponding CallTypes (e.g., `/chat/completions` -> [acompletion, completion])
+Tokenizer factories return Python tokenizer objects by default. Set `LITELLM_RUST=1` or call `litellm.rust(True)` before constructing tokenizers to select the Rust backend through `Route.TOKENIZER` in the Rust catalog. Missing native bindings or unsupported native features fall back to Python. Existing tokenizer objects keep their selected backend. Rust-backed tokenizer objects carry the read-only `tiktoken.Encoding` / `tokenizers.Tokenizer` surface and are immutable: `enable_padding`, `enable_truncation` and `add_tokens` stay on the Python tokenizer.
diff --git a/litellm/litellm_core_utils/default_encoding.py b/litellm/litellm_core_utils/default_encoding.py
index 71b30614d8d..c3b6a008411 100644
--- a/litellm/litellm_core_utils/default_encoding.py
+++ b/litellm/litellm_core_utils/default_encoding.py
@@ -1,5 +1,4 @@
import os
-from pathlib import Path
from typing import Final
import litellm
@@ -15,20 +14,6 @@ except (ImportError, AttributeError):
filename = pkg_resources.resource_filename(__name__, "litellm_core_utils/tokenizers")
-CL100K_BASE_RANK_FILE: Final = "9b5ad71b2ce5302211f9c61530b329a4922fc6a4"
-O200K_BASE_RANK_FILE: Final = "fb374d419588a4632f3f557e76b4b70aebbca790"
-
-
-def cl100k_base_rank_file() -> str:
- """The vendored tiktoken `cl100k_base` rank file (`base64(token) rank` lines)."""
- return Path(filename, CL100K_BASE_RANK_FILE).read_text(encoding="ascii")
-
-
-def o200k_base_rank_file() -> str:
- """The vendored tiktoken `o200k_base` rank file (`base64(token) rank` lines)."""
- return Path(filename, O200K_BASE_RANK_FILE).read_text(encoding="ascii")
-
-
# Always default TIKTOKEN_CACHE_DIR to the bundled tokenizers directory
# unless the user explicitly overrides it via CUSTOM_TIKTOKEN_CACHE_DIR.
# This keeps tiktoken fully offline-capable by default (see #1071).
diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py
index bf37b1be2e4..5d7956059e4 100644
--- a/litellm/litellm_core_utils/token_counter.py
+++ b/litellm/litellm_core_utils/token_counter.py
@@ -10,7 +10,6 @@ import anyio
import anyio.lowlevel
import httpx
import tiktoken
-from tokenizers import Tokenizer
from typing_extensions import ParamSpec, TypeVar
import litellm
@@ -30,8 +29,10 @@ from litellm.constants import (
TOKEN_COUNTER_MAX_EXACT_CHARS,
)
from litellm.litellm_core_utils.asyncify import asyncify
+from litellm.litellm_core_utils.tokenizer import Encoding, HuggingFace, HuggingFaceTokenizer, OpenAIEncoding
from litellm.litellm_core_utils.url_utils import safe_get
from litellm.llms.custom_httpx.http_handler import _get_httpx_client
+from litellm.rust_bridge.tokenizer import get_encoding
from litellm.types.llms.anthropic import (
AnthropicContentParamSource,
AnthropicContentParamSourceFileId,
@@ -622,9 +623,11 @@ def _get_exact_count_function(
if model is not None or custom_tokenizer is not None:
tokenizer_json: Final = custom_tokenizer or _select_tokenizer(model)
if tokenizer_json["type"] == "huggingface_tokenizer":
- tokenizer: Final[Tokenizer] = tokenizer_json["tokenizer"]
+ tokenizer: Final[HuggingFace] = tokenizer_json["tokenizer"]
def count_tokens(text: str) -> int:
+ if isinstance(tokenizer, HuggingFaceTokenizer):
+ return tokenizer.count(text)
return len(tokenizer.encode_batch_fast([text])[0])
return count_tokens
@@ -632,31 +635,43 @@ def _get_exact_count_function(
encoding: Final = openai_tokenizer_encoding(model)
def encode_length(text: str) -> int:
- return len(encoding.encode(text, disallowed_special=()))
+ return _encoding_count(encoding, text)
return _get_tiktoken_count_function(encode_length)
else:
raise ValueError("Unsupported tokenizer type")
else:
+ default_encoding: Final = _get_default_encoding()
def encode_length(text: str) -> int:
- return len(_get_default_encoding().encode(text, disallowed_special=()))
+ return _encoding_count(default_encoding, text)
return _get_tiktoken_count_function(encode_length)
-def openai_tokenizer_encoding(model: str) -> tiktoken.Encoding:
- """The tiktoken encoding `token_counter` uses for a model on the `openai_tokenizer` path."""
+def _encoding_count(encoding: Encoding, text: str) -> int:
+ if isinstance(encoding, OpenAIEncoding):
+ return encoding.count(text)
+ return len(encoding.encode(text, disallowed_special=()))
+
+
+def openai_tokenizer_encoding(model: str) -> Encoding:
+ """The encoding `token_counter` uses for a model on the `openai_tokenizer` path."""
+ return get_encoding(openai_tokenizer_encoding_name(model))
+
+
+def openai_tokenizer_encoding_name(model: str) -> str:
+ """The tiktoken encoding name for `model`, without loading the encoding."""
from litellm.utils import print_verbose
model_to_use: Final = _fix_model_name(model)
if "gpt-4o" in model_to_use:
- return tiktoken.get_encoding("o200k_base")
+ return "o200k_base"
try:
- return tiktoken.encoding_for_model(model_to_use)
+ return tiktoken.encoding_name_for_model(model_to_use)
except KeyError:
print_verbose("Warning: model not found. Using cl100k_base encoding.")
- return tiktoken.get_encoding("cl100k_base")
+ return "cl100k_base"
def uses_legacy_message_accounting(model: str) -> bool:
diff --git a/litellm/litellm_core_utils/tokenizer.py b/litellm/litellm_core_utils/tokenizer.py
new file mode 100644
index 00000000000..aea187fa08e
--- /dev/null
+++ b/litellm/litellm_core_utils/tokenizer.py
@@ -0,0 +1,402 @@
+"""Python faces of the Rust text codecs.
+
+``OpenAIEncoding`` mirrors ``tiktoken.Encoding`` and ``HuggingFaceTokenizer`` mirrors
+``tokenizers.Tokenizer``, so a caller holding ``litellm.encoding`` or the object returned by
+``litellm.create_tokenizer`` sees the same read-only surface whichever backend the Rust catalog
+selected. Both wrappers are immutable: ``tokenizers`` mutators (``enable_padding``,
+``enable_truncation``, ``add_tokens``) stay on the Python tokenizer.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Callable, Collection, Mapping, Sequence, Set
+from concurrent.futures import ThreadPoolExecutor
+from dataclasses import dataclass
+from functools import partial
+from pathlib import Path
+from types import MappingProxyType
+from typing import TYPE_CHECKING, Final, Literal, Protocol, TypeAlias, runtime_checkable
+
+import tiktoken
+from tokenizers import AddedToken
+from tokenizers import Tokenizer as PythonHuggingFaceTokenizer
+
+if TYPE_CHECKING:
+ import numpy as np
+ import numpy.typing as npt
+
+ from litellm.rust_bridge._native import HuggingFaceEncoding
+ from litellm.rust_bridge._native import Tokenizer as NativeTokenizer
+
+SpecialTokens: TypeAlias = Literal["all"] | Collection[str]
+AllowedSpecial: TypeAlias = Literal["all"] | Set[str]
+HuggingFaceInput: TypeAlias = str | list[str] | tuple[str, ...]
+HuggingFaceBatchInput: TypeAlias = HuggingFaceInput | tuple[HuggingFaceInput, HuggingFaceInput] | list[HuggingFaceInput]
+
+
+@dataclass(frozen=True, slots=True)
+class OpenAIEncoding:
+ """``tiktoken.Encoding`` over the Rust tiktoken codec."""
+
+ _native: NativeTokenizer
+ _special_tokens: Mapping[str, int]
+
+ @staticmethod
+ def wrap(native: NativeTokenizer) -> OpenAIEncoding:
+ return OpenAIEncoding(native, MappingProxyType(native.special_tokens()))
+
+ @staticmethod
+ def from_tiktoken(encoding: str) -> OpenAIEncoding:
+ from litellm.rust_bridge._native import Tokenizer as NativeTokenizer
+
+ return OpenAIEncoding.wrap(NativeTokenizer.from_tiktoken(encoding))
+
+ def __repr__(self) -> str:
+ return f"<Encoding {self.name!r}>"
+
+ @property
+ def name(self) -> str:
+ return self._native.name
+
+ @property
+ def max_token_value(self) -> int:
+ return self._native.max_token_value()
+
+ @property
+ def n_vocab(self) -> int:
+ """For backwards compatibility. Prefer to use `enc.max_token_value + 1`."""
+ return self.max_token_value + 1
+
+ @property
+ def eot_token(self) -> int:
+ return self._special_tokens["<|endoftext|>"]
+
+ @property
+ def special_tokens_set(self) -> set[str]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return set(self._special_tokens)
+
+ def is_special_token(self, token: int) -> bool:
+ return self._native.is_special_token(token)
+
+ # ---- encoding -------------------------------------------------------------------------
+
+ def encode_ordinary(self, text: str) -> list[int]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._native.encode(text)
+
+ def encode(
+ self,
+ text: str,
+ *,
+ allowed_special: AllowedSpecial = frozenset(),
+ disallowed_special: SpecialTokens = "all",
+ ) -> list[int]: # mutable-ok: [LIT001, LIT002] SDK return type
+ allowed: Final = self._allowed(text, allowed_special, disallowed_special)
+ if not allowed:
+ return self.encode_ordinary(text)
+ return self._native.encode_special(text, tuple(allowed))
+
+ def encode_to_numpy(
+ self,
+ text: str,
+ *,
+ allowed_special: AllowedSpecial = frozenset(),
+ disallowed_special: SpecialTokens = "all",
+ ) -> npt.NDArray[np.uint32]:
+ import numpy
+
+ return numpy.asarray(
+ self.encode(text, allowed_special=allowed_special, disallowed_special=disallowed_special),
+ dtype=numpy.uint32,
+ )
+
+ def encode_ordinary_batch(
+ self, text: Sequence[str], *, num_threads: int = 8
+ ) -> list[list[int]]: # mutable-ok: [LIT001, LIT002] SDK return type
+ with ThreadPoolExecutor(num_threads) as executor:
+ return list( # mutable-ok: [LIT002] SDK returns a list
+ executor.map(self.encode_ordinary, text)
+ )
+
+ def encode_batch(
+ self,
+ text: Sequence[str],
+ *,
+ num_threads: int = 8,
+ allowed_special: AllowedSpecial = frozenset(),
+ disallowed_special: SpecialTokens = "all",
+ ) -> list[list[int]]: # mutable-ok: [LIT001, LIT002] SDK return type
+ encode: Final = partial(self.encode, allowed_special=allowed_special, disallowed_special=disallowed_special)
+ with ThreadPoolExecutor(num_threads) as executor:
+ return list( # mutable-ok: [LIT002] SDK returns a list
+ executor.map(encode, text)
+ )
+
+ def encode_with_unstable(
+ self,
+ text: str,
+ *,
+ allowed_special: AllowedSpecial = frozenset(),
+ disallowed_special: SpecialTokens = "all",
+ ) -> tuple[list[int], list[list[int]]]: # mutable-ok: [LIT001, LIT002] SDK return type
+ """The stable tokens of `text` and every completion its unstable tail could become.
+
+ Completions come back sorted; tiktoken returns them in hash order."""
+ allowed: Final = self._allowed(text, allowed_special, disallowed_special)
+ return self._native.encode_with_unstable(text, tuple(allowed))
+
+ def encode_single_token(self, text_or_bytes: str | bytes) -> int:
+ """The token of one whole piece, special tokens included. Raises `KeyError` otherwise."""
+ piece: Final = text_or_bytes.encode("utf-8") if isinstance(text_or_bytes, str) else text_or_bytes
+ return self._native.encode_single_token(piece)
+
+ def count(self, text: str, fast: bool = False) -> int:
+ """Count ordinary text; `fast` accelerates supported encodings and otherwise counts normally."""
+ return self._native.count(text, fast)
+
+ # ---- decoding -------------------------------------------------------------------------
+
+ def decode_bytes(self, tokens: Sequence[int]) -> bytes:
+ return self._native.decode_bytes(tokens)
+
+ def decode(self, tokens: Sequence[int], errors: str = "replace") -> str:
+ return self.decode_bytes(tokens).decode("utf-8", errors=errors)
+
+ def decode_single_token_bytes(self, token: int) -> bytes:
+ return self.decode_bytes((token,))
+
+ def decode_tokens_bytes(self, tokens: Sequence[int]) -> list[bytes]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return [ # mutable-ok: [LIT002] SDK returns a list
+ self.decode_single_token_bytes(token) for token in tokens
+ ]
+
+ def decode_with_offsets(
+ self, tokens: Sequence[int]
+ ) -> tuple[str, list[int]]: # mutable-ok: [LIT001, LIT002] SDK return type
+ """The decoded text and, per token, the index of the first character holding its bytes.
+
+ Like tiktoken, raises `UnicodeDecodeError` when the tokens do not decode to valid UTF-8."""
+ token_bytes: Final = self.decode_tokens_bytes(tokens)
+ text_len = 0
+ offsets: Final[list[int]] = [] # mutable-ok: [LIT001] local accumulator
+ for token in token_bytes:
+ offsets.append(max(0, text_len - (0x80 <= token[0] < 0xC0)))
+ text_len += sum(1 for c in token if not 0x80 <= c < 0xC0)
+ return b"".join(token_bytes).decode("utf-8", errors="strict"), offsets
+
+ def decode_batch(
+ self, batch: Sequence[Sequence[int]], *, errors: str = "replace", num_threads: int = 8
+ ) -> list[str]: # mutable-ok: [LIT001, LIT002] SDK return type
+ with ThreadPoolExecutor(num_threads) as executor:
+ return list( # mutable-ok: [LIT002] SDK returns a list
+ executor.map(partial(self.decode, errors=errors), batch)
+ )
+
+ def decode_bytes_batch(
+ self, batch: Sequence[Sequence[int]], *, num_threads: int = 8
+ ) -> list[bytes]: # mutable-ok: [LIT001, LIT002] SDK return type
+ with ThreadPoolExecutor(num_threads) as executor:
+ return list( # mutable-ok: [LIT002] SDK returns a list
+ executor.map(self.decode_bytes, batch)
+ )
+
+ def token_byte_values(self) -> list[bytes]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._native.token_byte_values()
+
+ def __reduce__(self) -> tuple[Callable[[str], OpenAIEncoding], tuple[str]]:
+ return (OpenAIEncoding.from_tiktoken, (self.name,))
+
+ # ---- private --------------------------------------------------------------------------
+
+ def _allowed(self, text: str, allowed_special: AllowedSpecial, disallowed_special: SpecialTokens) -> frozenset[str]:
+ """tiktoken's special-token policy: which specials `text` may encode, after rejecting
+ any it must not contain."""
+ allowed: Final = frozenset(self._special_tokens) if allowed_special == "all" else frozenset(allowed_special)
+ disallowed: Final = (
+ frozenset(self._special_tokens) - allowed if disallowed_special == "all" else frozenset(disallowed_special)
+ )
+ for token in disallowed:
+ if token in text:
+ raise ValueError(
+ 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"
+ "If you want this text to be encoded as normal text, disable the check for this token "
+ f"by passing `disallowed_special=(enc.special_tokens_set - {{{token!r}}})`.\n"
+ "To disable this check for all special tokens, pass `disallowed_special=()`.\n"
+ )
+ return allowed
+
+
+@dataclass(frozen=True, slots=True)
+class HuggingFaceTokenizer:
+ """The read-only ``tokenizers.Tokenizer`` surface over the Rust Hugging Face codec."""
+
+ _native: NativeTokenizer
+
+ @staticmethod
+ def from_str(json: str) -> HuggingFaceTokenizer:
+ from litellm.rust_bridge._native import Tokenizer as NativeTokenizer
+
+ return HuggingFaceTokenizer(NativeTokenizer.from_json(json))
+
+ from_json = from_str
+
+ @staticmethod
+ def from_buffer(buffer: bytes) -> HuggingFaceTokenizer:
+ return HuggingFaceTokenizer.from_str(buffer.decode("utf-8"))
+
+ @staticmethod
+ def from_file(path: str) -> HuggingFaceTokenizer:
+ return HuggingFaceTokenizer.from_str(Path(path).read_text(encoding="utf-8"))
+
+ @staticmethod
+ def from_pretrained(identifier: str, revision: str = "main", token: str | None = None) -> HuggingFaceTokenizer:
+ from litellm.rust_bridge._native import Tokenizer as NativeTokenizer
+
+ return HuggingFaceTokenizer(NativeTokenizer.from_pretrained(identifier, revision=revision, token=token))
+
+ def to_str(self, pretty: bool = False) -> str:
+ return self._native.to_json(pretty)
+
+ def save(self, path: str, pretty: bool = True) -> None:
+ Path(path).write_text(self.to_str(pretty), encoding="utf-8")
+
+ @property
+ def name(self) -> str:
+ return self._native.name
+
+ # ---- vocabulary -----------------------------------------------------------------------
+
+ def token_to_id(self, token: str) -> int | None:
+ return self._native.token_to_id(token)
+
+ def id_to_token(self, id: int) -> str | None:
+ return self._native.id_to_token(id)
+
+ def get_vocab(
+ self, with_added_tokens: bool = True
+ ) -> dict[str, int]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._native.get_vocab(with_added_tokens)
+
+ def get_vocab_size(self, with_added_tokens: bool = True) -> int:
+ return self._native.get_vocab_size(with_added_tokens)
+
+ def get_added_tokens_decoder(self) -> dict[int, AddedToken]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return { # mutable-ok: [LIT002] SDK returns a dict
+ token_id: AddedToken(
+ content, single_word=single_word, lstrip=lstrip, rstrip=rstrip, normalized=normalized, special=special
+ )
+ for token_id, (
+ content,
+ single_word,
+ lstrip,
+ rstrip,
+ normalized,
+ special,
+ ) in self._native.added_tokens_decoder()
+ }
+
+ def num_special_tokens_to_add(self, is_pair: bool) -> int:
+ return self._native.num_special_tokens_to_add(is_pair)
+
+ @property
+ def padding(self) -> dict[str, object] | None: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._native.padding()
+
+ @property
+ def truncation(self) -> dict[str, object] | None: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._native.truncation()
+
+ @property
+ def encode_special_tokens(self) -> bool:
+ return self._native.encode_special_tokens()
+
+ # ---- encoding and decoding ------------------------------------------------------------
+
+ def encode(
+ self,
+ sequence: HuggingFaceInput,
+ pair: HuggingFaceInput | None = None,
+ is_pretokenized: bool = False,
+ add_special_tokens: bool = True,
+ ) -> HuggingFaceEncoding:
+ return self._native.encode_huggingface(sequence, pair, is_pretokenized, add_special_tokens)
+
+ def encode_batch(
+ self,
+ input: Sequence[HuggingFaceBatchInput],
+ is_pretokenized: bool = False,
+ add_special_tokens: bool = True,
+ ) -> list[HuggingFaceEncoding]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._encode_batch(input, is_pretokenized, add_special_tokens, fast=False)
+
+ def encode_batch_fast(
+ self,
+ input: Sequence[HuggingFaceBatchInput],
+ is_pretokenized: bool = False,
+ add_special_tokens: bool = True,
+ ) -> list[HuggingFaceEncoding]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return self._encode_batch(input, is_pretokenized, add_special_tokens, fast=True)
+
+ def _encode_batch(
+ self, input: Sequence[HuggingFaceBatchInput], is_pretokenized: bool, add_special_tokens: bool, fast: bool
+ ) -> list[HuggingFaceEncoding]: # mutable-ok: [LIT001, LIT002] SDK return type
+ sequences: Final = tuple(_batch_input(item, is_pretokenized) for item in input)
+ return self._native.encode_batch_huggingface(sequences, is_pretokenized, add_special_tokens, fast)
+
+ def count(self, text: str, fast: bool = False) -> int:
+ """Count with this tokenizer's configuration; `fast` uses acceleration where supported."""
+ return self._native.count(text, fast)
+
+ def decode(self, ids: Sequence[int], skip_special_tokens: bool = True) -> str:
+ return self._native.decode(ids, skip_special_tokens=skip_special_tokens)
+
+ def decode_batch(
+ self, sequences: Sequence[Sequence[int]], skip_special_tokens: bool = True
+ ) -> list[str]: # mutable-ok: [LIT001, LIT002] SDK return type
+ return [ # mutable-ok: [LIT002] SDK returns a list
+ self.decode(ids, skip_special_tokens=skip_special_tokens) for ids in sequences
+ ]
+
+ def __reduce__(self) -> tuple[Callable[[str], HuggingFaceTokenizer], tuple[str]]:
+ return (HuggingFaceTokenizer.from_str, (self.to_str(),))
+
+
+def _batch_input(
+ item: HuggingFaceBatchInput, is_pretokenized: bool
+) -> tuple[HuggingFaceInput, HuggingFaceInput | None]:
+ if isinstance(item, str):
+ return (item, None)
+ if is_pretokenized and all(isinstance(word, str) for word in item):
+ return (tuple(word for word in item if isinstance(word, str)), None)
+ if len(item) != 2:
+ raise TypeError("batch input must be a sequence or a pair of sequences")
+ return (item[0], item[1])
+
+
+Encoding: TypeAlias = tiktoken.Encoding | OpenAIEncoding
+HuggingFace: TypeAlias = PythonHuggingFaceTokenizer | HuggingFaceTokenizer
+Tokenizer: TypeAlias = Encoding | HuggingFace
+
+
+class _AddedToken(Protocol):
+ @property
+ def special(self) -> bool: ...
+
+
+@runtime_checkable
+class _AddedTokenDecoder(Protocol):
+ def get_added_tokens_decoder(self) -> Mapping[int, _AddedToken]: ...
+
+
+def strip_special_tokens(tokenizer: object, tokens: Sequence[int]) -> Sequence[int]:
+ """Drop the special added tokens before a Python `tokenizers` decode; the Rust codec's
+ `decode(skip_special_tokens=True)` already does this itself."""
+ if isinstance(tokenizer, HuggingFaceTokenizer) or not isinstance(tokenizer, _AddedTokenDecoder):
+ return tokens
+ try:
+ added: Final = tokenizer.get_added_tokens_decoder()
+ except Exception: # noqa: BLE001 # optional metadata failures historically fall back to decoding
+ return tokens
+ special_ids: Final = frozenset(token_id for token_id, token in added.items() if token.special)
+ return tuple(token for token in tokens if token not in special_ids)
diff --git a/litellm/llms/a2a/chat/transformation.py b/litellm/llms/a2a/chat/transformation.py
index 77f26b65de0..c5a71daaba5 100644
--- a/litellm/llms/a2a/chat/transformation.py
+++ b/litellm/llms/a2a/chat/transformation.py
@@ -23,9 +23,8 @@ from ..common_utils import (
from .streaming_iterator import A2AModelResponseIterator
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
_REGISTRY_PARAMS_KEPT_OUT_OF_OPTIONAL_PARAMS: Final = (
@@ -292,7 +291,7 @@ class A2AConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/aiml/image_generation/transformation.py b/litellm/llms/aiml/image_generation/transformation.py
index 4f4cd074165..b585d35a2ac 100644
--- a/litellm/llms/aiml/image_generation/transformation.py
+++ b/litellm/llms/aiml/image_generation/transformation.py
@@ -14,9 +14,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -171,7 +170,7 @@ class AimlImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/aiohttp_openai/chat/transformation.py b/litellm/llms/aiohttp_openai/chat/transformation.py
index 530896bf9b0..a06c670e3f1 100644
--- a/litellm/llms/aiohttp_openai/chat/transformation.py
+++ b/litellm/llms/aiohttp_openai/chat/transformation.py
@@ -16,9 +16,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import Choices, ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -68,7 +67,7 @@ class AiohttpOpenAIChatConfig(OpenAILikeChatConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/amazon_nova/chat/transformation.py b/litellm/llms/amazon_nova/chat/transformation.py
index 7551fb28c21..a93fbf1e933 100644
--- a/litellm/llms/amazon_nova/chat/transformation.py
+++ b/litellm/llms/amazon_nova/chat/transformation.py
@@ -17,7 +17,7 @@ from litellm.types.utils import ModelResponse
from ...openai_like.chat.transformation import OpenAILikeChatConfig
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AmazonNovaChatConfig(OpenAILikeChatConfig):
@@ -86,7 +86,7 @@ class AmazonNovaChatConfig(OpenAILikeChatConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/anthropic/batches/transformation.py b/litellm/llms/anthropic/batches/transformation.py
index 4f4d39f09b0..7dee7513538 100644
--- a/litellm/llms/anthropic/batches/transformation.py
+++ b/litellm/llms/anthropic/batches/transformation.py
@@ -14,9 +14,8 @@ from litellm.types.llms.openai import AllMessageValues, CreateBatchRequest
from litellm.types.utils import LiteLLMBatch, LlmProviders, ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -290,7 +289,7 @@ class AnthropicBatchesConfig(BaseBatchesConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py
index 545e920156e..c221e9f1505 100644
--- a/litellm/llms/anthropic/chat/transformation.py
+++ b/litellm/llms/anthropic/chat/transformation.py
@@ -100,9 +100,8 @@ from ..common_utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -2688,7 +2687,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/anthropic/completion/transformation.py b/litellm/llms/anthropic/completion/transformation.py
index b15b0159bd9..46ab27ab0c7 100644
--- a/litellm/llms/anthropic/completion/transformation.py
+++ b/litellm/llms/anthropic/completion/transformation.py
@@ -33,7 +33,7 @@ from litellm.types.utils import (
)
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AnthropicTextError(BaseLLMException):
@@ -185,7 +185,7 @@ class AnthropicTextConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/azure/chat/gpt_transformation.py b/litellm/llms/azure/chat/gpt_transformation.py
index 424422612db..355714c0daf 100644
--- a/litellm/llms/azure/chat/gpt_transformation.py
+++ b/litellm/llms/azure/chat/gpt_transformation.py
@@ -29,9 +29,8 @@ from ...base_llm.chat.transformation import BaseConfig
from ..common_utils import AzureOpenAIError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -304,7 +303,7 @@ class AzureOpenAIConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/azure_ai/agents/transformation.py b/litellm/llms/azure_ai/agents/transformation.py
index 60ce81a23c7..baba3149963 100644
--- a/litellm/llms/azure_ai/agents/transformation.py
+++ b/litellm/llms/azure_ai/agents/transformation.py
@@ -34,9 +34,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
LiteLLMLoggingObj = _LiteLLMLoggingObj
@@ -297,7 +296,7 @@ class AzureAIAgentsConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/azure_ai/azure_model_router/transformation.py b/litellm/llms/azure_ai/azure_model_router/transformation.py
index 9e35e396e15..0e4c8ca0d15 100644
--- a/litellm/llms/azure_ai/azure_model_router/transformation.py
+++ b/litellm/llms/azure_ai/azure_model_router/transformation.py
@@ -15,7 +15,7 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AzureModelRouterConfig(AzureAIStudioConfig):
@@ -59,7 +59,7 @@ class AzureModelRouterConfig(AzureAIStudioConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/azure_ai/chat/transformation.py b/litellm/llms/azure_ai/chat/transformation.py
index 00e1c1e25ba..779a86629e2 100644
--- a/litellm/llms/azure_ai/chat/transformation.py
+++ b/litellm/llms/azure_ai/chat/transformation.py
@@ -30,7 +30,7 @@ from litellm.types.utils import ModelResponse, ProviderField
from litellm.utils import _add_path_to_api_base, supports_tool_choice
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AzureFoundryErrorStrings(str, enum.Enum):
@@ -305,7 +305,7 @@ class AzureAIStudioConfig(OpenAIConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/azure_ai/image_generation/mai_transformation.py b/litellm/llms/azure_ai/image_generation/mai_transformation.py
index 67b1a8bcab3..18b4b6f456a 100644
--- a/litellm/llms/azure_ai/image_generation/mai_transformation.py
+++ b/litellm/llms/azure_ai/image_generation/mai_transformation.py
@@ -12,9 +12,10 @@ from litellm.types.utils import ImageResponse
from litellm.utils import convert_to_model_response_object
if TYPE_CHECKING:
- import tiktoken
from litellm.litellm_core_utils.logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
+
class AzureFoundryMAIImageGenerationConfig(BaseImageGenerationConfig):
"""Azure AI Foundry MAI image generation (e.g. MAI-Image-2.5)."""
@@ -245,7 +246,7 @@ class AzureFoundryMAIImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/base_llm/audio_transcription/transformation.py b/litellm/llms/base_llm/audio_transcription/transformation.py
index 2296909cfe1..e4bf148abf3 100644
--- a/litellm/llms/base_llm/audio_transcription/transformation.py
+++ b/litellm/llms/base_llm/audio_transcription/transformation.py
@@ -12,9 +12,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import FileTypes, ModelResponse, TranscriptionResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -121,7 +120,7 @@ class BaseAudioTranscriptionConfig(BaseConfig, ABC):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/base_llm/bridges/completion_transformation.py b/litellm/llms/base_llm/bridges/completion_transformation.py
index 87b55152d09..a5c03705088 100644
--- a/litellm/llms/base_llm/bridges/completion_transformation.py
+++ b/litellm/llms/base_llm/bridges/completion_transformation.py
@@ -7,10 +7,10 @@ from collections.abc import AsyncIterator, Iterator
from typing import TYPE_CHECKING, Union
if TYPE_CHECKING:
- import tiktoken
from pydantic import BaseModel
from litellm import LiteLLMLoggingObj, ModelResponse
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIterator
from litellm.types.llms.openai import AllMessageValues
@@ -39,7 +39,7 @@ class CompletionTransformationBridge(ABC):
messages: list["AllMessageValues"],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> "ModelResponse":
diff --git a/litellm/llms/base_llm/chat/transformation.py b/litellm/llms/base_llm/chat/transformation.py
index 7bfc87a30d6..7decf1b4186 100644
--- a/litellm/llms/base_llm/chat/transformation.py
+++ b/litellm/llms/base_llm/chat/transformation.py
@@ -21,9 +21,8 @@ from litellm.types.llms.openai import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.types.utils import ModelResponse
from ..base_utils import (
@@ -344,7 +343,7 @@ class BaseConfig(ABC):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> "ModelResponse":
diff --git a/litellm/llms/base_llm/completion/transformation.py b/litellm/llms/base_llm/completion/transformation.py
index fb472dfa63b..b8ebbfed12f 100644
--- a/litellm/llms/base_llm/completion/transformation.py
+++ b/litellm/llms/base_llm/completion/transformation.py
@@ -8,9 +8,8 @@ from litellm.types.llms.openai import AllMessageValues, OpenAITextCompletionUser
from litellm.types.utils import ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -68,7 +67,7 @@ class BaseTextCompletionConfig(BaseConfig, ABC):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/base_llm/embedding/transformation.py b/litellm/llms/base_llm/embedding/transformation.py
index da87dcc7f98..46ac3ccf433 100644
--- a/litellm/llms/base_llm/embedding/transformation.py
+++ b/litellm/llms/base_llm/embedding/transformation.py
@@ -8,9 +8,8 @@ from litellm.types.llms.openai import AllEmbeddingInputValues, AllMessageValues
from litellm.types.utils import EmbeddingResponse, ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -80,7 +79,7 @@ class BaseEmbeddingConfig(BaseConfig, ABC):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/base_llm/files/transformation.py b/litellm/llms/base_llm/files/transformation.py
index 254995c028f..5ecf033fb2c 100644
--- a/litellm/llms/base_llm/files/transformation.py
+++ b/litellm/llms/base_llm/files/transformation.py
@@ -21,9 +21,8 @@ from litellm.types.utils import LlmProviders, ModelResponse
from ..chat.transformation import BaseConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.router import Router as _Router
from litellm.types.llms.openai import HttpxBinaryResponseContent
@@ -231,7 +230,7 @@ class BaseFilesConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/base_llm/image_generation/transformation.py b/litellm/llms/base_llm/image_generation/transformation.py
index 4616441133e..7ac440c6f48 100644
--- a/litellm/llms/base_llm/image_generation/transformation.py
+++ b/litellm/llms/base_llm/image_generation/transformation.py
@@ -11,9 +11,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -93,7 +92,7 @@ class BaseImageGenerationConfig(ABC):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/base_llm/image_variations/transformation.py b/litellm/llms/base_llm/image_variations/transformation.py
index d3e02139e0e..15a4e0f243c 100644
--- a/litellm/llms/base_llm/image_variations/transformation.py
+++ b/litellm/llms/base_llm/image_variations/transformation.py
@@ -17,9 +17,8 @@ from litellm.types.utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -82,7 +81,7 @@ class BaseImageVariationConfig(BaseConfig, ABC):
image: FileTypes,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
) -> ImageResponse:
pass
@@ -98,7 +97,7 @@ class BaseImageVariationConfig(BaseConfig, ABC):
image: FileTypes,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
) -> ImageResponse:
pass
@@ -125,7 +124,7 @@ class BaseImageVariationConfig(BaseConfig, ABC):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/agentcore/transformation.py b/litellm/llms/bedrock/chat/agentcore/transformation.py
index e1a9a807abc..29133bcfaf9 100644
--- a/litellm/llms/bedrock/chat/agentcore/transformation.py
+++ b/litellm/llms/bedrock/chat/agentcore/transformation.py
@@ -40,9 +40,8 @@ from litellm.types.utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
LiteLLMLoggingObj = _LiteLLMLoggingObj
@@ -990,7 +989,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py
index 4f9b1f56a5b..21830eb0d8e 100644
--- a/litellm/llms/bedrock/chat/converse_transformation.py
+++ b/litellm/llms/bedrock/chat/converse_transformation.py
@@ -99,7 +99,7 @@ from ..common_utils import (
)
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
# Computer use tool prefixes supported by Bedrock
BEDROCK_COMPUTER_USE_TOOLS: Final = [
@@ -1920,7 +1920,7 @@ class AmazonConverseConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_agent/transformation.py b/litellm/llms/bedrock/chat/invoke_agent/transformation.py
index d489e47c3b5..6877af74494 100644
--- a/litellm/llms/bedrock/chat/invoke_agent/transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_agent/transformation.py
@@ -37,9 +37,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import Choices, Message, ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -438,7 +437,7 @@ class AmazonInvokeAgentConfig(BaseConfig, BaseAWSLLM):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_deepseek_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_deepseek_transformation.py
index 5a3f4f17b8b..5699f94d084 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_deepseek_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_deepseek_transformation.py
@@ -25,7 +25,7 @@ from litellm.types.utils import (
from .amazon_llama_transformation import AmazonLlamaConfig
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AmazonDeepSeekR1Config(AmazonLlamaConfig):
@@ -39,7 +39,7 @@ class AmazonDeepSeekR1Config(AmazonLlamaConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_moonshot_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_moonshot_transformation.py
index 5d39b68d9d5..f8b730b6cd0 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_moonshot_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_moonshot_transformation.py
@@ -21,9 +21,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import Choices
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.types.utils import ModelResponse
LiteLLMLoggingObj = _LiteLLMLoggingObj
@@ -198,7 +197,7 @@ class AmazonMoonshotConfig(AmazonInvokeConfig, MoonshotChatConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> "ModelResponse":
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_nova_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_nova_transformation.py
index bc97551d57a..8d1ff1d2bd9 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_nova_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_nova_transformation.py
@@ -28,7 +28,7 @@ from ..converse_transformation import AmazonConverseConfig
from .base_invoke_transformation import AmazonInvokeConfig
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
_CachePointCarrier = TypeVar("_CachePointCarrier", SystemContentBlock, ContentBlock)
_INJECTION_POINTS: Final = TypeAdapter(tuple[Mapping[str, object], ...])
@@ -128,7 +128,7 @@ class AmazonInvokeNovaConfig(AmazonInvokeConfig, AmazonConverseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py
index c78375c37bb..67364ccfda0 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen2_transformation.py
@@ -21,7 +21,7 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse, Usage
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AmazonQwen2Config(AmazonQwen3Config):
@@ -44,7 +44,7 @@ class AmazonQwen2Config(AmazonQwen3Config):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py
index e251fb15725..2e19dbf77af 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_qwen3_transformation.py
@@ -19,7 +19,7 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse, Usage
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class AmazonQwen3Config(AmazonInvokeConfig, BaseConfig):
@@ -170,7 +170,7 @@ class AmazonQwen3Config(AmazonInvokeConfig, BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_twelvelabs_pegasus_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_twelvelabs_pegasus_transformation.py
index 39cded4ed64..fe287111fdd 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_twelvelabs_pegasus_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_twelvelabs_pegasus_transformation.py
@@ -25,9 +25,8 @@ from litellm.types.utils import ModelResponse, Usage
from litellm.utils import get_base64_str
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -190,7 +189,7 @@ class AmazonTwelveLabsPegasusConfig(AmazonInvokeConfig, BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py
index 1326dc22ca0..a8b94fb5703 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude3_transformation.py
@@ -34,9 +34,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -359,7 +358,7 @@ class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py
index 90a2692f68a..dcc5e249d8a 100644
--- a/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py
+++ b/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py
@@ -34,9 +34,8 @@ from litellm.types.utils import ModelResponse, Usage
from litellm.utils import CustomStreamWrapper
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -288,7 +287,7 @@ class AmazonInvokeConfig(BaseConfig, BaseAWSLLM):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/black_forest_labs/image_generation/transformation.py b/litellm/llms/black_forest_labs/image_generation/transformation.py
index 119ffff1c34..e5c2bdf59e9 100644
--- a/litellm/llms/black_forest_labs/image_generation/transformation.py
+++ b/litellm/llms/black_forest_labs/image_generation/transformation.py
@@ -29,9 +29,8 @@ from ..common_utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -258,7 +257,7 @@ class BlackForestLabsImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/brave/search/__init__.py b/litellm/llms/brave/search/__init__.py
index cc1168d7ef8..de70c62e040 100644
--- a/litellm/llms/brave/search/__init__.py
+++ b/litellm/llms/brave/search/__init__.py
@@ -1,7 +1,7 @@
-"""
-Brave Search API module.
-"""
-
-from litellm.llms.brave.search.transformation import BraveSearchConfig
-
-__all__ = ["BraveSearchConfig"]
+"""
+Brave Search API module.
+"""
+
+from litellm.llms.brave.search.transformation import BraveSearchConfig
+
+__all__ = ["BraveSearchConfig"]
diff --git a/litellm/llms/bytez/chat/transformation.py b/litellm/llms/bytez/chat/transformation.py
index 7977db0f056..e622761dd7f 100644
--- a/litellm/llms/bytez/chat/transformation.py
+++ b/litellm/llms/bytez/chat/transformation.py
@@ -23,9 +23,8 @@ from litellm.utils import CustomStreamWrapper, ModelResponse, Usage
from ..common_utils import API_BASE, BytezError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -187,7 +186,7 @@ class BytezChatConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/clarifai/chat/transformation.py b/litellm/llms/clarifai/chat/transformation.py
index 76d35467497..a0946254de0 100644
--- a/litellm/llms/clarifai/chat/transformation.py
+++ b/litellm/llms/clarifai/chat/transformation.py
@@ -13,9 +13,8 @@ from litellm.types.utils import ModelResponse
from ...openai.chat.gpt_transformation import OpenAIGPTConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -87,7 +86,7 @@ class ClarifaiConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/cohere/chat/transformation.py b/litellm/llms/cohere/chat/transformation.py
index fa46bd7f6cf..a26cdc81695 100644
--- a/litellm/llms/cohere/chat/transformation.py
+++ b/litellm/llms/cohere/chat/transformation.py
@@ -15,9 +15,8 @@ from ..common_utils import ModelResponseIterator as CohereModelResponseIterator
from ..common_utils import validate_environment as cohere_validate_environment
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -227,7 +226,7 @@ class CohereChatConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/cohere/chat/v2_transformation.py b/litellm/llms/cohere/chat/v2_transformation.py
index 4252e7d02e9..37c53640d18 100644
--- a/litellm/llms/cohere/chat/v2_transformation.py
+++ b/litellm/llms/cohere/chat/v2_transformation.py
@@ -20,9 +20,8 @@ from ..common_utils import CohereError, CohereV2ModelResponseIterator
from ..common_utils import validate_environment as cohere_validate_environment
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -191,7 +190,7 @@ class CohereV2ChatConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/cohere/embed/handler.py b/litellm/llms/cohere/embed/handler.py
index 3cebf6b9a90..6d9543d7c30 100644
--- a/litellm/llms/cohere/embed/handler.py
+++ b/litellm/llms/cohere/embed/handler.py
@@ -20,7 +20,7 @@ from litellm.types.utils import EmbeddingResponse
from .v1_transformation import CohereEmbeddingConfig
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
def validate_environment(api_key, headers: dict):
@@ -60,7 +60,7 @@ async def async_embedding(
api_base: str,
api_key: str | None,
headers: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
client: AsyncHTTPHandler | None = None,
):
## LOGGING
@@ -122,7 +122,7 @@ def embedding(
logging_obj: LiteLLMLoggingObj,
optional_params: dict,
headers: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
data: dict | CohereEmbeddingRequest | None = None,
complete_api_base: str | None = None,
api_key: str | None = None,
diff --git a/litellm/llms/cometapi/image_generation/transformation.py b/litellm/llms/cometapi/image_generation/transformation.py
index 03c820de198..4432c151a64 100644
--- a/litellm/llms/cometapi/image_generation/transformation.py
+++ b/litellm/llms/cometapi/image_generation/transformation.py
@@ -13,9 +13,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -132,7 +131,7 @@ class CometAPIImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/compactifai/chat/transformation.py b/litellm/llms/compactifai/chat/transformation.py
index 3c5a889ce63..a02db2338d8 100644
--- a/litellm/llms/compactifai/chat/transformation.py
+++ b/litellm/llms/compactifai/chat/transformation.py
@@ -17,9 +17,8 @@ from litellm.types.utils import ModelResponse
from ...openai.chat.gpt_transformation import OpenAIGPTConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -66,7 +65,7 @@ class CompactifAIChatConfig(OpenAIGPTConfig):
messages: Sequence[AllMessageValues],
optional_params: Mapping[str, object],
litellm_params: Mapping[str, object],
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/custom_httpx/aiohttp_handler.py b/litellm/llms/custom_httpx/aiohttp_handler.py
index 0809ef5274f..034c9514092 100644
--- a/litellm/llms/custom_httpx/aiohttp_handler.py
+++ b/litellm/llms/custom_httpx/aiohttp_handler.py
@@ -26,9 +26,8 @@ from litellm.types.utils import HttpHandlerRequestFields, ImageResponse, LlmProv
from litellm.utils import CustomStreamWrapper, ModelResponse, ProviderConfigManager
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -268,7 +267,7 @@ class BaseLLMAIOHTTPHandler:
messages: list,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
client: ClientSession | None = None,
):
diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py
index 18d4c27fec1..333ce523e34 100644
--- a/litellm/llms/custom_httpx/llm_http_handler.py
+++ b/litellm/llms/custom_httpx/llm_http_handler.py
@@ -192,12 +192,12 @@ def _rust_responses_websocket_enabled(
from .http_handler import get_shared_realtime_ssl_context
if TYPE_CHECKING:
- import tiktoken
from aiohttp import ClientSession
from websockets.asyncio.client import ClientConnection
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.anthropic.experimental_pass_through.messages.fake_stream_iterator import (
FakeAnthropicMessagesStreamIterator,
)
@@ -493,7 +493,7 @@ class BaseLLMHTTPHandler:
messages: list,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
client: AsyncHTTPHandler | None = None,
json_mode: bool = False,
@@ -559,7 +559,7 @@ class BaseLLMHTTPHandler:
api_base: str | None,
custom_llm_provider: str,
model_response: ModelResponse,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
logging_obj: LiteLLMLoggingObj,
optional_params: dict,
timeout: float | httpx.Timeout,
diff --git a/litellm/llms/dashscope/image_generation/transformation.py b/litellm/llms/dashscope/image_generation/transformation.py
index c0e278a96ef..ffa60a3d9bc 100644
--- a/litellm/llms/dashscope/image_generation/transformation.py
+++ b/litellm/llms/dashscope/image_generation/transformation.py
@@ -38,9 +38,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -165,7 +164,7 @@ class DashScopeImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/databricks/chat/transformation.py b/litellm/llms/databricks/chat/transformation.py
index 30144d29f51..538904b34e6 100644
--- a/litellm/llms/databricks/chat/transformation.py
+++ b/litellm/llms/databricks/chat/transformation.py
@@ -149,9 +149,8 @@ def _split_parallel_tool_calls(messages: list[AllMessageValues]) -> list[AllMess
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -189,7 +188,7 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig):
return "databricks"
@classmethod
- def get_config(cls):
+ def get_config(cls, *, model: str | None = None):
return super().get_config()
def get_required_params(self) -> list[ProviderField]:
@@ -651,7 +650,7 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/deprecated_providers/aleph_alpha.py b/litellm/llms/deprecated_providers/aleph_alpha.py
index 2ad9ce4edc8..8f5c35f32f2 100644
--- a/litellm/llms/deprecated_providers/aleph_alpha.py
+++ b/litellm/llms/deprecated_providers/aleph_alpha.py
@@ -277,12 +277,7 @@ def completion(
## CALCULATING USAGE - baseten charges on time, not tokens - have some mapping of cost here.
prompt_tokens: Final = len(encoding.encode(prompt))
- completion_tokens: Final = len(
- encoding.encode(
- model_response["choices"][0]["message"]["content"],
- disallowed_special=(),
- )
- )
+ completion_tokens: Final = len(encoding.encode(model_response["choices"][0]["message"]["content"]))
model_response.created = int(time.time())
model_response.model = model
diff --git a/litellm/llms/edenai/chat/transformation.py b/litellm/llms/edenai/chat/transformation.py
index 4fd5a9d550b..67d308d9e38 100644
--- a/litellm/llms/edenai/chat/transformation.py
+++ b/litellm/llms/edenai/chat/transformation.py
@@ -25,9 +25,8 @@ from litellm.types.utils import ModelResponse, ModelResponseStream, Usage
from ..common_utils import EdenAIException, reported_cost, resolve_api_base, resolve_api_key
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding
_OPTIONAL_MAPPING: Final[TypeAdapter[Mapping[str, object] | None]] = TypeAdapter(Mapping[str, object] | None)
@@ -97,7 +96,7 @@ class EdenAIChatConfig(OpenAIGPTConfig):
messages: list[AllMessageValues], # mutable-ok: inherited contract
optional_params: dict[str, object], # mutable-ok: inherited contract
litellm_params: dict[str, object], # mutable-ok: inherited contract
- encoding: "tiktoken.Encoding | None",
+ encoding: "Encoding | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/edenai/image_generation/transformation.py b/litellm/llms/edenai/image_generation/transformation.py
index 2965c4041de..7f729cd7fbb 100644
--- a/litellm/llms/edenai/image_generation/transformation.py
+++ b/litellm/llms/edenai/image_generation/transformation.py
@@ -19,9 +19,8 @@ from litellm.utils import convert_to_model_response_object
from ..common_utils import EdenAIException, endpoint_url, json_headers, pick, reported_cost
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding
_SUPPORTED_PARAMS: Final[tuple[OpenAIImageGenerationOptionalParams, ...]] = (
"background",
@@ -94,7 +93,7 @@ class EdenAIImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict[str, object], # mutable-ok: inherited contract
optional_params: dict[str, object], # mutable-ok: inherited contract
litellm_params: dict[str, object], # mutable-ok: inherited contract
- encoding: "tiktoken.Encoding | None",
+ encoding: "Encoding | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/bria_transformation.py b/litellm/llms/fal_ai/image_generation/bria_transformation.py
index c528550811a..53da48e62ca 100644
--- a/litellm/llms/fal_ai/image_generation/bria_transformation.py
+++ b/litellm/llms/fal_ai/image_generation/bria_transformation.py
@@ -8,9 +8,8 @@ from litellm.types.utils import ImageObject, ImageResponse
from .transformation import FalAIBaseConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -187,7 +186,7 @@ class FalAIBriaConfig(FalAIBaseConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py b/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py
index 6b8558b8124..7c63e1077f1 100644
--- a/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py
+++ b/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py
@@ -8,9 +8,8 @@ from litellm.types.utils import ImageResponse
from .transformation import FalAIBaseConfig, fal_images_to_image_objects
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -194,7 +193,7 @@ class FalAIFluxProV11UltraConfig(FalAIBaseConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/ideogram_v3_transformation.py b/litellm/llms/fal_ai/image_generation/ideogram_v3_transformation.py
index 04b4f426878..ad1852a622b 100644
--- a/litellm/llms/fal_ai/image_generation/ideogram_v3_transformation.py
+++ b/litellm/llms/fal_ai/image_generation/ideogram_v3_transformation.py
@@ -8,9 +8,8 @@ from litellm.types.utils import ImageObject, ImageResponse
from .transformation import FalAIBaseConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -150,7 +149,7 @@ class FalAIIdeogramV3Config(FalAIBaseConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/imagen4_transformation.py b/litellm/llms/fal_ai/image_generation/imagen4_transformation.py
index 8a6665b2585..3624b76a4a3 100644
--- a/litellm/llms/fal_ai/image_generation/imagen4_transformation.py
+++ b/litellm/llms/fal_ai/image_generation/imagen4_transformation.py
@@ -8,9 +8,8 @@ from litellm.types.utils import ImageObject, ImageResponse
from .transformation import FalAIBaseConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -182,7 +181,7 @@ class FalAIImagen4Config(FalAIBaseConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/recraft_v3_transformation.py b/litellm/llms/fal_ai/image_generation/recraft_v3_transformation.py
index 4880dfec7e3..934ce420d53 100644
--- a/litellm/llms/fal_ai/image_generation/recraft_v3_transformation.py
+++ b/litellm/llms/fal_ai/image_generation/recraft_v3_transformation.py
@@ -8,9 +8,8 @@ from litellm.types.utils import ImageObject, ImageResponse
from .transformation import FalAIBaseConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -172,7 +171,7 @@ class FalAIRecraftV3Config(FalAIBaseConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/stable_diffusion_transformation.py b/litellm/llms/fal_ai/image_generation/stable_diffusion_transformation.py
index bc3a4d07282..79d8800773b 100644
--- a/litellm/llms/fal_ai/image_generation/stable_diffusion_transformation.py
+++ b/litellm/llms/fal_ai/image_generation/stable_diffusion_transformation.py
@@ -8,9 +8,8 @@ from litellm.types.utils import ImageObject, ImageResponse
from .transformation import FalAIBaseConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -208,7 +207,7 @@ class FalAIStableDiffusionConfig(FalAIBaseConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fal_ai/image_generation/transformation.py b/litellm/llms/fal_ai/image_generation/transformation.py
index fd8e280da1c..8f081c7228d 100644
--- a/litellm/llms/fal_ai/image_generation/transformation.py
+++ b/litellm/llms/fal_ai/image_generation/transformation.py
@@ -16,9 +16,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -117,7 +116,7 @@ class FalAIBaseConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/fireworks_ai/chat/transformation.py b/litellm/llms/fireworks_ai/chat/transformation.py
index 28ebb39a303..196022b3558 100644
--- a/litellm/llms/fireworks_ai/chat/transformation.py
+++ b/litellm/llms/fireworks_ai/chat/transformation.py
@@ -46,7 +46,7 @@ from ..common_utils import (
)
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
def _map_reasoning_effort(value: object) -> object:
@@ -708,7 +708,7 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/gemini/image_generation/transformation.py b/litellm/llms/gemini/image_generation/transformation.py
index d009fe4cd72..bb8d7455031 100644
--- a/litellm/llms/gemini/image_generation/transformation.py
+++ b/litellm/llms/gemini/image_generation/transformation.py
@@ -24,9 +24,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -173,7 +172,7 @@ class GoogleImageGenConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py
index d9250ea8836..c047dc0c881 100644
--- a/litellm/llms/gigachat/chat/transformation.py
+++ b/litellm/llms/gigachat/chat/transformation.py
@@ -26,9 +26,8 @@ from ..authenticator import get_access_token
from ..file_handler import upload_file_sync
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -416,7 +415,7 @@ class GigaChatConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: tiktoken.Encoding | None,
+ encoding: Tokenizer | None,
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/groq/chat/transformation.py b/litellm/llms/groq/chat/transformation.py
index 41a2df17c6f..1da7ad0a7b5 100644
--- a/litellm/llms/groq/chat/transformation.py
+++ b/litellm/llms/groq/chat/transformation.py
@@ -27,7 +27,7 @@ from litellm.types.utils import ModelResponse, ModelResponseStream, ServerToolUs
from ...openai_like.chat.transformation import OpenAILikeChatConfig
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
GROQ_COMPOUND_MODELS: Final = frozenset({"compound", "compound-mini"})
@@ -286,7 +286,7 @@ class GroqChatConfig(OpenAILikeChatConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/huggingface/embedding/handler.py b/litellm/llms/huggingface/embedding/handler.py
index 57d1357ee46..60917c68221 100644
--- a/litellm/llms/huggingface/embedding/handler.py
+++ b/litellm/llms/huggingface/embedding/handler.py
@@ -1,6 +1,5 @@
import json
import os
-from collections.abc import Sequence
from typing import Final, Literal, Protocol, get_args
import httpx
@@ -32,7 +31,7 @@ hf_tasks_embeddings: Final = (
class _SupportsTokenEncode(Protocol):
"""Token encoder handle. Only ``encode`` is ever called on it here."""
- def encode(self, text: str, *, disallowed_special: tuple[str, ...]) -> Sequence[int]: ...
+ def encode(self, text: str) -> list[int]: ...
def get_hf_task_embedding_for_model(model: str, task_type: str | None, api_base: str) -> str | None:
@@ -214,7 +213,7 @@ class HuggingFaceEmbedding(BaseLLM):
model_response.model = model
input_tokens = 0
for text in input:
- input_tokens += len(encoding.encode(text, disallowed_special=()))
+ input_tokens += len(encoding.encode_ordinary(text))
setattr(
model_response,
diff --git a/litellm/llms/huggingface/embedding/transformation.py b/litellm/llms/huggingface/embedding/transformation.py
index 33b0e21e326..3fdd4abda73 100644
--- a/litellm/llms/huggingface/embedding/transformation.py
+++ b/litellm/llms/huggingface/embedding/transformation.py
@@ -25,9 +25,8 @@ from litellm.utils import token_counter
from ..common_utils import HuggingFaceError, hf_task_list, hf_tasks, output_parser
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -479,7 +478,7 @@ class HuggingFaceEmbeddingConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/langflow/chat/transformation.py b/litellm/llms/langflow/chat/transformation.py
index 17ae7017cf6..a53887b36af 100644
--- a/litellm/llms/langflow/chat/transformation.py
+++ b/litellm/llms/langflow/chat/transformation.py
@@ -14,9 +14,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import Choices, Message, ModelResponse, Usage
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.utils import CustomStreamWrapper
@@ -225,7 +224,7 @@ class LangFlowConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/langgraph/chat/transformation.py b/litellm/llms/langgraph/chat/transformation.py
index 84d79e6bd31..c9388ee472f 100644
--- a/litellm/llms/langgraph/chat/transformation.py
+++ b/litellm/llms/langgraph/chat/transformation.py
@@ -23,9 +23,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import Choices, Message, ModelResponse, Usage
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.utils import CustomStreamWrapper
@@ -415,7 +414,7 @@ class LangGraphConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/lemonade/chat/transformation.py b/litellm/llms/lemonade/chat/transformation.py
index c01ad2a0edd..341e8dd2e12 100644
--- a/litellm/llms/lemonade/chat/transformation.py
+++ b/litellm/llms/lemonade/chat/transformation.py
@@ -19,7 +19,7 @@ from litellm.types.utils import ModelResponse
from ...openai_like.chat.transformation import OpenAILikeChatConfig
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class LemonadeChatConfig(OpenAILikeChatConfig):
@@ -231,7 +231,7 @@ class LemonadeChatConfig(OpenAILikeChatConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/mistral/chat/transformation.py b/litellm/llms/mistral/chat/transformation.py
index f77e828b59a..33b567e9710 100644
--- a/litellm/llms/mistral/chat/transformation.py
+++ b/litellm/llms/mistral/chat/transformation.py
@@ -32,7 +32,7 @@ from litellm.types.utils import ModelResponse, ModelResponseStream
from litellm.utils import convert_to_model_response_object, supports_reasoning
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
def _accepted_reasoning_effort(model: str, requested: str, custom_llm_provider: str) -> str:
@@ -580,7 +580,7 @@ class MistralConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/nlp_cloud/chat/transformation.py b/litellm/llms/nlp_cloud/chat/transformation.py
index 17c547618d3..2ff48894619 100644
--- a/litellm/llms/nlp_cloud/chat/transformation.py
+++ b/litellm/llms/nlp_cloud/chat/transformation.py
@@ -14,9 +14,8 @@ from litellm.utils import ModelResponse, Usage
from ..common_utils import NLPCloudError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -175,7 +174,7 @@ class NLPCloudConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/oci/chat/transformation.py b/litellm/llms/oci/chat/transformation.py
index 8e4e41b4ac1..ecff823a18d 100644
--- a/litellm/llms/oci/chat/transformation.py
+++ b/litellm/llms/oci/chat/transformation.py
@@ -65,9 +65,8 @@ from litellm.types.utils import (
from litellm.utils import supports_reasoning
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -603,7 +602,7 @@ class OCIChatConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py
index bcde8a041a6..cb3080e6534 100644
--- a/litellm/llms/ollama/chat/transformation.py
+++ b/litellm/llms/ollama/chat/transformation.py
@@ -31,9 +31,8 @@ from litellm.types.utils import ModelResponse, ModelResponseStream
from ..common_utils import OllamaError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -321,7 +320,7 @@ class OllamaChatConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/ollama/completion/transformation.py b/litellm/llms/ollama/completion/transformation.py
index 3eb2c833094..0fc1cd926b8 100644
--- a/litellm/llms/ollama/completion/transformation.py
+++ b/litellm/llms/ollama/completion/transformation.py
@@ -35,9 +35,8 @@ from litellm.types.utils import (
from ..common_utils import OllamaError, OllamaModelInfo, _convert_image
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -252,7 +251,7 @@ class OllamaConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/oobabooga/chat/transformation.py b/litellm/llms/oobabooga/chat/transformation.py
index 43d627102b6..05383a35389 100644
--- a/litellm/llms/oobabooga/chat/transformation.py
+++ b/litellm/llms/oobabooga/chat/transformation.py
@@ -11,9 +11,8 @@ from litellm.types.utils import ModelResponse, Usage
from ..common_utils import OobaboogaError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -39,7 +38,7 @@ class OobaboogaConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.py
index 9dbcf0cc089..b63684db782 100644
--- a/litellm/llms/openai/chat/gpt_transformation.py
+++ b/litellm/llms/openai/chat/gpt_transformation.py
@@ -60,9 +60,8 @@ from litellm.utils import convert_to_model_response_object
from ..common_utils import OpenAIError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.base_llm.base_utils import BaseTokenCounter
from litellm.types.llms.openai import ChatCompletionToolParam
@@ -671,7 +670,7 @@ class OpenAIGPTConfig(BaseLLMModelInfo, BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/openai/image_generation/dall_e_2_transformation.py b/litellm/llms/openai/image_generation/dall_e_2_transformation.py
index 74936cf1895..ffc6f1d5fe9 100644
--- a/litellm/llms/openai/image_generation/dall_e_2_transformation.py
+++ b/litellm/llms/openai/image_generation/dall_e_2_transformation.py
@@ -10,9 +10,10 @@ from litellm.types.utils import ImageResponse
from litellm.utils import convert_to_model_response_object
if TYPE_CHECKING:
- import tiktoken
from litellm.litellm_core_utils.logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
+
class DallE2ImageGenerationConfig(BaseImageGenerationConfig):
"""
@@ -52,7 +53,7 @@ class DallE2ImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/openai/image_generation/dall_e_3_transformation.py b/litellm/llms/openai/image_generation/dall_e_3_transformation.py
index 5c561d011a9..90b7eaedf2f 100644
--- a/litellm/llms/openai/image_generation/dall_e_3_transformation.py
+++ b/litellm/llms/openai/image_generation/dall_e_3_transformation.py
@@ -10,9 +10,10 @@ from litellm.types.utils import ImageResponse
from litellm.utils import convert_to_model_response_object
if TYPE_CHECKING:
- import tiktoken
from litellm.litellm_core_utils.logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
+
class DallE3ImageGenerationConfig(BaseImageGenerationConfig):
"""
@@ -52,7 +53,7 @@ class DallE3ImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/openai/image_generation/gpt_transformation.py b/litellm/llms/openai/image_generation/gpt_transformation.py
index 8dc4d8953ea..c3a826616ed 100644
--- a/litellm/llms/openai/image_generation/gpt_transformation.py
+++ b/litellm/llms/openai/image_generation/gpt_transformation.py
@@ -10,9 +10,10 @@ from litellm.types.utils import ImageResponse
from litellm.utils import convert_to_model_response_object
if TYPE_CHECKING:
- import tiktoken
from litellm.litellm_core_utils.logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
+
class GPTImageGenerationConfig(BaseImageGenerationConfig):
"""
@@ -61,7 +62,7 @@ class GPTImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/openai/image_variations/transformation.py b/litellm/llms/openai/image_variations/transformation.py
index afd2909b697..73b44d5ea6a 100644
--- a/litellm/llms/openai/image_variations/transformation.py
+++ b/litellm/llms/openai/image_variations/transformation.py
@@ -12,7 +12,7 @@ from ...base_llm.image_variations.transformation import BaseImageVariationConfig
from ..common_utils import OpenAIError
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class OpenAIImageVariationConfig(BaseImageVariationConfig):
@@ -53,7 +53,7 @@ class OpenAIImageVariationConfig(BaseImageVariationConfig):
image: FileTypes,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
) -> ImageResponse:
return model_response
@@ -68,7 +68,7 @@ class OpenAIImageVariationConfig(BaseImageVariationConfig):
image: FileTypes,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
) -> ImageResponse:
return model_response
diff --git a/litellm/llms/openai/openai.py b/litellm/llms/openai/openai.py
index 869ad387c5a..7ac0d988074 100644
--- a/litellm/llms/openai/openai.py
+++ b/litellm/llms/openai/openai.py
@@ -6,9 +6,10 @@ from typing import TYPE_CHECKING, Final, Literal, Optional, cast
import httpx
if TYPE_CHECKING:
- import tiktoken
from aiohttp import ClientSession
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
+
import openai
from openai import AsyncOpenAI, OpenAI
from openai._base_client import make_request_options
@@ -277,7 +278,7 @@ class OpenAIConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/openai_like/chat/transformation.py b/litellm/llms/openai_like/chat/transformation.py
index 030710c8b2d..e5d6cbb7e5e 100644
--- a/litellm/llms/openai_like/chat/transformation.py
+++ b/litellm/llms/openai_like/chat/transformation.py
@@ -13,9 +13,8 @@ from litellm.types.utils import ModelResponse
from ...openai.chat.gpt_transformation import OpenAIGPTConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -131,7 +130,7 @@ class OpenAILikeChatConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/openrouter/chat/transformation.py b/litellm/llms/openrouter/chat/transformation.py
index 77a902149d9..08d43c169f4 100644
--- a/litellm/llms/openrouter/chat/transformation.py
+++ b/litellm/llms/openrouter/chat/transformation.py
@@ -23,9 +23,8 @@ from ...openai.chat.gpt_transformation import OpenAIGPTConfig
from ..common_utils import OpenRouterException
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class CacheControlSupportedModels(str, Enum):
@@ -182,7 +181,7 @@ class OpenrouterConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/openrouter/image_generation/transformation.py b/litellm/llms/openrouter/image_generation/transformation.py
index 6bbda324336..67d90d027ec 100644
--- a/litellm/llms/openrouter/image_generation/transformation.py
+++ b/litellm/llms/openrouter/image_generation/transformation.py
@@ -50,9 +50,8 @@ from litellm.types.utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
else:
LiteLLMLoggingObj = Any
@@ -319,7 +318,7 @@ class OpenRouterImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/perplexity/chat/transformation.py b/litellm/llms/perplexity/chat/transformation.py
index 354f7692fd5..dca2f9857b8 100644
--- a/litellm/llms/perplexity/chat/transformation.py
+++ b/litellm/llms/perplexity/chat/transformation.py
@@ -15,7 +15,7 @@ from litellm.types.llms.openai import AllMessageValues, ChatCompletionAnnotation
from litellm.types.utils import ModelResponse, PromptTokensDetailsWrapper, Usage
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class PerplexityChatConfig(OpenAIGPTConfig):
@@ -75,7 +75,7 @@ class PerplexityChatConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/petals/completion/transformation.py b/litellm/llms/petals/completion/transformation.py
index 3e0de14a7b2..ee20c2b12d5 100644
--- a/litellm/llms/petals/completion/transformation.py
+++ b/litellm/llms/petals/completion/transformation.py
@@ -14,7 +14,7 @@ from litellm.types.utils import ModelResponse
from ..common_utils import PetalsError
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class PetalsConfig(BaseConfig):
@@ -112,7 +112,7 @@ class PetalsConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/predibase/chat/transformation.py b/litellm/llms/predibase/chat/transformation.py
index 2a63c489395..69924396a1e 100644
--- a/litellm/llms/predibase/chat/transformation.py
+++ b/litellm/llms/predibase/chat/transformation.py
@@ -18,9 +18,8 @@ from litellm.types.utils import Choices, Message, ModelResponse, Usage
from ..common_utils import PredibaseError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -150,7 +149,7 @@ class PredibaseConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/recraft/image_generation/transformation.py b/litellm/llms/recraft/image_generation/transformation.py
index 3a04e0a62b4..f65bf1e7292 100644
--- a/litellm/llms/recraft/image_generation/transformation.py
+++ b/litellm/llms/recraft/image_generation/transformation.py
@@ -14,9 +14,8 @@ from litellm.types.llms.recraft import RecraftImageGenerationRequestParams
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -122,7 +121,7 @@ class RecraftImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/replicate/chat/transformation.py b/litellm/llms/replicate/chat/transformation.py
index 769160c6ced..f7e09b7bec0 100644
--- a/litellm/llms/replicate/chat/transformation.py
+++ b/litellm/llms/replicate/chat/transformation.py
@@ -19,9 +19,8 @@ from litellm.utils import token_counter
from ..common_utils import ReplicateError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LoggingClass = LiteLLMLoggingObj
else:
@@ -237,7 +236,7 @@ class ReplicateConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/runwayml/image_generation/transformation.py b/litellm/llms/runwayml/image_generation/transformation.py
index 5913709c8a0..e5e988328d8 100644
--- a/litellm/llms/runwayml/image_generation/transformation.py
+++ b/litellm/llms/runwayml/image_generation/transformation.py
@@ -22,9 +22,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -308,7 +307,7 @@ class RunwayMLImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
@@ -383,7 +382,7 @@ class RunwayMLImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/sagemaker/completion/transformation.py b/litellm/llms/sagemaker/completion/transformation.py
index 576018f0046..e1bc496a82d 100644
--- a/litellm/llms/sagemaker/completion/transformation.py
+++ b/litellm/llms/sagemaker/completion/transformation.py
@@ -24,9 +24,8 @@ from litellm.utils import token_counter
from ..common_utils import SagemakerError
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -198,7 +197,7 @@ class SagemakerConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/sap/chat/transformation.py b/litellm/llms/sap/chat/transformation.py
index d64d7a57281..4c73ccacc16 100755
--- a/litellm/llms/sap/chat/transformation.py
+++ b/litellm/llms/sap/chat/transformation.py
@@ -15,9 +15,8 @@ from litellm.types.utils import ModelResponse
from ...openai.chat.gpt_transformation import OpenAIGPTConfig
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -57,7 +56,7 @@ def validate_dict(data: dict, model) -> dict:
return model(**data).model_dump(by_alias=True, exclude_unset=True)
-def _messages_to_sap_template(messages: list[dict[str, str]]) -> list:
+def _messages_to_sap_template(messages: list[AllMessageValues]) -> list:
template: Final = []
for message in messages:
if message["role"] == "user":
@@ -311,7 +310,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig):
def transform_request(
self,
model: str,
- messages: list[dict[str, str]],
+ messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
headers: dict,
@@ -383,7 +382,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/stability/image_generation/transformation.py b/litellm/llms/stability/image_generation/transformation.py
index cf3576a9404..656ffe395c8 100644
--- a/litellm/llms/stability/image_generation/transformation.py
+++ b/litellm/llms/stability/image_generation/transformation.py
@@ -26,9 +26,8 @@ from litellm.types.llms.stability import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -207,7 +206,7 @@ class StabilityImageGenerationConfig(BaseImageGenerationConfig):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/topaz/image_variations/transformation.py b/litellm/llms/topaz/image_variations/transformation.py
index f4753c8ba17..94f60d29cb9 100644
--- a/litellm/llms/topaz/image_variations/transformation.py
+++ b/litellm/llms/topaz/image_variations/transformation.py
@@ -23,7 +23,7 @@ from ...base_llm.image_variations.transformation import BaseImageVariationConfig
from ..common_utils import TopazException, TopazModelInfo
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class TopazImageVariationConfig(TopazModelInfo, BaseImageVariationConfig):
@@ -139,7 +139,7 @@ class TopazImageVariationConfig(TopazModelInfo, BaseImageVariationConfig):
image: FileTypes,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
) -> ImageResponse:
image_content: Final = await raw_response.read()
@@ -158,7 +158,7 @@ class TopazImageVariationConfig(TopazModelInfo, BaseImageVariationConfig):
image: FileTypes,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
) -> ImageResponse:
image_content: Final = raw_response.content
diff --git a/litellm/llms/triton/completion/transformation.py b/litellm/llms/triton/completion/transformation.py
index 3c868b3a96f..b37bbd78f2b 100644
--- a/litellm/llms/triton/completion/transformation.py
+++ b/litellm/llms/triton/completion/transformation.py
@@ -29,7 +29,7 @@ from litellm.types.utils import (
from ..common_utils import TritonError
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class TritonConfig(BaseConfig):
@@ -95,7 +95,7 @@ class TritonConfig(BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
@@ -215,7 +215,7 @@ class TritonGenerateConfig(TritonConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
@@ -280,7 +280,7 @@ class TritonInferConfig(TritonConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/vertex_ai/agent_engine/transformation.py b/litellm/llms/vertex_ai/agent_engine/transformation.py
index c5ca9f38144..b37bf473731 100644
--- a/litellm/llms/vertex_ai/agent_engine/transformation.py
+++ b/litellm/llms/vertex_ai/agent_engine/transformation.py
@@ -29,9 +29,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import Choices, Message, ModelResponse, Usage
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.utils import CustomStreamWrapper
@@ -285,7 +284,7 @@ class VertexAgentEngineConfig(BaseConfig, VertexBase):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py b/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py
index b2c52c53580..17ccf16837b 100644
--- a/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py
+++ b/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py
@@ -24,9 +24,8 @@ from litellm.types.utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -284,7 +283,7 @@ class VertexAIGeminiImageGenerationConfig(BaseImageGenerationConfig, VertexLLM):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/vertex_ai/image_generation/vertex_imagen_transformation.py b/litellm/llms/vertex_ai/image_generation/vertex_imagen_transformation.py
index 8faf7b0d484..ae6e08611ae 100644
--- a/litellm/llms/vertex_ai/image_generation/vertex_imagen_transformation.py
+++ b/litellm/llms/vertex_ai/image_generation/vertex_imagen_transformation.py
@@ -20,9 +20,8 @@ from litellm.types.llms.openai import (
from litellm.types.utils import ImageObject, ImageResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -214,7 +213,7 @@ class VertexAIImagenImageGenerationConfig(BaseImageGenerationConfig, VertexLLM):
request_data: dict,
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py
index 508f68b3eca..2fab6f438f6 100644
--- a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py
+++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py
@@ -14,7 +14,7 @@ from ....anthropic.chat.transformation import AnthropicConfig
from .output_params_utils import sanitize_vertex_anthropic_output_params
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class VertexAIError(Exception):
@@ -197,7 +197,7 @@ class VertexAIAnthropicConfig(AnthropicConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/llama3/transformation.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/llama3/transformation.py
index 89a5b8a570e..f2d2c0896d2 100644
--- a/litellm/llms/vertex_ai/vertex_ai_partner_models/llama3/transformation.py
+++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/llama3/transformation.py
@@ -21,7 +21,7 @@ from litellm.types.utils import (
from ...common_utils import VertexAIError
if TYPE_CHECKING:
- import tiktoken
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
class VertexAILlama3Config(OpenAIGPTConfig):
@@ -112,7 +112,7 @@ class VertexAILlama3Config(OpenAIGPTConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/llms/vertex_ai/vertex_gemma_models/transformation.py b/litellm/llms/vertex_ai/vertex_gemma_models/transformation.py
index 67b01c2dc43..2ae8b4cd188 100644
--- a/litellm/llms/vertex_ai/vertex_gemma_models/transformation.py
+++ b/litellm/llms/vertex_ai/vertex_gemma_models/transformation.py
@@ -24,9 +24,8 @@ from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import ModelResponse
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.base_llm.base_model_iterator import MockResponseIterator
@@ -276,7 +275,7 @@ class VertexGemmaConfig(OpenAIGPTConfig):
litellm_params: dict,
client: HTTPHandler | httpx.Client | None = None,
timeout: float | httpx.Timeout | None = None,
- encoding: "tiktoken.Encoding | None" = None,
+ encoding: "Tokenizer | None" = None,
):
"""Synchronous completion request"""
from litellm.utils import convert_to_model_response_object
@@ -366,7 +365,7 @@ class VertexGemmaConfig(OpenAIGPTConfig):
litellm_params: dict,
client: AsyncHTTPHandler | httpx.AsyncClient | None = None,
timeout: float | httpx.Timeout | None = None,
- encoding: "tiktoken.Encoding | None" = None,
+ encoding: "Tokenizer | None" = None,
):
"""Asynchronous completion request"""
from litellm.utils import convert_to_model_response_object
diff --git a/litellm/llms/watsonx/completion/transformation.py b/litellm/llms/watsonx/completion/transformation.py
index 2be007336b4..2fec8485cf9 100644
--- a/litellm/llms/watsonx/completion/transformation.py
+++ b/litellm/llms/watsonx/completion/transformation.py
@@ -21,9 +21,8 @@ from ..common_utils import (
)
if TYPE_CHECKING:
- import tiktoken
-
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
+ from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@@ -280,7 +279,7 @@ class IBMWatsonXAIConfig(IBMWatsonXMixin, BaseConfig):
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
- encoding: "tiktoken.Encoding | None",
+ encoding: "Tokenizer | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
diff --git a/litellm/main.py b/litellm/main.py
index 2570b93455f..7d231a1bf7a 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -37,7 +37,6 @@ if TYPE_CHECKING:
import dotenv
import httpx
import openai
-import tiktoken
from pydantic import BaseModel
from typing_extensions import overload
@@ -100,6 +99,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (
from litellm.litellm_core_utils.request_timeout_resolver import (
get_configured_request_timeout,
)
+from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer
from litellm.llms.azure_ai.common_utils import (
azure_ai_supports_native_responses,
foundry_chat_rejects_function_tools_while_reasoning,
@@ -7489,7 +7489,9 @@ def text_completion(
if isinstance(prompt, list):
import concurrent.futures
- tokenizer: Final = tiktoken.encoding_for_model("text-davinci-003")
+ from litellm.rust_bridge.tokenizer import get_encoding
+
+ tokenizer: Final = get_encoding("p50k_base")
## if it's a 2d list - each element in the list is a text_completion() request
if len(prompt) > 0 and isinstance(prompt[0], list):
responses: Final = [None for x in prompt] # init responses
@@ -9259,7 +9261,7 @@ async def acount_tokens(
except Exception as e:
verbose_logger.debug("Provider token counting failed for model=%s, falling back to local: %s", model, e)
- # Fallback to local tiktoken-based token counting
+ # Fallback to local token counting
fallback_messages = messages or []
if system and fallback_messages:
fallback_messages = [{"role": "system", "content": system}] + fallback_messages
@@ -9278,16 +9280,16 @@ async def acount_tokens(
# Cache for encoding to avoid repeated __getattr__ calls
-_encoding_cache: tiktoken.Encoding | None = None
+_encoding_cache: Tokenizer | None = None
-def _load_module_encoding() -> tiktoken.Encoding:
+def _load_module_encoding() -> Tokenizer:
import sys
return sys.modules[__name__].encoding
-def _get_encoding() -> tiktoken.Encoding:
+def _get_encoding() -> Tokenizer:
"""Get encoding, loading it lazily if needed."""
global _encoding_cache
if _encoding_cache is None:
@@ -9296,18 +9298,15 @@ def _get_encoding() -> tiktoken.Encoding:
return _encoding_cache
-def _load_default_encoding() -> tiktoken.Encoding:
+def _load_default_encoding() -> Tokenizer:
from litellm._lazy_imports import _get_default_encoding
return _get_default_encoding()
-def __getattr__(name: str) -> tiktoken.Encoding:
+def __getattr__(name: str) -> Tokenizer:
"""Lazy import handler for main module"""
if name == "encoding":
- # Use _get_default_encoding which properly sets TIKTOKEN_CACHE_DIR
- # before loading tiktoken, ensuring the local cache is used
- # instead of downloading from the internet
_encoding: Final = _load_default_encoding()
# Cache it in the module's __dict__ for subsequent accesses
import sys
diff --git a/litellm/proxy/spend_tracking/budget_reservation.py b/litellm/proxy/spend_tracking/budget_reservation.py
index ac9b07a55f7..e28fa2c06a4 100644
--- a/litellm/proxy/spend_tracking/budget_reservation.py
+++ b/litellm/proxy/spend_tracking/budget_reservation.py
@@ -36,10 +36,10 @@ from litellm.proxy.common_utils.user_api_key_cache import (
tag_cache_key,
team_membership_reservation_cache_key,
)
+from litellm.proxy.spend_tracking.input_tokens import count_input_tokens, count_input_tokens_for_model
from litellm.proxy.spend_tracking.spend_counter_batch import PendingSpendIncrement, spend_counter_batch_scope
from litellm.proxy.utils import PrismaClient, ProxyLogging
from litellm.router import Router
-from litellm.rust_bridge.token_counter import RustTokenizer, count_input_tokens, rust_tokenizer
from litellm.types.proxy.model_access_group_budget import ModelAccessGroupBudget
from litellm.types.router import DeploymentTypedDict
@@ -1489,9 +1489,6 @@ def _get_request_models(
return (model,) if isinstance(model, str) else tuple(model)
-TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS: Final = 30_000
-
-
async def count_request_input_tokens(
request_body: dict,
route: str,
@@ -1500,105 +1497,11 @@ async def count_request_input_tokens(
) -> Mapping[str, int]:
"""Input-token count per candidate model, counted once per request.
- Tokenizing is the reservation path's dominant CPU cost and is O(prompt), so
- counting a large prompt inline stalls every other request on the worker.
- Models whose tokenizer the Rust bridge ports (Anthropic, tiktoken cl100k_base
- and o200k_base) are counted from the raw body by the bridge when it is enabled, once per
- distinct tokenizer, which parses and tokenizes with the GIL released.
- Everything it declines is counted in Python, large prompts in a worker
- thread. The counts are reused by both the max-cost and the input-cost
- estimate.
- """
+ The counts are reused by both the max-cost and the input-cost estimate."""
models: Final = _get_request_models(request_body=request_body, route=route, llm_router=llm_router)
if not models:
return MappingProxyType({})
- tokenizers: Final[Mapping[str, RustTokenizer | None]] = MappingProxyType(
- {model: rust_tokenizer(model) for model in models}
- )
- distinct_tokenizers: Final[tuple[RustTokenizer, ...]] = tuple(
- dict.fromkeys(tokenizer for tokenizer in tokenizers.values() if tokenizer is not None)
- )
- rust_counts_by_tokenizer: Final[Mapping[RustTokenizer, int]] = MappingProxyType(
- {
- tokenizer: count.input_tokens
- for tokenizer in distinct_tokenizers
- if raw_body is not None and (count := await count_input_tokens(raw_body, tokenizer)) is not None
- }
- )
- rust_counts: Final = MappingProxyType(
- {
- model: rust_counts_by_tokenizer[tokenizer]
- for model, tokenizer in tokenizers.items()
- if tokenizer is not None and tokenizer in rust_counts_by_tokenizer
- }
- )
- python_models: Final = tuple(model for model in models if model not in rust_counts)
- python_counts: Final = (
- MappingProxyType({})
- if not python_models
- else _count_input_tokens_for_models(request_body=request_body, models=python_models)
- if _approximate_input_size(request_body) < TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS
- else await asyncio.to_thread(
- _count_input_tokens_for_models,
- request_body=request_body,
- models=python_models,
- )
- )
- verbose_proxy_logger.debug("input token counts: rust=%s python=%s", dict(rust_counts), dict(python_counts))
- return MappingProxyType({**rust_counts, **python_counts})
-
-
-def _count_input_tokens_for_models(
- request_body: dict,
- models: Sequence[str],
-) -> Mapping[str, int]:
- return MappingProxyType(
- {
- model: tokens
- for model in models
- if (tokens := _count_input_tokens(request_body=request_body, model=model)) is not None
- }
- )
-
-
-_INPUT_SIZE_FIELDS: Final = ("messages", "prompt", "input", "query", "documents", "tools", "tool_choice")
-
-
-def _approximate_input_size(request_body: Mapping[str, object]) -> int:
- """Length of the request's input text, a cheap stand-in for tokenizing cost.
-
- Every field _count_input_tokens hands the tokenizer is sized here, and
- rendering rather than walking keeps mapping keys in the total, which a tool
- schema's property names are."""
- return sum(len(str(request_body.get(field, ""))) for field in _INPUT_SIZE_FIELDS)
-
-
-def _count_input_tokens(request_body: dict, model: str) -> int | None:
- try:
- if "messages" in request_body:
- try:
- return litellm.token_counter(
- model=model,
- messages=request_body.get("messages") or (),
- tools=request_body.get("tools"),
- tool_choice=request_body.get("tool_choice"),
- )
- except ValueError:
- return _count_text_tokens(model=model, text=request_body.get("messages"))
- if "prompt" in request_body:
- return _count_text_tokens(model=model, text=request_body.get("prompt"))
- if "input" in request_body:
- return _count_text_tokens(model=model, text=request_body.get("input"))
- if "query" in request_body or "documents" in request_body:
- query_tokens: Final = _count_text_tokens(model=model, text=request_body.get("query"))
- document_tokens: Final = _count_text_tokens(
- model=model,
- text=request_body.get("documents"),
- )
- return query_tokens + document_tokens
- except Exception:
- verbose_proxy_logger.debug("Unable to count input tokens for budget reservation", exc_info=True)
- return None
+ return await count_input_tokens(request_body=request_body, raw_body=raw_body, models=models)
def _estimate_input_tokens(
@@ -1609,7 +1512,9 @@ def _estimate_input_tokens(
input_tokens: int | None = None,
) -> int | None:
counted: Final = (
- input_tokens if input_tokens is not None else _count_input_tokens(request_body=request_body, model=model)
+ input_tokens
+ if input_tokens is not None
+ else count_input_tokens_for_model(request_body=request_body, model=model)
)
if counted is not None:
return counted
@@ -1658,26 +1563,6 @@ def _requested_output_tokens(request_body: Mapping[str, object]) -> int | None:
return next((tokens for tokens in map(_to_int, candidates) if tokens is not None), None)
-def _count_text_tokens(model: str, text: object) -> int:
- if text is None:
- return 0
-
- token_count = 0
- stack: Final = [text]
- while stack:
- item = stack.pop()
- if item is None:
- continue
- if isinstance(item, list):
- stack.extend(item)
- continue
- if isinstance(item, dict):
- token_count += litellm.token_counter(model=model, text=json.dumps(item))
- continue
- token_count += litellm.token_counter(model=model, text=str(item))
- return token_count
-
-
def _get_output_multiplier(request_body: dict) -> int:
output_multiplier = 1
for key in ("n", "best_of"):
diff --git a/litellm/proxy/spend_tracking/input_tokens.py b/litellm/proxy/spend_tracking/input_tokens.py
new file mode 100644
index 00000000000..6c7083fb6db
--- /dev/null
+++ b/litellm/proxy/spend_tracking/input_tokens.py
@@ -0,0 +1,173 @@
+"""Input-token counting for the budget reservation path.
+
+Tokenizing is the reservation path's dominant CPU cost and is O(prompt), so
+counting a large prompt inline stalls every other request on the worker.
+Models whose tokenizer the Rust bridge ports (Anthropic, tiktoken cl100k_base
+and o200k_base) are counted from the raw body by the bridge, once per distinct
+tokenizer, which parses and tokenizes with the GIL released. Everything it
+declines, and every model with no Rust tokenizer, is counted in Python, large
+prompts in a worker thread.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+from collections.abc import Mapping, Sequence
+from types import MappingProxyType
+from typing import Final
+
+import litellm
+from litellm._logging import verbose_proxy_logger
+from litellm.rust_bridge import runtime
+from litellm.rust_bridge.catalog import Route, RouteContext
+from litellm.rust_bridge.token_counter import (
+ TOKEN_COUNTER,
+ RustTokenCounterFactory,
+ RustTokenizer,
+ native_count,
+ rust_tokenizer,
+)
+
+TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS: Final = 30_000
+
+_INPUT_SIZE_FIELDS: Final = ("messages", "prompt", "input", "query", "documents", "tools", "tool_choice")
+
+
+def _approximate_input_size(request_body: Mapping[str, object]) -> int:
+ """Length of the request's input text, a cheap stand-in for tokenizing cost.
+
+ Every field count_input_tokens_for_model hands the tokenizer is sized here,
+ and rendering rather than walking keeps mapping keys in the total, which a
+ tool schema's property names are."""
+ return sum(len(str(request_body.get(field, ""))) for field in _INPUT_SIZE_FIELDS)
+
+
+async def count_input_tokens(
+ request_body: dict,
+ raw_body: bytes | None,
+ models: Sequence[str],
+) -> Mapping[str, int]:
+ """Input-token count per model, sharing one native count across models that
+ select the same tokenizer."""
+ tokenizers: Final[tuple[tuple[str, RustTokenizer | None], ...]] = tuple(
+ (model, rust_tokenizer(model)) for model in models
+ )
+ groups: Final[tuple[RustTokenizer | None, ...]] = tuple(dict.fromkeys(tokenizer for _, tokenizer in tokenizers))
+ group_counts: Final = [
+ await _count_group(
+ request_body=request_body,
+ raw_body=raw_body,
+ tokenizer=tokenizer,
+ models=tuple(model for model, selected in tokenizers if selected == tokenizer),
+ )
+ for tokenizer in groups
+ ]
+ counts: Final = MappingProxyType({model: tokens for group in group_counts for model, tokens in group.items()})
+ verbose_proxy_logger.debug("input token counts: %s", dict(counts))
+ return counts
+
+
+async def _count_group(
+ request_body: dict,
+ raw_body: bytes | None,
+ tokenizer: RustTokenizer | None,
+ models: tuple[str, ...],
+) -> Mapping[str, int]:
+ async def python() -> Mapping[str, int]:
+ if _approximate_input_size(request_body) < TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS:
+ return _count_input_tokens_for_models(request_body=request_body, models=models)
+ return await asyncio.to_thread(
+ _count_input_tokens_for_models,
+ request_body=request_body,
+ models=models,
+ )
+
+ if tokenizer is None or raw_body is None:
+ return await python()
+ try:
+ return await runtime.arun(
+ RouteContext(Route.TOKEN_COUNTER, provider=tokenizer),
+ binding=TOKEN_COUNTER,
+ native=lambda factory: _native_counts(factory, tokenizer, raw_body, models),
+ python=python,
+ )
+ except (RuntimeError, ValueError) as error:
+ from litellm.rust_bridge.fork_guard import ForkedAfterNativeRuntimeStarted, ProcessReservedForForking
+
+ if isinstance(error, (ForkedAfterNativeRuntimeStarted, ProcessReservedForForking)):
+ raise
+ verbose_proxy_logger.debug("Rust token counter (%s) failed, counting in Python: %s", tokenizer, error)
+ return await python()
+
+
+async def _native_counts(
+ factory: RustTokenCounterFactory,
+ tokenizer: RustTokenizer,
+ raw_body: bytes,
+ models: tuple[str, ...],
+) -> Mapping[str, int]:
+ count: Final = await native_count(factory, tokenizer, raw_body)
+ verbose_proxy_logger.debug("Rust token counter (%s) counted %d input tokens", tokenizer, count.input_tokens)
+ return MappingProxyType({model: count.input_tokens for model in models})
+
+
+def _count_input_tokens_for_models(
+ request_body: dict,
+ models: Sequence[str],
+) -> Mapping[str, int]:
+ return MappingProxyType(
+ {
+ model: tokens
+ for model in models
+ if (tokens := count_input_tokens_for_model(request_body=request_body, model=model)) is not None
+ }
+ )
+
+
+def count_input_tokens_for_model(request_body: dict, model: str) -> int | None:
+ try:
+ if "messages" in request_body:
+ try:
+ return litellm.token_counter(
+ model=model,
+ messages=request_body.get("messages") or (),
+ tools=request_body.get("tools"),
+ tool_choice=request_body.get("tool_choice"),
+ )
+ except ValueError:
+ return _count_text_tokens(model=model, text=request_body.get("messages"))
+ if "prompt" in request_body:
+ return _count_text_tokens(model=model, text=request_body.get("prompt"))
+ if "input" in request_body:
+ return _count_text_tokens(model=model, text=request_body.get("input"))
+ if "query" in request_body or "documents" in request_body:
+ query_tokens: Final = _count_text_tokens(model=model, text=request_body.get("query"))
+ document_tokens: Final = _count_text_tokens(
+ model=model,
+ text=request_body.get("documents"),
+ )
+ return query_tokens + document_tokens
+ except Exception:
+ verbose_proxy_logger.debug("Unable to count input tokens for budget reservation", exc_info=True)
+ return None
+
+
+def _count_text_tokens(model: str, text: object) -> int:
+ if text is None:
+ return 0
+
+ token_count = 0
+ stack: Final = [text]
+ while stack:
+ item = stack.pop()
+ if item is None:
+ continue
+ if isinstance(item, list):
+ stack.extend(item)
+ continue
+ if isinstance(item, dict):
+ token_count += litellm.token_counter(model=model, text=json.dumps(item))
+ continue
+ token_count += litellm.token_counter(model=model, text=str(item))
+ return token_count
diff --git a/litellm/rust_bridge/_native.pyi b/litellm/rust_bridge/_native.pyi
index a033b89a6a8..61e597bf674 100644
--- a/litellm/rust_bridge/_native.pyi
+++ b/litellm/rust_bridge/_native.pyi
@@ -224,14 +224,107 @@ class _CacheTestResolver:
@final
class TokenCounter:
- def __new__(cls, tokenizer_json: str) -> TokenCounter: ...
@staticmethod
- def from_cl100k_ranks(rank_file: str) -> TokenCounter: ...
+ def from_tokenizer(tokenizer: Tokenizer, fast: bool = False) -> TokenCounter: ...
+ def acount_request(self, body: bytes) -> Future[dict[str, object]]: ...
+
+@final
+class Tokenizer:
@staticmethod
- def from_o200k_ranks(rank_file: str) -> TokenCounter: ...
+ def from_tiktoken(encoding: str) -> Tokenizer: ...
@staticmethod
- def from_tiktoken(encoding: str) -> TokenCounter: ...
- def acount_request(self, body: bytes) -> Future[dict[str, object]]: ...
+ def from_json(tokenizer_json: str) -> Tokenizer: ...
+ @staticmethod
+ def from_pretrained(
+ identifier: str,
+ revision: str = "main",
+ token: str | None = None,
+ ) -> Tokenizer: ...
+ def encode(self, text: str) -> list[int]: ...
+ def decode(self, ids: Sequence[int], skip_special_tokens: bool = True) -> str: ...
+ def count(self, text: str, fast: bool = False) -> int: ...
+ # tiktoken encodings
+ def encode_special(self, text: str, allowed: Sequence[str]) -> list[int]: ...
+ def encode_with_unstable(self, text: str, allowed: Sequence[str]) -> tuple[list[int], list[list[int]]]: ...
+ def encode_single_token(self, piece: bytes) -> int: ...
+ def special_tokens(self) -> dict[str, int]: ...
+ def max_token_value(self) -> int: ...
+ def is_special_token(self, token: int) -> bool: ...
+ def token_byte_values(self) -> list[bytes]: ...
+ def decode_bytes(self, ids: Sequence[int]) -> bytes: ...
+ # Hugging Face tokenizers
+ def to_json(self, pretty: bool = False) -> str: ...
+ def token_to_id(self, token: str) -> int | None: ...
+ def id_to_token(self, id: int) -> str | None: ...
+ def get_vocab(self, with_added_tokens: bool = True) -> dict[str, int]: ...
+ def get_vocab_size(self, with_added_tokens: bool = True) -> int: ...
+ def added_tokens_decoder(self) -> list[tuple[int, tuple[str, bool, bool, bool, bool, bool]]]: ...
+ def padding(self) -> dict[str, object] | None: ...
+ def truncation(self) -> dict[str, object] | None: ...
+ def num_special_tokens_to_add(self, is_pair: bool) -> int: ...
+ def encode_special_tokens(self) -> bool: ...
+ def encode_huggingface(
+ self,
+ sequence: str | Sequence[str],
+ pair: str | Sequence[str] | None = None,
+ is_pretokenized: bool = False,
+ add_special_tokens: bool = True,
+ fast: bool = False,
+ ) -> HuggingFaceEncoding: ...
+ def encode_batch_huggingface(
+ self,
+ inputs: Sequence[tuple[str | Sequence[str], str | Sequence[str] | None]],
+ is_pretokenized: bool = False,
+ add_special_tokens: bool = True,
+ fast: bool = False,
+ ) -> list[HuggingFaceEncoding]: ...
+ @property
+ def name(self) -> str: ...
+
+@final
+class HuggingFaceEncoding:
+ def __new__(cls, json: str | None = None) -> HuggingFaceEncoding: ...
+ @staticmethod
+ def merge(encodings: Sequence[HuggingFaceEncoding], growing_offsets: bool = True) -> HuggingFaceEncoding: ...
+ def __len__(self) -> int: ...
+ def __reduce__(self) -> tuple[type[HuggingFaceEncoding], tuple[str]]: ...
+ def word_to_tokens(self, word_index: int, sequence_index: int = 0) -> tuple[int, int] | None: ...
+ def word_to_chars(self, word_index: int, sequence_index: int = 0) -> tuple[int, int] | None: ...
+ def token_to_sequence(self, token_index: int) -> int | None: ...
+ def token_to_chars(self, token_index: int) -> tuple[int, int] | None: ...
+ def token_to_word(self, token_index: int) -> int | None: ...
+ def char_to_token(self, char_pos: int, sequence_index: int = 0) -> int | None: ...
+ def char_to_word(self, char_pos: int, sequence_index: int = 0) -> int | None: ...
+ def set_sequence_id(self, sequence_id: int) -> None: ...
+ def pad(
+ self,
+ length: int,
+ direction: str = "right",
+ pad_id: int = 0,
+ pad_type_id: int = 0,
+ pad_token: str = "[PAD]",
+ ) -> None: ...
+ def truncate(self, max_length: int, stride: int = 0, direction: str = "right") -> None: ...
+ @property
+ def ids(self) -> list[int]: ...
+ @property
+ def tokens(self) -> list[str]: ...
+ @property
+ def offsets(self) -> list[tuple[int, int]]: ...
+ @property
+ def type_ids(self) -> list[int]: ...
+ @property
+ def attention_mask(self) -> list[int]: ...
+ @property
+ def special_tokens_mask(self) -> list[int]: ...
+ @property
+ def word_ids(self) -> list[int | None]: ...
+ @property
+ def sequence_ids(self) -> list[int | None]: ...
+ @property
+ def overflowing(self) -> list[HuggingFaceEncoding]: ...
+ @property
+ def n_sequences(self) -> int: ...
def gil_stats() -> dict[str, int]: ...
def process_state_started() -> bool: ...
@@ -239,11 +332,13 @@ def reserve_process_for_forking() -> None: ...
__all__ = [
"ForkedAfterNativeRuntimeStarted",
+ "HuggingFaceEncoding",
"ProcessReservedForForking",
"ResponsesWebSocketConnection",
"RustBridgeDeclined",
"RustUpstreamError",
"TokenCounter",
+ "Tokenizer",
"achat_completions",
"amessages",
"aocr",
diff --git a/litellm/rust_bridge/catalog.py b/litellm/rust_bridge/catalog.py
index 74ceb1ba123..d7479631e04 100644
--- a/litellm/rust_bridge/catalog.py
+++ b/litellm/rust_bridge/catalog.py
@@ -22,6 +22,8 @@ class Route(str, Enum):
RESPONSES = "responses"
TRANSCRIPTION = "transcription"
OCR = "ocr"
+ TOKEN_COUNTER = "token_counter"
+ TOKENIZER = "tokenizer"
class Delivery(Enum):
@@ -92,6 +94,8 @@ RULES: Final[Rules] = (
RouteRule(Route.OCR, Rollout.RUST_REQUIRED, providers=frozenset({"aws_textract"})),
RouteRule(Route.OCR, Rollout.RUST_OPT_OUT),
RouteRule(Route.MESSAGES, Rollout.RUST_OPT_IN),
+ RouteRule(Route.TOKEN_COUNTER, Rollout.RUST_OPT_IN),
+ RouteRule(Route.TOKENIZER, Rollout.RUST_OPT_IN),
RouteRule(Route.TRANSCRIPTION, Rollout.RUST_REQUIRED, providers=frozenset({"bedrock"})),
CacheRule(Rollout.PYTHON_ONLY, backends=frozenset({LiteLLMCacheType.LOCAL})),
CacheRule(Rollout.PYTHON_ONLY, backends=frozenset({LiteLLMCacheType.REDIS})),
diff --git a/litellm/rust_bridge/configuration.py b/litellm/rust_bridge/configuration.py
index cd468c80655..152ba632996 100644
--- a/litellm/rust_bridge/configuration.py
+++ b/litellm/rust_bridge/configuration.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import os
from enum import Enum, auto
+from functools import lru_cache
from typing import Final
from pydantic import TypeAdapter, ValidationError
@@ -32,7 +33,9 @@ class _RustConfiguration:
_CONFIGURATION: Final = _RustConfiguration()
+@lru_cache(maxsize=16)
def _parse_env_bool(value: str | None) -> bool | None:
+ """`LITELLM_RUST` as a bool; cached by raw value because `decision` runs per tokenizer call."""
if value is None:
return None
try:
diff --git a/litellm/rust_bridge/token_counter.py b/litellm/rust_bridge/token_counter.py
index d36234f56c1..250ad18d44c 100644
--- a/litellm/rust_bridge/token_counter.py
+++ b/litellm/rust_bridge/token_counter.py
@@ -5,18 +5,19 @@ from __future__ import annotations
from collections.abc import Awaitable
from dataclasses import dataclass
from functools import lru_cache
-from typing import Final, Literal, Protocol, cast # noqa: TID251 # native extension exposes untyped callables
+from typing import TYPE_CHECKING, Final, Literal, Protocol, cast # noqa: TID251 # PyO3 binding validation
from pydantic import TypeAdapter
+from typing_extensions import assert_never
import litellm
-from litellm._logging import verbose_logger
-from litellm.litellm_core_utils.default_encoding import cl100k_base_rank_file, o200k_base_rank_file
-from litellm.litellm_core_utils.token_counter import openai_tokenizer_encoding, uses_legacy_message_accounting
+from litellm.litellm_core_utils.token_counter import openai_tokenizer_encoding_name, uses_legacy_message_accounting
+from litellm.rust_bridge import tokenizer as tokenizer_dispatch
from litellm.rust_bridge.bindings import NativeBinding
-from litellm.rust_bridge.configuration import rust_enabled
-from litellm.rust_bridge.runtime import BridgeErrorContext, RustHandled, aattempt
-from litellm.utils import claude_json_str, huggingface_tokenizer_kind
+from litellm.utils import huggingface_tokenizer_kind
+
+if TYPE_CHECKING:
+ from litellm.rust_bridge._native import Tokenizer as NativeTokenizer
RustTokenizer = Literal["anthropic", "cl100k_base", "o200k_base"]
@@ -27,13 +28,7 @@ class RustTokenCounter(Protocol):
class RustTokenCounterFactory(Protocol):
- def __call__(self, tokenizer_json: str) -> RustTokenCounter:
- raise NotImplementedError
-
- def from_cl100k_ranks(self, rank_file: str) -> RustTokenCounter:
- raise NotImplementedError
-
- def from_o200k_ranks(self, rank_file: str) -> RustTokenCounter:
+ def from_tokenizer(self, tokenizer: NativeTokenizer, fast: bool = False) -> RustTokenCounter:
raise NotImplementedError
@@ -51,7 +46,7 @@ def _as_factory(value: object) -> RustTokenCounterFactory | None:
cast( # cast-ok: native extension protocol is runtime-defined
RustTokenCounterFactory, value
)
- if callable(value)
+ if callable(getattr(value, "from_tokenizer", None))
else None
)
@@ -73,7 +68,7 @@ def rust_tokenizer(model: str) -> RustTokenizer | None:
return "anthropic"
if kind is not None or uses_legacy_message_accounting(model):
return None
- match openai_tokenizer_encoding(model).name:
+ match openai_tokenizer_encoding_name(model):
case "cl100k_base":
return "cl100k_base"
case "o200k_base":
@@ -84,31 +79,26 @@ def rust_tokenizer(model: str) -> RustTokenizer | None:
@lru_cache(maxsize=4)
def _counter(factory: RustTokenCounterFactory, tokenizer: RustTokenizer) -> RustTokenCounter:
+ return factory.from_tokenizer(_native_tokenizer(tokenizer))
+
+
+def _native_tokenizer(tokenizer: RustTokenizer) -> NativeTokenizer:
match tokenizer:
case "anthropic":
- return factory(claude_json_str)
- case "cl100k_base":
- return factory.from_cl100k_ranks(cl100k_base_rank_file())
- case "o200k_base":
- return factory.from_o200k_ranks(o200k_base_rank_file())
+ native = tokenizer_dispatch.native_anthropic()
+ case "cl100k_base" | "o200k_base":
+ native = tokenizer_dispatch.native_encoding(tokenizer)
+ case _:
+ assert_never(tokenizer)
+ if native is None:
+ raise RuntimeError(f"native {tokenizer} tokenizer is unavailable")
+ return native
-async def count_input_tokens(body: bytes, tokenizer: RustTokenizer) -> InputTokenCount | None:
- if not rust_enabled():
- return None
- factory: Final = TOKEN_COUNTER.load()
- if factory is None:
- return None
- try:
- attempt: Final = await aattempt(
- native_call=lambda: _counter(factory, tokenizer).acount_request(body),
- adapt=_INPUT_TOKEN_COUNT.validate_python,
- context=BridgeErrorContext(route="token_counter", provider=tokenizer, model=""),
- )
- except (RuntimeError, ValueError) as error:
- verbose_logger.debug("Rust token counter (%s) failed, counting in Python: %s", tokenizer, error)
- return None
- if not isinstance(attempt, RustHandled):
- return None
- verbose_logger.debug("Rust token counter (%s) counted %d input tokens", tokenizer, attempt.value.input_tokens)
- return attempt.value
+async def native_count(factory: RustTokenCounterFactory, tokenizer: RustTokenizer, body: bytes) -> InputTokenCount:
+ """One native count, validated into the public shape.
+
+ ``RustBridgeDeclined`` and upstream errors propagate so the caller's route
+ runner can map them onto its fallback policy; other failures (RuntimeError,
+ ValueError) propagate as-is."""
+ return _INPUT_TOKEN_COUNT.validate_python(await _counter(factory, tokenizer).acount_request(body))
diff --git a/litellm/rust_bridge/tokenizer.py b/litellm/rust_bridge/tokenizer.py
new file mode 100644
index 00000000000..5ed89813620
--- /dev/null
+++ b/litellm/rust_bridge/tokenizer.py
@@ -0,0 +1,108 @@
+from __future__ import annotations
+
+from functools import lru_cache
+from typing import TYPE_CHECKING, Final, cast # noqa: TID251 # native class is validated at the binding boundary
+
+import tiktoken
+from tokenizers import Tokenizer as PythonHuggingFaceTokenizer
+
+from litellm.litellm_core_utils.tokenizer import Encoding, HuggingFace, HuggingFaceTokenizer, OpenAIEncoding
+from litellm.rust_bridge import runtime
+from litellm.rust_bridge.bindings import NativeBinding
+from litellm.rust_bridge.catalog import Route, RouteContext
+
+if TYPE_CHECKING:
+ from litellm.rust_bridge._native import Tokenizer as NativeTokenizer
+
+
+def _as_factory(value: object) -> type[NativeTokenizer] | None:
+ return (
+ cast(type["NativeTokenizer"], value) # cast-ok: PyO3 class validated at the native boundary
+ if isinstance(value, type)
+ else None
+ )
+
+
+TOKENIZER: Final = NativeBinding("Tokenizer", validate=_as_factory)
+
+# The catalog contexts the tokenizer factories dispatch on. Callers that cache a tokenizer per
+# backend key their cache on `decision(...)` of the same context, so key and dispatch agree.
+TIKTOKEN_CONTEXT: Final = RouteContext(Route.TOKENIZER, provider="tiktoken")
+HUGGINGFACE_CONTEXT: Final = RouteContext(Route.TOKENIZER, provider="huggingface")
+
+
+@lru_cache(maxsize=8)
+def _native_tiktoken(factory: type[NativeTokenizer], name: str) -> NativeTokenizer:
+ return factory.from_tiktoken(name)
+
+
+@lru_cache(maxsize=1)
+def _native_anthropic(factory: type[NativeTokenizer]) -> NativeTokenizer:
+ from litellm.utils import claude_json_str
+
+ return factory.from_json(claude_json_str)
+
+
+@lru_cache(maxsize=8)
+def _native_encoding(factory: type[NativeTokenizer], name: str) -> OpenAIEncoding:
+ return OpenAIEncoding.wrap(_native_tiktoken(factory, name))
+
+
+def native_encoding(name: str) -> NativeTokenizer | None:
+ """The native tiktoken encoding behind `get_encoding(name)`, for a Rust route that counts
+ with the same loaded model; `None` without the extension."""
+ factory: Final = TOKENIZER.load()
+ return None if factory is None else _native_tiktoken(factory, name)
+
+
+def native_anthropic() -> NativeTokenizer | None:
+ """The native packaged Anthropic tokenizer behind `anthropic()`, parsed once per process."""
+ factory: Final = TOKENIZER.load()
+ return None if factory is None else _native_anthropic(factory)
+
+
+def _python_encoding(name: str) -> tiktoken.Encoding:
+ from litellm.litellm_core_utils.default_encoding import encoding
+
+ return encoding if name == encoding.name else tiktoken.get_encoding(name)
+
+
+def get_encoding(name: str) -> Encoding:
+ return runtime.run(
+ TIKTOKEN_CONTEXT,
+ binding=TOKENIZER,
+ native=lambda factory: _native_encoding(factory, name),
+ python=lambda: _python_encoding(name),
+ )
+
+
+def anthropic() -> HuggingFace:
+ """The packaged Anthropic tokenizer on the selected backend."""
+ from litellm.utils import claude_json_str
+
+ return runtime.run(
+ HUGGINGFACE_CONTEXT,
+ binding=TOKENIZER,
+ native=lambda factory: HuggingFaceTokenizer(_native_anthropic(factory)),
+ python=lambda: PythonHuggingFaceTokenizer.from_str(claude_json_str),
+ )
+
+
+def from_str(json: str) -> HuggingFace:
+ return runtime.run(
+ HUGGINGFACE_CONTEXT,
+ binding=TOKENIZER,
+ native=lambda factory: HuggingFaceTokenizer(factory.from_json(json)),
+ python=lambda: PythonHuggingFaceTokenizer.from_str(json),
+ )
+
+
+def from_pretrained(identifier: str, revision: str = "main", token: str | None = None) -> HuggingFace:
+ return runtime.run(
+ HUGGINGFACE_CONTEXT,
+ binding=TOKENIZER,
+ native=lambda factory: HuggingFaceTokenizer(
+ factory.from_pretrained(identifier, revision=revision, token=token)
+ ),
+ python=lambda: PythonHuggingFaceTokenizer.from_pretrained(identifier, revision=revision, token=token),
+ )
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index 2e8c869b89d..65d66677b27 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -104,6 +104,8 @@ def _nested_selector(
if TYPE_CHECKING:
+ from litellm.litellm_core_utils.tokenizer import Tokenizer
+
from .vector_stores import VectorStoreSearchResponse
else:
VectorStoreSearchResponse = Any
@@ -4406,7 +4408,7 @@ class ProviderSpecificHeader(TypedDict):
class SelectTokenizerResponse(TypedDict):
type: Literal["openai_tokenizer", "huggingface_tokenizer"]
- tokenizer: Any
+ tokenizer: ReadOnly["Tokenizer"]
class LiteLLMFineTuningJob(FineTuningJob):
diff --git a/litellm/utils.py b/litellm/utils.py
index 812299560c7..e088a5988c8 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -40,14 +40,11 @@ from types import MappingProxyType
import dotenv
import httpx
import openai
-import tiktoken
from httpx import Proxy
from httpx._utils import get_environment_proxies
from openai.lib import _parsing, _pydantic
from openai.types.chat.completion_create_params import ResponseFormat
from pydantic import BaseModel
-from tiktoken import Encoding
-from tokenizers import Tokenizer
import litellm
import litellm.litellm_core_utils
@@ -91,6 +88,10 @@ from litellm.litellm_core_utils.fallback_generalizations import (
match_fill_missing_generalizations,
)
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_payload
+from litellm.litellm_core_utils.tokenizer import Encoding, HuggingFace, strip_special_tokens
+from litellm.rust_bridge import tokenizer as tokenizer_dispatch
+from litellm.rust_bridge.catalog import decision
+from litellm.rust_bridge.configuration import Decision
_CachingHandlerResponse = None
_LLMCachingHandler = None
@@ -289,6 +290,8 @@ import importlib.metadata
from collections.abc import AsyncIterator, Callable, Iterable, Iterator, Mapping, Sequence
from typing import TYPE_CHECKING, Any, Final, Literal, Optional, Union, cast, get_args
+from typing_extensions import assert_never
+
from litellm import utils as litellm_utils
# These are lazy loaded via __getattr__
@@ -2254,17 +2257,27 @@ def _select_tokenizer(model: str, custom_tokenizer: CustomHuggingfaceTokenizer |
identifier=custom_tokenizer["identifier"],
revision=custom_tokenizer["revision"],
auth_token=custom_tokenizer["auth_token"],
+ backend=_huggingface_tokenizer_backend(),
)
return _select_tokenizer_helper(model=model)
+def _huggingface_tokenizer_backend() -> Decision:
+ """The backend `tokenizer_dispatch.from_str` / `from_pretrained` will select right now.
+
+ Cached HuggingFace tokenizers are keyed on it, so flipping `LITELLM_RUST` or
+ `litellm.rust(...)` reaches a fresh object instead of the other backend's."""
+ return decision(tokenizer_dispatch.HUGGINGFACE_CONTEXT)
+
+
@lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)
-def _select_custom_tokenizer_helper(identifier: str, revision: str, auth_token: str | None) -> SelectTokenizerResponse:
+def _select_custom_tokenizer_helper(
+ identifier: str, revision: str, auth_token: str | None, backend: Decision
+) -> SelectTokenizerResponse:
verbose_logger.debug("Loading custom HuggingFace tokenizer %s (revision %s)", identifier, revision)
return create_pretrained_tokenizer(identifier=identifier, revision=revision, auth_token=auth_token)
-@lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)
def _select_tokenizer_helper(model: str) -> SelectTokenizerResponse:
if litellm.disable_hf_tokenizer_download is True:
return _return_openai_tokenizer(model)
@@ -2274,6 +2287,10 @@ def _select_tokenizer_helper(model: str) -> SelectTokenizerResponse:
if result is not None:
return result
except Exception as e:
+ from litellm.rust_bridge.fork_guard import ForkedAfterNativeRuntimeStarted, ProcessReservedForForking
+
+ if isinstance(e, (ForkedAfterNativeRuntimeStarted, ProcessReservedForForking)):
+ raise
verbose_logger.debug("Error selecting tokenizer: %s", e)
# default - tiktoken
@@ -2308,19 +2325,26 @@ def _return_huggingface_tokenizer(model: str) -> SelectTokenizerResponse | None:
kind: Final = huggingface_tokenizer_kind(model)
if kind is None:
return None
- return {"type": "huggingface_tokenizer", "tokenizer": _load_huggingface_tokenizer(kind)}
+ return {
+ "type": "huggingface_tokenizer",
+ "tokenizer": _load_huggingface_tokenizer(kind, _huggingface_tokenizer_backend()),
+ }
-def _load_huggingface_tokenizer(kind: HuggingFaceTokenizerKind) -> Tokenizer:
+@lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)
+def _load_huggingface_tokenizer(kind: HuggingFaceTokenizerKind, backend: Decision) -> HuggingFace:
+ """One tokenizer per kind and backend; `backend` is the cache key, the dispatch re-derives it."""
match kind:
case "cohere":
- return Tokenizer.from_pretrained("Xenova/c4ai-command-r-v01-tokenizer")
+ return tokenizer_dispatch.from_pretrained("Xenova/c4ai-command-r-v01-tokenizer")
case "anthropic":
- return Tokenizer.from_str(claude_json_str)
+ return tokenizer_dispatch.anthropic()
case "llama2":
- return Tokenizer.from_pretrained("hf-internal-testing/llama-tokenizer")
+ return tokenizer_dispatch.from_pretrained("hf-internal-testing/llama-tokenizer")
case "llama3":
- return Tokenizer.from_pretrained("Xenova/llama-3-tokenizer")
+ return tokenizer_dispatch.from_pretrained("Xenova/llama-3-tokenizer")
+ case _:
+ assert_never(kind)
def encode(model="", text="", custom_tokenizer: dict | None = None):
@@ -2336,15 +2360,13 @@ def encode(model="", text="", custom_tokenizer: dict | None = None):
enc: The encoded text.
"""
tokenizer_json: Final = custom_tokenizer or _select_tokenizer(model=model)
- if isinstance(tokenizer_json["tokenizer"], Encoding):
- enc = tokenizer_json["tokenizer"].encode(text, disallowed_special=())
- else:
- enc = tokenizer_json["tokenizer"].encode(text)
- # Normalize: HuggingFace Tokenizer.encode() returns an Encoding object;
- # extract .ids so the return type is always List[int].
- if hasattr(enc, "ids"):
- return enc.ids
- return enc
+ if tokenizer_json["type"] == "openai_tokenizer":
+ openai_tokenizer: Final = cast( # cast-ok: [LIT006] caller's explicit type tag selects this interface
+ Encoding, tokenizer_json["tokenizer"]
+ )
+ return openai_tokenizer.encode(text, disallowed_special=())
+ encoded: Final = tokenizer_json["tokenizer"].encode(text)
+ return encoded.ids if hasattr(encoded, "ids") else encoded
def decode(
@@ -2363,26 +2385,12 @@ def decode(
"""
tokenizer_json: Final = custom_tokenizer or _select_tokenizer(model=model)
if tokenizer_json["type"] == "huggingface_tokenizer":
- if skip_special_tokens:
- tokens = _strip_huggingface_special_token_ids(tokenizer_json["tokenizer"], tokens)
- dec = tokenizer_json["tokenizer"].decode(tokens, skip_special_tokens=skip_special_tokens)
- return dec
- dec = tokenizer_json["tokenizer"].decode(tokens)
- return dec
-
-
-def _strip_huggingface_special_token_ids(tokenizer: Tokenizer, tokens: Sequence[int]) -> Sequence[int]:
- try:
- added_tokens_decoder: Final = tokenizer.get_added_tokens_decoder()
- except Exception:
- return tokens
-
- special_token_ids: Final = {
- token_id for token_id, added_token in added_tokens_decoder.items() if getattr(added_token, "special", False)
- }
- if not special_token_ids:
- return tokens
- return [token for token in tokens if token not in special_token_ids]
+ ids: Final = strip_special_tokens(tokenizer_json["tokenizer"], tokens) if skip_special_tokens else tokens
+ hf_tokenizer: Final = cast( # cast-ok: [LIT006] caller's explicit type tag selects this interface
+ HuggingFace, tokenizer_json["tokenizer"]
+ )
+ return hf_tokenizer.decode(ids, skip_special_tokens=skip_special_tokens)
+ return tokenizer_json["tokenizer"].decode(tokens)
def create_pretrained_tokenizer(identifier: str, revision="main", auth_token: str | None = None):
@@ -2398,7 +2406,7 @@ def create_pretrained_tokenizer(identifier: str, revision="main", auth_token: st
dict: A dictionary with the tokenizer and its type.
"""
- tokenizer: Final = Tokenizer.from_pretrained(identifier, revision=revision, token=auth_token)
+ tokenizer: Final = tokenizer_dispatch.from_pretrained(identifier, revision=revision, token=auth_token)
return {"type": "huggingface_tokenizer", "tokenizer": tokenizer}
@@ -2413,7 +2421,7 @@ def create_tokenizer(json: str):
dict: A dictionary with the tokenizer and its type.
"""
- tokenizer: Final = Tokenizer.from_str(json)
+ tokenizer: Final = tokenizer_dispatch.from_str(json)
return {"type": "huggingface_tokenizer", "tokenizer": tokenizer}
diff --git a/migrations/Dockerfile b/migrations/Dockerfile
index c6d1b0cc46e..f34940c0ce0 100644
--- a/migrations/Dockerfile
+++ b/migrations/Dockerfile
@@ -67,6 +67,8 @@ RUN --mount=type=cache,target=/root/.cache/uv \
--extra extra_proxy \
--python python3.13
+RUN cp "$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"/litellm/rust_bridge/_native*.so litellm/rust_bridge/
+
COPY migrations/run.py /app/run.py
# Pre-warm the Prisma binary cache so the Job pod doesn't reach the
diff --git a/pyproject.toml b/pyproject.toml
index a1b276e4e8c..f447343ff33 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -19,11 +19,13 @@ dependencies = [
"httpx[http2]>=0.28.0,<1.0",
"openai>=2.20.0,<3.0.0",
"python-dotenv>=1.0.0,<2.0",
+ "pyyaml>=6.0.3,<7.0",
+ "packaging>=24.0",
+ "importlib-metadata>=8.0.0,<9.0",
"tiktoken>=0.8.0,<1.0; python_version < '3.14'",
"tiktoken>=0.12.0,<1.0; python_version >= '3.14'",
- "importlib-metadata>=8.0.0,<9.0",
- "packaging>=24.0",
"tokenizers>=0.21.0,<1.0",
+ "huggingface-hub>=0.34.0,<2.0",
"click>=8.0.0,<9.0",
"jinja2>=3.1.6,<4.0",
"aiohttp>=3.14.2,<4.0",
@@ -191,6 +193,7 @@ litellm-proxy = "litellm.proxy.client.cli:litellm_proxy_cli"
[dependency-groups]
dev = [
+ "numpy>=1.26.0,<3.0",
"diff-cover==9.7.2",
"hypothesis==6.165.10",
"reportlab==5.0.1",
@@ -290,6 +293,12 @@ healthcheck = [
"httpx==0.28.1",
"pyyaml==6.0.3",
]
+benchmarks = [
+ "pytest==9.0.3",
+ "pytest-codspeed==4.3.0",
+ "mcp>=2.2.0,<3",
+ "a2a-sdk==1.1.0",
+]
[build-system]
requires = ["maturin==1.15.0"]
diff --git a/tests/benchmarks/conftest.py b/tests/benchmarks/conftest.py
index c9b31cfb7d7..309ecab9991 100644
--- a/tests/benchmarks/conftest.py
+++ b/tests/benchmarks/conftest.py
@@ -8,8 +8,11 @@ flipping results between runs. Running the executor inline keeps each
benchmark's cost self-contained and deterministic.
"""
+import os
+import sys
from collections.abc import Callable, Iterator
from concurrent.futures import Future
+from pathlib import Path
from typing import ParamSpec, TypeVar
import pytest
@@ -20,6 +23,21 @@ P = ParamSpec("P")
R = TypeVar("R")
+def pytest_configure(config: pytest.Config) -> None:
+ if os.environ.get("LITELLM_REQUIRE_INSTALLED_WHEEL") != "1":
+ return
+
+ import litellm
+ import litellm.rust_bridge._native as native
+
+ prefix = Path(sys.prefix).resolve()
+ for name, module_file in (("litellm", litellm.__file__), ("litellm.rust_bridge._native", native.__file__)):
+ path = Path(module_file).resolve()
+ if not path.is_relative_to(prefix):
+ raise pytest.UsageError(f"{name} resolved outside the benchmark environment: {path}")
+ print(f"{name}: {path}") # noqa: T201 # provenance evidence must be visible in CI logs
+
+
def _submit_inline(fn: Callable[P, R], /, *args: P.args, **kwargs: P.kwargs) -> Future[R]:
future: Future[R] = Future()
try:
diff --git a/tests/proxy_unit_tests/test_custom_tokenizer_bug.py b/tests/proxy_unit_tests/test_custom_tokenizer_bug.py
index c4b1f4f3afd..23b6b302202 100644
--- a/tests/proxy_unit_tests/test_custom_tokenizer_bug.py
+++ b/tests/proxy_unit_tests/test_custom_tokenizer_bug.py
@@ -22,10 +22,8 @@ from litellm.proxy.proxy_server import token_counter
def _fake_hf_tokenizer(num_tokens: int) -> MagicMock:
- encoding = MagicMock()
- encoding.__len__.return_value = num_tokens
tokenizer = MagicMock()
- tokenizer.encode_batch_fast.return_value = [encoding]
+ tokenizer.encode_batch_fast.return_value = [[0] * num_tokens]
return tokenizer
@@ -58,7 +56,7 @@ async def test_custom_tokenizer_from_model_info_is_used(monkeypatch):
)
monkeypatch.setattr(litellm.proxy.proxy_server, "llm_router", llm_router)
- with patch.object(litellm.utils, "Tokenizer") as mock_tokenizer_cls:
+ with patch.object(litellm.utils, "tokenizer_dispatch") as mock_tokenizer_cls:
mock_tokenizer_cls.from_pretrained.return_value = _fake_hf_tokenizer(7)
response = await token_counter(
@@ -92,7 +90,7 @@ async def test_model_without_custom_tokenizer_uses_default(monkeypatch):
)
monkeypatch.setattr(litellm.proxy.proxy_server, "llm_router", llm_router)
- with patch.object(litellm.utils, "Tokenizer") as mock_tokenizer_cls:
+ with patch.object(litellm.utils, "tokenizer_dispatch") as mock_tokenizer_cls:
response = await token_counter(
request=TokenCountRequest(
model="gpt-4",
diff --git a/tests/test_litellm/litellm_core_utils/test_decode_special_tokens.py b/tests/test_litellm/litellm_core_utils/test_decode_special_tokens.py
index d1a5f78a859..dd2daef5484 100644
--- a/tests/test_litellm/litellm_core_utils/test_decode_special_tokens.py
+++ b/tests/test_litellm/litellm_core_utils/test_decode_special_tokens.py
@@ -1,23 +1,11 @@
-from tokenizers import AddedToken, Tokenizer
-from tokenizers.models import WordLevel
-from tokenizers.pre_tokenizers import Whitespace
-from tokenizers.processors import TemplateProcessing
-
from litellm import decode, encode
+from tokenizers import Tokenizer
+
+TOKENIZER_JSON = """{"version":"1.0","truncation":null,"padding":null,"added_tokens":[{"id":3,"content":"[BOS]","single_word":false,"lstrip":false,"rstrip":false,"normalized":false,"special":true}],"normalizer":null,"pre_tokenizer":{"type":"Whitespace"},"post_processor":{"type":"TemplateProcessing","single":[{"SpecialToken":{"id":"[BOS]","type_id":0}},{"Sequence":{"id":"A","type_id":0}}],"pair":[{"Sequence":{"id":"A","type_id":0}},{"Sequence":{"id":"B","type_id":1}}],"special_tokens":{"[BOS]":{"id":"[BOS]","ids":[3],"tokens":["[BOS]"]}}},"decoder":null,"model":{"type":"WordLevel","vocab":{"[UNK]":0,"Hello":1,"World":2},"unk_token":"[UNK]"}}"""
def _create_custom_tokenizer():
- tokenizer = Tokenizer(
- WordLevel({"[UNK]": 0, "Hello": 1, "World": 2}, unk_token="[UNK]")
- )
- tokenizer.pre_tokenizer = Whitespace()
- tokenizer.add_special_tokens([AddedToken("[BOS]", special=True)])
- bos_token_id = tokenizer.token_to_id("[BOS]")
- assert bos_token_id is not None
- tokenizer.post_processor = TemplateProcessing(
- single="[BOS] $A",
- special_tokens=[("[BOS]", bos_token_id)],
- )
+ tokenizer = Tokenizer.from_str(TOKENIZER_JSON)
return {"type": "huggingface_tokenizer", "tokenizer": tokenizer}
diff --git a/tests/test_litellm/litellm_core_utils/test_token_counter.py b/tests/test_litellm/litellm_core_utils/test_token_counter.py
index f19a8891609..5ce6a4b1ce9 100644
--- a/tests/test_litellm/litellm_core_utils/test_token_counter.py
+++ b/tests/test_litellm/litellm_core_utils/test_token_counter.py
@@ -786,23 +786,23 @@ def test_token_counter():
import unittest
-from litellm.utils import _select_tokenizer_helper, claude_json_str, encoding
+from litellm.utils import _load_huggingface_tokenizer, _select_tokenizer_helper, claude_json_str, encoding
# Clear the cache at module load to ensure clean state
-_select_tokenizer_helper.cache_clear()
+_load_huggingface_tokenizer.cache_clear()
class TestTokenizerSelection(unittest.TestCase):
def setUp(self):
"""Clear the LRU cache before each test method.
- The _select_tokenizer_helper function is decorated with @lru_cache,
- which can cause cache hits from previous tests when running with
+ The HuggingFace tokenizers behind _select_tokenizer_helper are cached with
+ @lru_cache, which can cause cache hits from previous tests when running with
--dist=loadscope (tests from same file run on same worker).
"""
- _select_tokenizer_helper.cache_clear()
+ _load_huggingface_tokenizer.cache_clear()
- @patch("litellm.utils.Tokenizer.from_pretrained")
+ @patch("litellm.utils.tokenizer_dispatch.from_pretrained")
def test_llama3_tokenizer_api_failure(self, mock_from_pretrained):
# Setup mock to raise an error
mock_from_pretrained.side_effect = Exception("Failed to load tokenizer")
@@ -817,7 +817,7 @@ class TestTokenizerSelection(unittest.TestCase):
self.assertEqual(result["type"], "openai_tokenizer")
self.assertEqual(result["tokenizer"], encoding)
- @patch("litellm.utils.Tokenizer.from_pretrained")
+ @patch("litellm.utils.tokenizer_dispatch.from_pretrained")
def test_cohere_tokenizer_api_failure(self, mock_from_pretrained):
# Setup mock to raise an error
mock_from_pretrained.side_effect = Exception("Failed to load tokenizer")
@@ -837,10 +837,10 @@ class TestTokenizerSelection(unittest.TestCase):
self.assertEqual(result["type"], "openai_tokenizer")
self.assertEqual(result["tokenizer"], encoding)
- @patch("litellm.utils.Tokenizer.from_str")
- def test_claude_tokenizer_api_failure(self, mock_from_str):
+ @patch("litellm.utils.tokenizer_dispatch.anthropic")
+ def test_claude_tokenizer_api_failure(self, mock_anthropic):
# Setup mock to raise an error
- mock_from_str.side_effect = Exception("Failed to load tokenizer")
+ mock_anthropic.side_effect = Exception("Failed to load tokenizer")
# Add Claude model to the list for testing
litellm.anthropic_models = ["claude-2"]
@@ -849,13 +849,13 @@ class TestTokenizerSelection(unittest.TestCase):
result = _select_tokenizer_helper("claude-2")
# Verify the attempt to load Claude tokenizer
- mock_from_str.assert_called_once_with(claude_json_str)
+ mock_anthropic.assert_called_once_with()
# Verify fallback to OpenAI tokenizer
self.assertEqual(result["type"], "openai_tokenizer")
self.assertEqual(result["tokenizer"], encoding)
- @patch("litellm.utils.Tokenizer.from_pretrained")
+ @patch("litellm.utils.tokenizer_dispatch.from_pretrained")
def test_llama2_tokenizer_api_failure(self, mock_from_pretrained):
# Setup mock to raise an error
mock_from_pretrained.side_effect = Exception("Failed to load tokenizer")
diff --git a/tests/test_litellm/litellm_core_utils/test_tokenizer.py b/tests/test_litellm/litellm_core_utils/test_tokenizer.py
new file mode 100644
index 00000000000..aa4a0fc6a1c
--- /dev/null
+++ b/tests/test_litellm/litellm_core_utils/test_tokenizer.py
@@ -0,0 +1,403 @@
+import copy
+import os
+import pickle
+import subprocess
+import sys
+from pathlib import Path
+from typing import Final, Literal
+
+import pytest
+import tiktoken
+from tokenizers import Tokenizer as ReferenceTokenizer
+
+import litellm
+from litellm.caching._embedding_router import truncate_embedding_input
+from litellm.litellm_core_utils.tokenizer import HuggingFaceTokenizer, OpenAIEncoding
+from litellm.utils import claude_json_str
+from tests.test_litellm.litellm_core_utils.test_decode_special_tokens import TOKENIZER_JSON
+
+
+ "name", ("cl100k_base", "o200k_base", "p50k_base", "p50k_edit", "r50k_base", "gpt2", "o200k_harmony")
+)
+ "text", ("hello world", "café 漢字 🙂", "", "a\ud800b", "\ud83d\ude42", "🙂\ud83d\ude42\udfff", " " * 64)
+)
+def test_openai_encoding_matches_python_unicode_and_batches(name: str, text: str) -> None:
+ reference: Final = tiktoken.get_encoding(name)
+ encoding: Final = OpenAIEncoding.from_tiktoken(name)
+ expected: Final = reference.encode(text)
+
+ assert encoding.encode(text) == expected
+ assert encoding.count(text) == len(expected)
+ assert encoding.encode_batch([text], num_threads=2) == reference.encode_batch([text], num_threads=2)
+ assert encoding.encode_ordinary_batch([text]) == reference.encode_ordinary_batch([text])
+ assert encoding.decode_batch([expected]) == reference.decode_batch([expected])
+ assert encoding.decode_bytes_batch([expected]) == reference.decode_bytes_batch([expected])
+
+
[email protected]("allowed", (frozenset(), frozenset({"<|endoftext|>"}), "all"))
[email protected]("disallowed", (frozenset(), frozenset({"<|fim_prefix|>"}), "all"))
+def test_openai_special_token_options_match_python(
+ allowed: frozenset[str] | Literal["all"], disallowed: frozenset[str] | Literal["all"]
+) -> None:
+ reference: Final = tiktoken.get_encoding("cl100k_base")
+ encoding: Final = OpenAIEncoding.from_tiktoken(reference.name)
+ text: Final = "hello<|endoftext|><|fim_prefix|>world"
+ allowed_set: Final = reference.special_tokens_set if allowed == "all" else allowed
+ disallowed_set: Final = reference.special_tokens_set - allowed_set if disallowed == "all" else disallowed
+ if any(token in text for token in disallowed_set):
+ with pytest.raises(ValueError, match="disallowed special token"):
+ encoding.encode(text, allowed_special=allowed, disallowed_special=disallowed)
+ return
+ assert encoding.encode(text, allowed_special=allowed, disallowed_special=disallowed) == reference.encode(
+ text, allowed_special=allowed, disallowed_special=disallowed
+ )
+ assert encoding.special_tokens_set == reference.special_tokens_set
+ assert encoding.eot_token == reference.eot_token
+
+
[email protected]("errors", ("replace", "ignore", "backslashreplace", "strict"))
+def test_openai_partial_token_decoding_preserves_error_policy(errors: str) -> None:
+ reference: Final = tiktoken.get_encoding("cl100k_base")
+ encoding: Final = OpenAIEncoding.from_tiktoken(reference.name)
+ tokens: Final = reference.encode("🙂")[:1]
+ assert encoding.decode_bytes(tokens) == reference.decode_bytes(tokens)
+ if errors == "strict":
+ with pytest.raises(UnicodeDecodeError):
+ encoding.decode(tokens, errors=errors)
+ return
+ assert encoding.decode(tokens, errors=errors) == reference.decode(tokens, errors=errors)
+ assert encoding.decode_tokens_bytes(tokens) == reference.decode_tokens_bytes(tokens)
+
+
+def test_public_encoding_and_semantic_cache_preserve_truncated_unicode() -> None:
+ reference: Final = tiktoken.get_encoding(litellm.encoding.name)
+ text: Final = "🙂"
+ tokens: Final = reference.encode(text)
+
+ assert litellm.encoding.encode(text, disallowed_special=()) == tokens
+ assert litellm.encoding.encode_batch([text]) == [tokens]
+ assert litellm.decode(tokens=tokens[:1]) == reference.decode(tokens[:1])
+ assert truncate_embedding_input(text, "", 1) == reference.decode(tokens[:1])
+
+
[email protected]("add_special_tokens", (True, False))
+def test_huggingface_encoding_preserves_result_fields_and_serialization(add_special_tokens: bool) -> None:
+ reference: Final = ReferenceTokenizer.from_str(TOKENIZER_JSON)
+ tokenizer: Final = HuggingFaceTokenizer.from_str(TOKENIZER_JSON)
+ expected: Final = reference.encode("Hello World", add_special_tokens=add_special_tokens)
+ actual: Final = tokenizer.encode("Hello World", add_special_tokens=add_special_tokens)
+
+ assert (actual.ids, actual.tokens, actual.type_ids, actual.offsets, actual.word_ids, actual.sequence_ids) == (
+ expected.ids,
+ expected.tokens,
+ expected.type_ids,
+ expected.offsets,
+ expected.word_ids,
+ expected.sequence_ids,
+ )
+ assert (actual.attention_mask, actual.special_tokens_mask, actual.n_sequences, len(actual)) == (
+ expected.attention_mask,
+ expected.special_tokens_mask,
+ expected.n_sequences,
+ len(expected),
+ )
+ assert copy.deepcopy(actual).ids == expected.ids
+ assert pickle.loads(pickle.dumps(actual)).offsets == expected.offsets
+ assert tokenizer.decode(actual.ids, skip_special_tokens=False) == reference.decode(
+ expected.ids, skip_special_tokens=False
+ )
+
+
+def test_huggingface_character_offsets_and_pretokenized_pairs_match_python() -> None:
+ reference: Final = ReferenceTokenizer.from_str(claude_json_str)
+ tokenizer: Final = HuggingFaceTokenizer.from_str(claude_json_str)
+ text: Final = "café 漢字 🙂"
+ actual: Final = tokenizer.encode(text)
+ expected: Final = reference.encode(text)
+
+ assert actual.offsets == expected.offsets
+ assert actual.ids == expected.ids
+ assert (
+ tokenizer.encode(["hello", "world"], ["again"], is_pretokenized=True).ids
+ == reference.encode(["hello", "world"], ["again"], is_pretokenized=True).ids
+ )
+
+
+def test_huggingface_batches_apply_padding_across_inputs() -> None:
+ reference: Final = ReferenceTokenizer.from_str(TOKENIZER_JSON)
+ reference.enable_padding(pad_id=0, pad_token="[UNK]")
+ tokenizer: Final = HuggingFaceTokenizer.from_str(reference.to_str())
+ inputs: Final = ["Hello", ("Hello World", "World")]
+ expected: Final = reference.encode_batch(inputs)
+ actual: Final = tokenizer.encode_batch(inputs)
+ fast: Final = tokenizer.encode_batch_fast(inputs)
+
+ assert [(item.ids, item.attention_mask, item.offsets) for item in actual] == [
+ (item.ids, item.attention_mask, item.offsets) for item in expected
+ ]
+ assert [item.ids for item in fast] == [item.ids for item in expected]
+ assert tokenizer.decode_batch([item.ids for item in actual]) == reference.decode_batch(
+ [item.ids for item in expected]
+ )
+
+
+def test_caller_supplied_huggingface_tokenizer_preserves_public_encode_and_count() -> None:
+ tokenizer: Final = ReferenceTokenizer.from_str(TOKENIZER_JSON)
+ custom: Final = {"type": "huggingface_tokenizer", "tokenizer": tokenizer}
+ expected: Final = tokenizer.encode("Hello World").ids
+
+ assert litellm.encode(text="Hello World", custom_tokenizer=custom) == expected
+ assert litellm.token_counter(text="Hello World", custom_tokenizer=custom) == len(expected)
+ assert litellm.decode(tokens=expected, custom_tokenizer=custom) == "Hello World"
+
+
+def test_caller_supplied_tiktoken_treats_special_spellings_as_text() -> None:
+ tokenizer: Final = tiktoken.get_encoding("cl100k_base")
+ custom: Final = {"type": "openai_tokenizer", "tokenizer": tokenizer}
+ text: Final = "<|endoftext|>"
+
+ assert litellm.encode(text=text, custom_tokenizer=custom) == tokenizer.encode(text, disallowed_special=())
+
+
+def test_public_tokenizer_objects_survive_pickle_and_deepcopy(tmp_path: Path) -> None:
+ custom: Final = litellm.create_tokenizer(TOKENIZER_JSON)
+ tokenizer: Final = custom["tokenizer"]
+ path: Final = tmp_path / "tokenizer.json"
+ tokenizer.save(str(path))
+
+ assert copy.deepcopy(custom)["tokenizer"].encode("Hello World").ids == tokenizer.encode("Hello World").ids
+ assert (
+ pickle.loads(pickle.dumps(custom))["tokenizer"].encode("Hello World").ids == tokenizer.encode("Hello World").ids
+ )
+ assert HuggingFaceTokenizer.from_file(str(path)).encode("Hello World").ids == tokenizer.encode("Hello World").ids
+ assert copy.deepcopy(litellm.encoding).encode("hello") == litellm.encoding.encode("hello")
+ assert pickle.loads(pickle.dumps(litellm.encoding)).encode("hello") == litellm.encoding.encode("hello")
+
+
[email protected]("offline", ("0", "1"))
+def test_hub_loader_preserves_environment_auth_cache_and_offline(tmp_path: Path, offline: str) -> None:
+ script: Final = """
+import json
+import sys
+from pathlib import Path
+sys.path.insert(0, sys.argv[1])
+import httpx
+import huggingface_hub
+from huggingface_hub.errors import LocalEntryNotFoundError
+import litellm
+payload = sys.argv[2].encode()
+offline = sys.argv[3] == "1"
+observed = []
+def handle(request):
+ assert not offline, "offline loading issued a request"
+ if request.url.path.endswith("/tokenizer.json"):
+ observed.append(request.headers.get("authorization"))
+ if request.headers.get("authorization") != "Bearer audit-fixture-token":
+ return httpx.Response(401)
+ return httpx.Response(200, headers={"content-length": str(len(payload)), "etag": '"fixture"', "x-repo-commit": "a" * 40}, content=payload if request.method == "GET" else b"")
+if not offline:
+ huggingface_hub.set_client_factory(lambda: httpx.Client(transport=httpx.MockTransport(handle)))
+try:
+ tokenizer = litellm.create_pretrained_tokenizer("test-fixture/tokenizer")["tokenizer"]
+except LocalEntryNotFoundError:
+ assert offline
+ assert observed == []
+else:
+ assert not offline
+ assert "Bearer audit-fixture-token" in observed
+ assert tokenizer.decode(tokenizer.encode("Hello World").ids) == "Hello World"
+ assert tuple(Path(sys.argv[4]).rglob("tokenizer.json"))
+print("compatible")
+"""
+ result: Final = subprocess.run(
+ [
+ sys.executable,
+ "-I",
+ "-c",
+ script,
+ str(Path(litellm.__file__).parent.parent),
+ TOKENIZER_JSON,
+ offline,
+ str(tmp_path / "cache"),
+ ],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ env={
+ **os.environ,
+ "HF_HOME": str(tmp_path / "home"),
+ "HF_HUB_CACHE": str(tmp_path / "cache"),
+ "HF_ENDPOINT": "http://127.0.0.1:9",
+ "HF_TOKEN": "audit-fixture-token",
+ "HF_HUB_OFFLINE": offline,
+ "HF_HUB_DISABLE_IMPLICIT_TOKEN": "0",
+ "LITELLM_LOCAL_MODEL_COST_MAP": "True",
+ },
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert result.stdout.strip() == "compatible"
+
+
[email protected]("rust", (None, "0", "1"))
+def test_tokenization_without_native_extension_stays_offline(tmp_path: Path, rust: str | None) -> None:
+ script: Final = """
+import importlib.abc
+import sys
+sys.path.insert(0, sys.argv[1])
+def reject_network(event, args):
+ if event == "socket.connect":
+ raise AssertionError("tokenizer attempted a network connection")
+sys.addaudithook(reject_network)
+class Block(importlib.abc.MetaPathFinder):
+ def find_spec(self, fullname, path=None, target=None):
+ if fullname == "litellm.rust_bridge._native":
+ raise ImportError("native extension is unavailable")
+sys.meta_path.insert(0, Block())
+import litellm
+from litellm.rust_bridge.tokenizer import get_encoding
+import tiktoken
+from tokenizers import Tokenizer
+assert isinstance(litellm.encoding, tiktoken.Encoding)
+for name in ("cl100k_base", "o200k_base", "o200k_harmony", "p50k_base", "p50k_edit"):
+ encoding = get_encoding(name)
+ text = "offline café 漢字 🙂" + " " * 64
+ assert encoding.decode(encoding.encode(text)) == text
+ids = litellm.encode(text="hello world")
+assert litellm.decode(tokens=ids) == "hello world"
+assert litellm.token_counter(model=None, text="hello world") == len(ids)
+custom = litellm.create_tokenizer(sys.argv[2])
+assert isinstance(custom["tokenizer"], Tokenizer)
+custom["tokenizer"].enable_padding(pad_id=0, pad_token="[UNK]")
+assert litellm.decode(tokens=litellm.encode(text="Hello World", custom_tokenizer=custom), custom_tokenizer=custom) == "Hello World"
+print("compatible")
+"""
+ result: Final = subprocess.run(
+ [sys.executable, "-I", "-c", script, str(Path(litellm.__file__).parent.parent), TOKENIZER_JSON],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ cwd=tmp_path,
+ env={
+ **{key: value for key, value in os.environ.items() if key != "LITELLM_RUST"},
+ **({"LITELLM_RUST": rust} if rust is not None else {}),
+ "LITELLM_LOCAL_MODEL_COST_MAP": "True",
+ "TIKTOKEN_CACHE_DIR": str(tmp_path / "unused-tokenizer-cache"),
+ },
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert result.stdout.strip() == "compatible"
+ assert not (tmp_path / "unused-tokenizer-cache").exists()
+
+
[email protected]("is_pretokenized", (False, True))
+def test_huggingface_batch_sequence_containers_match_python(is_pretokenized: bool) -> None:
+ reference: Final = ReferenceTokenizer.from_str(TOKENIZER_JSON)
+ tokenizer: Final = HuggingFaceTokenizer.from_str(TOKENIZER_JSON)
+ inputs: Final = [["Hello", "World"], ("Hello", "World")]
+ actual: Final = tokenizer.encode_batch(inputs, is_pretokenized=is_pretokenized)
+ expected: Final = reference.encode_batch(inputs, is_pretokenized=is_pretokenized)
+ assert [(item.ids, item.type_ids, item.sequence_ids) for item in actual] == [
+ (item.ids, item.type_ids, item.sequence_ids) for item in expected
+ ]
+
+
[email protected]("name", ("cl100k_base", "o200k_base", "p50k_edit", "gpt2"))
+def test_openai_encoding_exposes_the_tiktoken_vocabulary_surface(name: str) -> None:
+ reference: Final = tiktoken.get_encoding(name)
+ encoding: Final = OpenAIEncoding.from_tiktoken(name)
+ text: Final = "hello fanta"
+
+ assert repr(encoding) == repr(reference) == f"<Encoding {name!r}>"
+ assert (encoding.name, encoding.n_vocab, encoding.max_token_value) == (
+ reference.name,
+ reference.n_vocab,
+ reference.max_token_value,
+ )
+ assert encoding.token_byte_values() == reference.token_byte_values()
+ assert encoding.encode_single_token("hello") == reference.encode_single_token("hello")
+ assert encoding.encode_single_token(b"<|endoftext|>") == reference.eot_token
+ assert [encoding.is_special_token(token) for token in (0, reference.eot_token)] == [False, True]
+ assert encoding.decode_with_offsets(reference.encode(text)) == reference.decode_with_offsets(reference.encode(text))
+ assert encoding.encode_to_numpy(text).tolist() == reference.encode_to_numpy(text).tolist()
+ stable, completions = encoding.encode_with_unstable(text)
+ expected_stable, expected_completions = reference.encode_with_unstable(text)
+ assert (stable, sorted(completions)) == (expected_stable, sorted(expected_completions))
+ with pytest.raises(KeyError):
+ encoding.encode_single_token("<|not-a-token|>")
+
+
+def test_huggingface_tokenizer_exposes_the_tokenizers_vocabulary_surface() -> None:
+ reference: Final = ReferenceTokenizer.from_str(TOKENIZER_JSON)
+ reference.enable_padding(pad_id=0, pad_token="[UNK]", length=4)
+ reference.enable_truncation(max_length=3, stride=1, strategy="only_first", direction="left")
+ tokenizer: Final = HuggingFaceTokenizer.from_str(reference.to_str())
+
+ assert tokenizer.token_to_id("Hello") == reference.token_to_id("Hello") == 1
+ assert tokenizer.id_to_token(3) == reference.id_to_token(3) == "[BOS]"
+ assert tokenizer.id_to_token(99) is None
+ assert tokenizer.get_vocab() == reference.get_vocab()
+ assert tokenizer.get_vocab(with_added_tokens=False) == reference.get_vocab(with_added_tokens=False)
+ assert tokenizer.get_vocab_size() == reference.get_vocab_size() == 4
+ assert tokenizer.get_vocab_size(with_added_tokens=False) == reference.get_vocab_size(with_added_tokens=False)
+ added: Final = tokenizer.get_added_tokens_decoder()
+ expected_added: Final = reference.get_added_tokens_decoder()
+ assert {token_id: str(token) for token_id, token in added.items()} == {
+ token_id: str(token) for token_id, token in expected_added.items()
+ }
+ assert added[3].special == expected_added[3].special
+ assert tokenizer.num_special_tokens_to_add(False) == reference.num_special_tokens_to_add(False) == 1
+ assert tokenizer.num_special_tokens_to_add(True) == reference.num_special_tokens_to_add(True) == 0
+ assert tokenizer.padding == reference.padding
+ assert tokenizer.truncation == reference.truncation
+ assert tokenizer.encode_special_tokens == reference.encode_special_tokens is False
+ assert HuggingFaceTokenizer.from_buffer(TOKENIZER_JSON.encode()).encode("Hello").ids == [3, 1]
+ assert HuggingFaceTokenizer.from_str(TOKENIZER_JSON).padding is None
+ assert HuggingFaceTokenizer.from_str(TOKENIZER_JSON).truncation is None
+
+
+def test_huggingface_encoding_exposes_the_tokenizers_lookup_and_mutation_surface() -> None:
+ reference: Final = ReferenceTokenizer.from_str(claude_json_str)
+ tokenizer: Final = HuggingFaceTokenizer.from_str(claude_json_str)
+ text: Final = "hello wide world"
+ actual: Final = tokenizer.encode(text, "again")
+ expected: Final = reference.encode(text, "again")
+
+ lookups: Final = (
+ lambda encoding: [encoding.token_to_chars(index) for index in range(len(encoding))],
+ lambda encoding: [encoding.token_to_word(index) for index in range(len(encoding))],
+ lambda encoding: [encoding.token_to_sequence(index) for index in range(len(encoding))],
+ lambda encoding: [encoding.char_to_token(position) for position in range(len(text))],
+ lambda encoding: [encoding.char_to_word(position) for position in range(len(text))],
+ lambda encoding: [encoding.char_to_token(position, 1) for position in range(5)],
+ lambda encoding: [encoding.word_to_tokens(word) for word in range(3)],
+ lambda encoding: [encoding.word_to_chars(word) for word in range(3)],
+ lambda encoding: [encoding.word_to_tokens(0, 1), encoding.word_to_chars(0, 1)],
+ )
+ for lookup in lookups:
+ assert lookup(actual) == lookup(expected)
+ assert repr(actual) == repr(expected)
+
+ actual.truncate(4, stride=1, direction="left")
+ expected.truncate(4, stride=1, direction="left")
+ assert (actual.ids, [item.ids for item in actual.overflowing]) == (
+ expected.ids,
+ [item.ids for item in expected.overflowing],
+ )
+ actual.pad(6, direction="left", pad_id=7, pad_type_id=1, pad_token="<pad>")
+ expected.pad(6, direction="left", pad_id=7, pad_type_id=1, pad_token="<pad>")
+ assert (actual.ids, actual.attention_mask, actual.type_ids, actual.tokens) == (
+ expected.ids,
+ expected.attention_mask,
+ expected.type_ids,
+ expected.tokens,
+ )
+ actual.set_sequence_id(3)
+ expected.set_sequence_id(3)
+ assert actual.sequence_ids == expected.sequence_ids
+ merged: Final = type(actual).merge([actual, tokenizer.encode("more")])
+ assert merged.ids == type(expected).merge([expected, reference.encode("more")]).ids
+ assert merged.offsets == type(expected).merge([expected, reference.encode("more")]).offsets
+ with pytest.raises(ValueError, match="direction"):
+ actual.pad(8, direction="sideways")
diff --git a/tests/test_litellm/proxy/spend_tracking/test_budget_reservation.py b/tests/test_litellm/proxy/spend_tracking/test_budget_reservation.py
index 214b5cde7da..13488106df4 100644
--- a/tests/test_litellm/proxy/spend_tracking/test_budget_reservation.py
+++ b/tests/test_litellm/proxy/spend_tracking/test_budget_reservation.py
@@ -4,7 +4,7 @@ import json
import math
from datetime import datetime, timedelta, timezone
from types import MappingProxyType
-from typing import Final
+from typing import Final, cast
import pytest
@@ -33,6 +33,7 @@ from litellm.proxy.utils import ProxyLogging
from litellm.router import Router
from litellm.rust_bridge import bindings, configuration
from litellm.rust_bridge import token_counter as rust_token_counter
+from litellm.rust_bridge import tokenizer as tokenizer_dispatch
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
TOKEN_COUNTING_ROUTES: Final = (
@@ -239,6 +240,22 @@ class _FakeUpstream(Exception):
pass
+class _FakeTokenizer:
+ """Stands in for one shared native `Tokenizer`; only its name identifies it."""
+
+ def __init__(self, name: str, json: str | None = None) -> None:
+ self.name = name
+ self.json = json
+
+
+def _fake_native_tokenizers(monkeypatch: pytest.MonkeyPatch, anthropic_json: str | None = None) -> None:
+ """Point the counter's tokenizer lookups at fakes; the codec path keeps falling back to Python."""
+ fakes: Final = {name: _FakeTokenizer(name) for name in ("cl100k_base", "o200k_base")}
+ anthropic: Final = _FakeTokenizer("anthropic", anthropic_json)
+ monkeypatch.setattr(tokenizer_dispatch, "native_encoding", fakes.__getitem__)
+ monkeypatch.setattr(tokenizer_dispatch, "native_anthropic", lambda: anthropic)
+
+
class _FakeNative:
RustBridgeDeclined = _FakeDeclined
RustUpstreamError = _FakeUpstream
@@ -257,19 +274,13 @@ class _RecordingCounter:
class _RecordingFactory:
- """Stands in for the native `TokenCounter` class: called with tokenizer JSON, or `from_*_ranks`."""
+ """Stands in for the native `TokenCounter` class, built over a loaded `Tokenizer`."""
def __init__(self) -> None:
self.calls: list[tuple[rust_token_counter.RustTokenizer, bytes]] = []
- def __call__(self, tokenizer_json: str) -> _RecordingCounter:
- return _RecordingCounter(self, "anthropic")
-
- def from_cl100k_ranks(self, rank_file: str) -> _RecordingCounter:
- return _RecordingCounter(self, "cl100k_base")
-
- def from_o200k_ranks(self, rank_file: str) -> _RecordingCounter:
- return _RecordingCounter(self, "o200k_base")
+ def from_tokenizer(self, tokenizer: _FakeTokenizer, fast: bool = False) -> _RecordingCounter:
+ return _RecordingCounter(self, cast(rust_token_counter.RustTokenizer, tokenizer.name))
class _DecliningCounter:
@@ -278,19 +289,14 @@ class _DecliningCounter:
class _DecliningFactory:
- def __call__(self, tokenizer_json: str) -> _DecliningCounter:
- return _DecliningCounter()
-
- def from_cl100k_ranks(self, rank_file: str) -> _DecliningCounter:
- return _DecliningCounter()
-
- def from_o200k_ranks(self, rank_file: str) -> _DecliningCounter:
+ def from_tokenizer(self, tokenizer: _FakeTokenizer, fast: bool = False) -> _DecliningCounter:
return _DecliningCounter()
@pytest.fixture
def rust_counter(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(bindings, "get_native_bridge", lambda: _FakeNative())
+ _fake_native_tokenizers(monkeypatch)
rust_token_counter._counter.cache_clear()
configuration.reset_rust_configuration()
yield
diff --git a/tests/test_litellm/proxy/spend_tracking/test_input_tokens.py b/tests/test_litellm/proxy/spend_tracking/test_input_tokens.py
new file mode 100644
index 00000000000..49bbe148386
--- /dev/null
+++ b/tests/test_litellm/proxy/spend_tracking/test_input_tokens.py
@@ -0,0 +1,191 @@
+"""Tests for input-token counting shared across the reservation path's models."""
+
+from __future__ import annotations
+
+import json
+from types import MappingProxyType
+from typing import Final, cast
+
+import pytest
+
+import litellm
+from litellm.proxy.spend_tracking.input_tokens import (
+ TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS,
+ count_input_tokens,
+ count_input_tokens_for_model,
+)
+from litellm.rust_bridge import bindings, configuration, token_counter
+from litellm.rust_bridge import tokenizer as tokenizer_dispatch
+from litellm.rust_bridge.token_counter import RustTokenizer
+
+ANTHROPIC_MODEL: Final = "claude-sonnet-4-5-20250929"
+CL100K_MODEL: Final = "gpt-4"
+O200K_MODEL: Final = "gpt-4o"
+PYTHON_ONLY_MODEL: Final = "replicate/meta/llama-2-70b-chat"
+MESSAGES: Final = [{"role": "user", "content": "hello"}]
+RUST_TOKENS: Final = 777
+
+
+class _FakeDeclined(Exception):
+ pass
+
+
+class _FakeUpstream(Exception):
+ pass
+
+
+class _FakeTokenizer:
+ """Stands in for one shared native `Tokenizer`; only its name identifies it."""
+
+ def __init__(self, name: str, json: str | None = None) -> None:
+ self.name = name
+ self.json = json
+
+
+def _fake_native_tokenizers(monkeypatch: pytest.MonkeyPatch, anthropic_json: str | None = None) -> None:
+ """Point the counter's tokenizer lookups at fakes; the codec path keeps falling back to Python."""
+ fakes: Final = {name: _FakeTokenizer(name) for name in ("cl100k_base", "o200k_base")}
+ anthropic: Final = _FakeTokenizer("anthropic", anthropic_json)
+ monkeypatch.setattr(tokenizer_dispatch, "native_encoding", fakes.__getitem__)
+ monkeypatch.setattr(tokenizer_dispatch, "native_anthropic", lambda: anthropic)
+
+
+class _FakeNative:
+ RustBridgeDeclined = _FakeDeclined
+ RustUpstreamError = _FakeUpstream
+
+
+class _RecordingCounter:
+ def __init__(self, factory: _RecordingFactory, tokenizer: RustTokenizer) -> None:
+ self.factory = factory
+ self.tokenizer = tokenizer
+
+ async def acount_request(self, body: bytes) -> object:
+ self.factory.calls.append((self.tokenizer, body))
+ return {"model": "", "input_tokens": RUST_TOKENS}
+
+
+class _RecordingFactory:
+ def __init__(self) -> None:
+ self.calls: list[tuple[RustTokenizer, bytes]] = []
+
+ def from_tokenizer(self, tokenizer: _FakeTokenizer, fast: bool = False) -> _RecordingCounter:
+ return _RecordingCounter(self, cast(RustTokenizer, tokenizer.name))
+
+
+class _DecliningCounter:
+ async def acount_request(self, body: bytes) -> object:
+ raise _FakeDeclined("unsupported request shape")
+
+
+class _DecliningFactory:
+ def from_tokenizer(self, tokenizer: _FakeTokenizer, fast: bool = False) -> _DecliningCounter:
+ return _DecliningCounter()
+
+
[email protected](autouse=True)
+def _reset_bridge(monkeypatch: pytest.MonkeyPatch):
+ monkeypatch.setattr(bindings, "get_native_bridge", lambda: _FakeNative())
+ _fake_native_tokenizers(monkeypatch)
+ token_counter.TOKEN_COUNTER.reset()
+ token_counter._counter.cache_clear()
+ configuration.reset_rust_configuration()
+ yield
+ token_counter.TOKEN_COUNTER.reset()
+ token_counter._counter.cache_clear()
+ configuration.reset_rust_configuration()
+
+
+def _body(model: object) -> tuple[dict[str, object], bytes]:
+ body: Final = {"model": model, "messages": MESSAGES}
+ return body, json.dumps(body).encode()
+
+
+async def test_models_sharing_a_tokenizer_are_counted_once_and_merged() -> None:
+ factory: Final = _RecordingFactory()
+ litellm.rust(True)
+ token_counter.TOKEN_COUNTER.override(factory)
+ request_body, raw_body = _body([ANTHROPIC_MODEL, CL100K_MODEL, O200K_MODEL, "gpt-5", PYTHON_ONLY_MODEL])
+
+ counts: Final = await count_input_tokens(
+ request_body=request_body,
+ raw_body=raw_body,
+ models=(ANTHROPIC_MODEL, CL100K_MODEL, O200K_MODEL, "gpt-5", PYTHON_ONLY_MODEL),
+ )
+
+ assert factory.calls == [("anthropic", raw_body), ("cl100k_base", raw_body), ("o200k_base", raw_body)]
+ assert dict(counts) == {
+ ANTHROPIC_MODEL: RUST_TOKENS,
+ CL100K_MODEL: RUST_TOKENS,
+ O200K_MODEL: RUST_TOKENS,
+ "gpt-5": RUST_TOKENS,
+ PYTHON_ONLY_MODEL: count_input_tokens_for_model(request_body=request_body, model=PYTHON_ONLY_MODEL),
+ }
+
+
+async def test_rust_disabled_counts_everything_in_python() -> None:
+ factory: Final = _RecordingFactory()
+ litellm.rust(False)
+ token_counter.TOKEN_COUNTER.override(factory)
+ request_body, raw_body = _body([ANTHROPIC_MODEL, CL100K_MODEL])
+
+ counts: Final = await count_input_tokens(
+ request_body=request_body, raw_body=raw_body, models=(ANTHROPIC_MODEL, CL100K_MODEL)
+ )
+
+ assert factory.calls == []
+ assert dict(counts) == {
+ model: count_input_tokens_for_model(request_body=request_body, model=model)
+ for model in (ANTHROPIC_MODEL, CL100K_MODEL)
+ }
+
+
+async def test_missing_raw_body_counts_in_python() -> None:
+ factory: Final = _RecordingFactory()
+ litellm.rust(True)
+ token_counter.TOKEN_COUNTER.override(factory)
+ request_body, _ = _body(ANTHROPIC_MODEL)
+
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=None, models=(ANTHROPIC_MODEL,))
+
+ assert factory.calls == []
+ assert counts[ANTHROPIC_MODEL] == count_input_tokens_for_model(request_body=request_body, model=ANTHROPIC_MODEL)
+
+
+async def test_missing_binding_counts_in_python() -> None:
+ litellm.rust(True)
+ token_counter.TOKEN_COUNTER.override(None)
+ request_body, raw_body = _body(ANTHROPIC_MODEL)
+
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=raw_body, models=(ANTHROPIC_MODEL,))
+
+ assert counts[ANTHROPIC_MODEL] == count_input_tokens_for_model(request_body=request_body, model=ANTHROPIC_MODEL)
+
+
+async def test_declined_request_counts_in_python() -> None:
+ litellm.rust(True)
+ token_counter.TOKEN_COUNTER.override(_DecliningFactory())
+ request_body, raw_body = _body(ANTHROPIC_MODEL)
+
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=raw_body, models=(ANTHROPIC_MODEL,))
+
+ assert counts[ANTHROPIC_MODEL] == count_input_tokens_for_model(request_body=request_body, model=ANTHROPIC_MODEL)
+ assert counts[ANTHROPIC_MODEL] != RUST_TOKENS
+
+
+async def test_large_input_is_still_counted() -> None:
+ request_body: Final = {
+ "model": CL100K_MODEL,
+ "messages": [{"role": "user", "content": "x" * (TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS + 1)}],
+ }
+
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=None, models=(CL100K_MODEL,))
+
+ assert counts[CL100K_MODEL] == count_input_tokens_for_model(request_body=request_body, model=CL100K_MODEL)
+ assert isinstance(counts, MappingProxyType)
diff --git a/tests/test_litellm/proxy/test_budget_reservation.py b/tests/test_litellm/proxy/test_budget_reservation.py
index 86bf896188f..b3913079bb2 100644
--- a/tests/test_litellm/proxy/test_budget_reservation.py
+++ b/tests/test_litellm/proxy/test_budget_reservation.py
@@ -39,8 +39,6 @@ from litellm.proxy.common_utils.user_api_key_cache import (
model_access_group_spend_counter_key,
)
from litellm.proxy.spend_tracking.budget_reservation import (
- TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS,
- _approximate_input_size,
_get_model_access_group_budget_counters,
estimate_request_max_cost,
get_budget_window_start,
@@ -49,6 +47,10 @@ from litellm.proxy.spend_tracking.budget_reservation import (
release_budget_reservation_on_cancel,
reserve_budget_for_request,
)
+from litellm.proxy.spend_tracking.input_tokens import (
+ TOKENIZE_OFF_EVENT_LOOP_MIN_CHARS,
+ _approximate_input_size,
+)
from litellm.proxy.utils import ProxyLogging
from litellm.router import Router
from litellm.types.proxy.model_access_group_budget import ModelAccessGroupBudget
diff --git a/tests/test_litellm/proxy/test_proxy_server.py b/tests/test_litellm/proxy/test_proxy_server.py
index 2792e176e0e..26bfd5c52bd 100644
--- a/tests/test_litellm/proxy/test_proxy_server.py
+++ b/tests/test_litellm/proxy/test_proxy_server.py
@@ -14877,7 +14877,7 @@ async def test_token_counter_keeps_the_event_loop_free_during_a_huggingface_coun
async def test_token_counter_loads_a_custom_tokenizer_off_the_event_loop(monkeypatch):
- from tokenizers import Tokenizer
+ from litellm.rust_bridge._native import Tokenizer
from litellm import Router
from tests.test_litellm.litellm_core_utils.event_loop_lag import assert_loop_stayed_free, timed_with_loop_lags
@@ -14890,7 +14890,7 @@ async def test_token_counter_loads_a_custom_tokenizer_off_the_event_loop(monkeyp
time.sleep(0.3)
return claude_tokenizer
- monkeypatch.setattr(litellm.utils, "Tokenizer", SlowHubTokenizer)
+ monkeypatch.setattr("litellm.rust_bridge.tokenizer.from_pretrained", SlowHubTokenizer.from_pretrained)
monkeypatch.setattr(
"litellm.proxy.proxy_server.llm_router",
Router(
@@ -14914,7 +14914,7 @@ async def test_token_counter_loads_a_custom_tokenizer_off_the_event_loop(monkeyp
async def test_token_counter_loads_a_custom_tokenizer_once_per_identifier_revision_and_token(monkeypatch):
- from tokenizers import Tokenizer
+ from litellm.rust_bridge._native import Tokenizer
from litellm import Router
from litellm.types.router import DeploymentTypedDict
@@ -14931,7 +14931,7 @@ async def test_token_counter_loads_a_custom_tokenizer_once_per_identifier_revisi
},
}
- monkeypatch.setattr(litellm.utils, "Tokenizer", MagicMock(from_pretrained=from_pretrained))
+ monkeypatch.setattr("litellm.rust_bridge.tokenizer.from_pretrained", from_pretrained)
monkeypatch.setattr(
"litellm.proxy.proxy_server.llm_router",
Router(
diff --git a/tests/test_litellm/rust_bridge/test_catalog.py b/tests/test_litellm/rust_bridge/test_catalog.py
index 3fa5f3de7ab..82e3766e8a2 100644
--- a/tests/test_litellm/rust_bridge/test_catalog.py
+++ b/tests/test_litellm/rust_bridge/test_catalog.py
@@ -53,7 +53,7 @@ def test_shipped_decisions(
enabled: Final = environment == "1" if environment is not None else process is not False
assert catalog.rollout(context) is Rollout.RUST_OPT_OUT
assert catalog.decision(context) is (Decision.RUST_WITH_FALLBACK if enabled else Decision.PYTHON)
- elif route is Route.MESSAGES:
+ elif route in (Route.MESSAGES, Route.TOKEN_COUNTER, Route.TOKENIZER):
enabled: Final = environment == "1" if environment is not None else process is True
assert catalog.rollout(context) is Rollout.RUST_OPT_IN
assert catalog.decision(context) is (Decision.RUST_WITH_FALLBACK if enabled else Decision.PYTHON)
diff --git a/tests/test_litellm/rust_bridge/test_token_counter.py b/tests/test_litellm/rust_bridge/test_token_counter.py
index 71aa79cc4bb..3da291c898d 100644
--- a/tests/test_litellm/rust_bridge/test_token_counter.py
+++ b/tests/test_litellm/rust_bridge/test_token_counter.py
@@ -12,25 +12,32 @@ from types import MappingProxyType
from typing import Final
import pytest
-import tiktoken
-from tokenizers import Tokenizer
import litellm
from litellm.constants import TIKTOKEN_ENCODE_CHUNK_SIZE_CHARS
from litellm.litellm_core_utils.token_counter import openai_tokenizer_encoding
-from litellm.proxy.spend_tracking.budget_reservation import _count_input_tokens
+from litellm.proxy.spend_tracking.input_tokens import count_input_tokens, count_input_tokens_for_model
from litellm.rust_bridge import bindings, configuration
from litellm.rust_bridge import token_counter as bridge
+from litellm.rust_bridge import tokenizer as tokenizer_dispatch
+from litellm.rust_bridge._native import Tokenizer
from litellm.utils import claude_json_str
MODEL: Final = "claude-sonnet-4-5-20250929"
CL100K_MODEL: Final = "gpt-4"
O200K_MODEL: Final = "gpt-4o"
+MODEL_BY_TOKENIZER: Final[MappingProxyType[bridge.RustTokenizer, str]] = MappingProxyType(
+ {"anthropic": MODEL, "cl100k_base": CL100K_MODEL, "o200k_base": O200K_MODEL}
+)
TOKENIZERS: Final[tuple[bridge.RustTokenizer, ...]] = ("anthropic", "cl100k_base", "o200k_base")
-RANK_FILE_LINES: Final = MappingProxyType({"cl100k_base": 100_256, "o200k_base": 199_998})
BODY: Final = json.dumps({"model": MODEL, "messages": [{"role": "user", "content": "hello"}]}).encode()
+def _counted(body: dict[str, object], model: str) -> tuple[bytes, dict[str, object]]:
+ raw: Final = json.dumps({**body, "model": model}).encode()
+ return raw, json.loads(raw)
+
+
class _FakeDeclined(Exception):
pass
@@ -39,14 +46,41 @@ class _FakeUpstream(Exception):
pass
+class _FakeTokenizer:
+ """Stands in for one shared native `Tokenizer`; only its name identifies it."""
+
+ def __init__(self, name: str, json: str | None = None) -> None:
+ self.name = name
+ self.json = json
+
+
+def _fake_native_tokenizers(monkeypatch: pytest.MonkeyPatch, anthropic_json: str | None = None) -> None:
+ """Point the counter's tokenizer lookups at fakes while the bridge is faked; the codec path
+ keeps falling back to Python. Parity tests that restore the real extension get the real
+ lookups back."""
+ fakes: Final = {name: _FakeTokenizer(name) for name in ("cl100k_base", "o200k_base")}
+ anthropic: Final = _FakeTokenizer("anthropic", anthropic_json)
+ real_encoding: Final = tokenizer_dispatch.native_encoding
+ real_anthropic: Final = tokenizer_dispatch.native_anthropic
+
+ def faked() -> bool:
+ return isinstance(bindings.get_native_bridge(), _FakeNative)
+
+ monkeypatch.setattr(
+ tokenizer_dispatch, "native_encoding", lambda name: fakes[name] if faked() else real_encoding(name)
+ )
+ monkeypatch.setattr(tokenizer_dispatch, "native_anthropic", lambda: anthropic if faked() else real_anthropic())
+
+
class _FakeNative:
RustBridgeDeclined = _FakeDeclined
RustUpstreamError = _FakeUpstream
class _RecordingCounter:
- def __init__(self, tokenizer_json: str) -> None:
- self.tokenizer_json = tokenizer_json
+ def __init__(self, tokenizer: _FakeTokenizer, fast: bool) -> None:
+ self.tokenizer = tokenizer
+ self.fast = fast
self.bodies: list[bytes] = []
async def acount_request(self, body: bytes) -> object:
@@ -55,25 +89,16 @@ class _RecordingCounter:
class _RecordingFactory:
- """Stands in for the native `TokenCounter` class: callable for tokenizer JSON, `from_*_ranks` for rank files."""
+ """Stands in for the native `TokenCounter` class, built over a loaded `Tokenizer`."""
def __init__(self) -> None:
self.counters: list[_RecordingCounter] = []
- self.rank_files: list[str] = []
- def __call__(self, tokenizer_json: str) -> _RecordingCounter:
- counter = _RecordingCounter(tokenizer_json)
+ def from_tokenizer(self, tokenizer: _FakeTokenizer, fast: bool = False) -> _RecordingCounter:
+ counter = _RecordingCounter(tokenizer, fast)
self.counters.append(counter)
return counter
- def from_cl100k_ranks(self, rank_file: str) -> _RecordingCounter:
- self.rank_files.append(rank_file)
- return self("cl100k_base")
-
- def from_o200k_ranks(self, rank_file: str) -> _RecordingCounter:
- self.rank_files.append(rank_file)
- return self("o200k_base")
-
class _RaisingCounter:
def __init__(self, error: Exception) -> None:
@@ -89,13 +114,7 @@ class _RaisingFactory:
def __init__(self, error: Exception) -> None:
self.error = error
- def __call__(self, tokenizer_json: str) -> _RaisingCounter:
- return _RaisingCounter(self.error)
-
- def from_cl100k_ranks(self, rank_file: str) -> _RaisingCounter:
- return _RaisingCounter(self.error)
-
- def from_o200k_ranks(self, rank_file: str) -> _RaisingCounter:
+ def from_tokenizer(self, tokenizer: _FakeTokenizer, fast: bool = False) -> _RaisingCounter:
return _RaisingCounter(self.error)
@@ -105,6 +124,7 @@ def _reset_bridge(monkeypatch: pytest.MonkeyPatch):
bridge._counter.cache_clear()
configuration.reset_rust_configuration()
monkeypatch.setattr(bindings, "get_native_bridge", lambda: _FakeNative())
+ _fake_native_tokenizers(monkeypatch, anthropic_json=claude_json_str)
yield
bridge.TOKEN_COUNTER.reset()
bridge._counter.cache_clear()
@@ -117,8 +137,12 @@ async def test_disabled_bridge_never_constructs_a_counter(tokenizer: bridge.Rust
factory: Final = _RecordingFactory()
litellm.rust(False)
bridge.TOKEN_COUNTER.override(factory)
+ model: Final = MODEL_BY_TOKENIZER[tokenizer]
+ raw, request_body = _counted({"messages": [{"role": "user", "content": "hello"}]}, model)
- assert await bridge.count_input_tokens(BODY, tokenizer) is None
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=raw, models=(model,))
+
+ assert counts[model] == count_input_tokens_for_model(request_body=request_body, model=model)
assert factory.counters == []
@@ -128,31 +152,33 @@ async def test_enabled_bridge_returns_typed_count_and_reuses_one_counter() -> No
litellm.rust(True)
bridge.TOKEN_COUNTER.override(factory)
- first: Final = await bridge.count_input_tokens(BODY, "anthropic")
- second: Final = await bridge.count_input_tokens(BODY, "anthropic")
+ first: Final = await bridge.native_count(factory, "anthropic", BODY)
+ second: Final = await bridge.native_count(factory, "anthropic", BODY)
assert first == bridge.InputTokenCount(model=MODEL, input_tokens=42)
assert second == first
assert len(factory.counters) == 1
assert factory.counters[0].bodies == [BODY, BODY]
- assert json.loads(factory.counters[0].tokenizer_json)["model"]["type"] == "BPE"
+ assert factory.counters[0].fast is False
+ assert factory.counters[0].tokenizer is tokenizer_dispatch.native_anthropic()
+ assert json.loads(factory.counters[0].tokenizer.json or "")["model"]["type"] == "BPE"
@pytest.mark.asyncio
@pytest.mark.parametrize("tokenizer", ("cl100k_base", "o200k_base"))
-async def test_tiktoken_counter_is_built_from_the_vendored_rank_file_once(tokenizer: bridge.RustTokenizer) -> None:
+async def test_tiktoken_counter_is_built_over_the_shared_encoding_once(tokenizer: bridge.RustTokenizer) -> None:
factory: Final = _RecordingFactory()
litellm.rust(True)
bridge.TOKEN_COUNTER.override(factory)
- first: Final = await bridge.count_input_tokens(BODY, tokenizer)
- second: Final = await bridge.count_input_tokens(BODY, tokenizer)
+ first: Final = await bridge.native_count(factory, tokenizer, BODY)
+ second: Final = await bridge.native_count(factory, tokenizer, BODY)
assert first == second == bridge.InputTokenCount(model=MODEL, input_tokens=42)
- assert len(factory.rank_files) == 1
- assert factory.rank_files[0].startswith("IQ== 0\n")
- assert factory.rank_files[0].count("\n") == RANK_FILE_LINES[tokenizer]
- assert factory.counters[0].tokenizer_json == tokenizer
+ assert len(factory.counters) == 1
+ assert factory.counters[0].tokenizer.name == tokenizer
+ assert factory.counters[0].tokenizer is tokenizer_dispatch.native_encoding(tokenizer)
+ assert factory.counters[0].fast is False
assert factory.counters[0].bodies == [BODY, BODY]
@@ -162,13 +188,13 @@ async def test_each_tokenizer_gets_its_own_cached_counter() -> None:
litellm.rust(True)
bridge.TOKEN_COUNTER.override(factory)
- await bridge.count_input_tokens(BODY, "anthropic")
- await bridge.count_input_tokens(BODY, "cl100k_base")
- await bridge.count_input_tokens(BODY, "o200k_base")
- await bridge.count_input_tokens(BODY, "anthropic")
- await bridge.count_input_tokens(BODY, "o200k_base")
+ await bridge.native_count(factory, "anthropic", BODY)
+ await bridge.native_count(factory, "cl100k_base", BODY)
+ await bridge.native_count(factory, "o200k_base", BODY)
+ await bridge.native_count(factory, "anthropic", BODY)
+ await bridge.native_count(factory, "o200k_base", BODY)
- assert [counter.tokenizer_json for counter in factory.counters][1:] == ["cl100k_base", "o200k_base"]
+ assert [counter.tokenizer.name for counter in factory.counters] == ["anthropic", "cl100k_base", "o200k_base"]
assert [len(counter.bodies) for counter in factory.counters] == [2, 1, 2]
@@ -176,8 +202,11 @@ async def test_each_tokenizer_gets_its_own_cached_counter() -> None:
async def test_missing_native_module_falls_back(monkeypatch: pytest.MonkeyPatch) -> None:
litellm.rust(True)
monkeypatch.setattr(bindings, "get_native_bridge", lambda: None)
+ raw, request_body = _counted({"messages": [{"role": "user", "content": "hello"}]}, MODEL)
+
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=raw, models=(MODEL,))
- assert [await bridge.count_input_tokens(BODY, tokenizer) for tokenizer in TOKENIZERS] == [None, None, None]
+ assert counts[MODEL] == count_input_tokens_for_model(request_body=request_body, model=MODEL)
@pytest.mark.asyncio
@@ -185,8 +214,12 @@ async def test_missing_native_module_falls_back(monkeypatch: pytest.MonkeyPatch)
async def test_declined_request_falls_back(tokenizer: bridge.RustTokenizer) -> None:
litellm.rust(True)
bridge.TOKEN_COUNTER.override(_RaisingFactory(_FakeDeclined("request has no messages")))
+ model: Final = MODEL_BY_TOKENIZER[tokenizer]
+ raw, request_body = _counted({"messages": [{"role": "user", "content": "hello"}]}, model)
- assert await bridge.count_input_tokens(BODY, tokenizer) is None
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=raw, models=(model,))
+
+ assert counts[model] == count_input_tokens_for_model(request_body=request_body, model=model)
@pytest.mark.asyncio
@@ -194,8 +227,12 @@ async def test_declined_request_falls_back(tokenizer: bridge.RustTokenizer) -> N
async def test_runtime_failure_falls_back(tokenizer: bridge.RustTokenizer) -> None:
litellm.rust(True)
bridge.TOKEN_COUNTER.override(_RaisingFactory(RuntimeError("encode failed")))
+ model: Final = MODEL_BY_TOKENIZER[tokenizer]
+ raw, request_body = _counted({"messages": [{"role": "user", "content": "hello"}]}, model)
+
+ counts: Final = await count_input_tokens(request_body=request_body, raw_body=raw, models=(model,))
- assert await bridge.count_input_tokens(BODY, tokenizer) is None
+ assert counts[model] == count_input_tokens_for_model(request_body=request_body, model=model)
@pytest.mark.parametrize(
@@ -273,8 +310,8 @@ def test_rust_tokenizer_names_the_encoding_python_actually_counts_with(model: st
"Hello, world! camelCase ABCdef \u00e9\u00e8 12345 \u3053\u3093\u306b\u3061\u306f <|endoftext|>\r\n" * 9
)
python_count: Final = litellm.token_counter(model=model, text=text)
- cl100k_count: Final = len(tiktoken.get_encoding("cl100k_base").encode(text, disallowed_special=()))
- o200k_count: Final = len(tiktoken.get_encoding("o200k_base").encode(text, disallowed_special=()))
+ cl100k_count: Final = Tokenizer.from_tiktoken("cl100k_base").count(text)
+ o200k_count: Final = Tokenizer.from_tiktoken("o200k_base").count(text)
assert cl100k_count != o200k_count
match bridge.rust_tokenizer(model):
case "cl100k_base":
@@ -282,7 +319,7 @@ def test_rust_tokenizer_names_the_encoding_python_actually_counts_with(model: st
case "o200k_base":
assert python_count == o200k_count
case "anthropic":
- assert python_count == len(Tokenizer.from_str(claude_json_str).encode(text).ids)
+ assert python_count == Tokenizer.from_json(claude_json_str).count(text)
assert python_count not in {cl100k_count, o200k_count}
case None:
pytest.fail(f"{model} must have a Rust tokenizer")
@@ -386,12 +423,11 @@ async def test_native_count_matches_python_budget_counter(
litellm.rust(True)
body: Final = json.dumps(request_body).replace(MODEL, model)
- rust_count: Final = await bridge.count_input_tokens(body.encode(), tokenizer)
- python_count: Final = _count_input_tokens(request_body=json.loads(body), model=model)
+ request_body_parsed: Final = json.loads(body)
+ counts: Final = await count_input_tokens(request_body=request_body_parsed, raw_body=body.encode(), models=(model,))
+ python_count: Final = count_input_tokens_for_model(request_body=request_body_parsed, model=model)
- assert rust_count is not None
- assert rust_count.model == json.loads(body).get("model")
- assert rust_count.input_tokens == python_count
+ assert counts[model] == python_count
@pytest.mark.asyncio
@@ -405,15 +441,14 @@ async def test_tiktoken_counts_long_text_exactly_where_python_chunks(
litellm.rust(True)
text: Final = "x " * 20_000
body: Final = {"model": model, "messages": [{"role": "user", "content": text}]}
- encoding: Final = tiktoken.get_encoding(tokenizer)
- exact: Final = 3 + len(encoding.encode("user")) + len(encoding.encode(text)) + 3
+ encoding: Final = Tokenizer.from_tiktoken(tokenizer)
+ exact: Final = 3 + encoding.count("user") + encoding.count(text) + 3
chunks: Final = -(-len(text) // TIKTOKEN_ENCODE_CHUNK_SIZE_CHARS)
- rust_count: Final = await bridge.count_input_tokens(json.dumps(body).encode(), tokenizer)
- python_count: Final = _count_input_tokens(request_body=body, model=model)
+ counts: Final = await count_input_tokens(request_body=body, raw_body=json.dumps(body).encode(), models=(model,))
+ python_count: Final = count_input_tokens_for_model(request_body=body, model=model)
- assert rust_count is not None
- assert rust_count.input_tokens == exact
+ assert counts[model] == exact
assert python_count is not None
assert exact < python_count <= exact + chunks
@@ -440,5 +475,9 @@ async def test_native_declines_shapes_python_prices_differently(
native: Final = pytest.importorskip("litellm.rust_bridge._native")
monkeypatch.setattr(bindings, "get_native_bridge", lambda: native)
litellm.rust(True)
+ model: Final = MODEL_BY_TOKENIZER[tokenizer]
+ raw, parsed = _counted(request_body, model)
+
+ counts: Final = await count_input_tokens(request_body=parsed, raw_body=raw, models=(model,))
- assert await bridge.count_input_tokens(json.dumps(request_body).encode(), tokenizer) is None
+ assert counts.get(model) == count_input_tokens_for_model(request_body=parsed, model=model)
diff --git a/tests/test_litellm/rust_bridge/test_tokenizer.py b/tests/test_litellm/rust_bridge/test_tokenizer.py
new file mode 100644
index 00000000000..0de7ad50b1e
--- /dev/null
+++ b/tests/test_litellm/rust_bridge/test_tokenizer.py
@@ -0,0 +1,134 @@
+from collections.abc import Generator
+from typing import Final
+
+import pytest
+import tiktoken
+from tokenizers import Tokenizer
+
+import litellm
+from litellm.litellm_core_utils.tokenizer import HuggingFaceTokenizer, OpenAIEncoding
+from litellm.rust_bridge import configuration, tokenizer
+from litellm.utils import _select_tokenizer
+from tests.test_litellm.litellm_core_utils.test_decode_special_tokens import TOKENIZER_JSON
+
+
[email protected](autouse=True)
+def isolated_configuration(monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
+ monkeypatch.delenv("LITELLM_RUST", raising=False)
+ configuration.reset_rust_configuration()
+ yield
+ tokenizer.TOKENIZER.reset()
+ configuration.reset_rust_configuration()
+
+
[email protected]("environment", (None, "0", "1"))
[email protected]("process", (None, False, True))
+def test_tokenizer_factories_follow_rollout(
+ monkeypatch: pytest.MonkeyPatch, environment: str | None, process: bool | None
+) -> None:
+ configuration.rust(process)
+ if environment is not None:
+ monkeypatch.setenv("LITELLM_RUST", environment)
+ enabled: Final = environment == "1" if environment is not None else process is True
+ encoding: Final = tokenizer.get_encoding("cl100k_base")
+ custom: Final = litellm.create_tokenizer(TOKENIZER_JSON)
+ reference: Final = Tokenizer.from_str(TOKENIZER_JSON)
+
+ assert isinstance(encoding, OpenAIEncoding if enabled else tiktoken.Encoding)
+ assert isinstance(custom["tokenizer"], HuggingFaceTokenizer if enabled else Tokenizer)
+ assert encoding.encode("café 漢字 🙂") == tiktoken.get_encoding(encoding.name).encode("café 漢字 🙂")
+ assert litellm.encode(text="Hello World", custom_tokenizer=custom) == reference.encode("Hello World").ids
+ assert litellm.token_counter(text="Hello World", custom_tokenizer=custom) == len(reference.encode("Hello World"))
+
+
+def test_missing_native_binding_keeps_python_tokenizer_api() -> None:
+ configuration.rust(True)
+ tokenizer.TOKENIZER.override(None)
+ encoding: Final = tokenizer.get_encoding("cl100k_base")
+ custom: Final = litellm.create_tokenizer(TOKENIZER_JSON)["tokenizer"]
+
+ assert isinstance(encoding, tiktoken.Encoding)
+ assert isinstance(custom, Tokenizer)
+ custom.enable_padding(pad_id=0, pad_token="[UNK]")
+ assert [item.ids for item in custom.encode_batch(["Hello", "Hello World"])] == [[3, 1, 0], [3, 1, 2]]
+
+
+def test_cached_selection_follows_backend_changes(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(litellm, "disable_hf_tokenizer_download", True)
+ configuration.rust(True)
+ native: Final = _select_tokenizer("dispatch-fixture")["tokenizer"]
+ configuration.rust(False)
+ python: Final = _select_tokenizer("dispatch-fixture")["tokenizer"]
+
+ assert isinstance(native, OpenAIEncoding)
+ assert isinstance(python, tiktoken.Encoding)
+ assert native.encode("hello") == python.encode("hello")
+
+
+def test_declined_native_factory_falls_back_before_tokenizing() -> None:
+ from litellm.rust_bridge._native import RustBridgeDeclined
+
+ class UnavailableTokenizer:
+ @staticmethod
+ def from_json(json: str) -> None:
+ raise RustBridgeDeclined("huggingface feature is disabled")
+
+ configuration.rust(True)
+ binding: Final = tokenizer._as_factory(UnavailableTokenizer)
+ tokenizer.TOKENIZER.override(binding)
+ custom: Final = litellm.create_tokenizer(TOKENIZER_JSON)
+
+ assert isinstance(custom["tokenizer"], Tokenizer)
+ assert (
+ litellm.decode(tokens=litellm.encode(text="Hello World", custom_tokenizer=custom), custom_tokenizer=custom)
+ == "Hello World"
+ )
+
+
+ ("model", "text"),
+ (
+ ("gpt-4o", "hello <|endoftext|> world"),
+ ("gpt-3.5-turbo", "café 漢字 🙂"),
+ ("text-davinci-003", " def f():\n return 1\n"),
+ ("tokenizer-parity-fixture", "<SOS>hello<EOT> again"),
+ ),
+)
+def test_public_token_api_is_identical_across_backends(monkeypatch: pytest.MonkeyPatch, model: str, text: str) -> None:
+ """`litellm.token_counter`, `encode` and `decode` return the same values whichever backend
+ the catalog picks; only the object types differ."""
+ monkeypatch.setattr(litellm, "anthropic_models", {*litellm.anthropic_models, "tokenizer-parity-fixture"})
+ messages: Final = [{"role": "user", "content": text}, {"role": "assistant", "content": "ok"}]
+
+ def observe() -> tuple[int, int, list[int], str]:
+ ids: Final = litellm.encode(model=model, text=text)
+ return (
+ litellm.token_counter(model=model, text=text),
+ litellm.token_counter(model=model, messages=messages),
+ ids,
+ litellm.decode(model=model, tokens=ids),
+ )
+
+ configuration.rust(False)
+ python: Final = observe()
+ configuration.rust(True)
+ rust: Final = observe()
+
+ assert rust == python
+
+
+def test_cached_huggingface_tokenizers_follow_backend_changes(monkeypatch: pytest.MonkeyPatch) -> None:
+ from litellm.litellm_core_utils.tokenizer import HuggingFaceTokenizer as RustHuggingFaceTokenizer
+ from litellm.utils import _load_huggingface_tokenizer
+
+ monkeypatch.setattr(litellm, "anthropic_models", {*litellm.anthropic_models, "tokenizer-cache-fixture"})
+ _load_huggingface_tokenizer.cache_clear()
+ configuration.rust(True)
+ native: Final = _select_tokenizer("tokenizer-cache-fixture")["tokenizer"]
+ configuration.rust(False)
+ python: Final = _select_tokenizer("tokenizer-cache-fixture")["tokenizer"]
+ configuration.rust(True)
+
+ assert isinstance(native, RustHuggingFaceTokenizer)
+ assert isinstance(python, Tokenizer)
+ assert _select_tokenizer("tokenizer-cache-fixture")["tokenizer"] is native
diff --git a/tests/test_litellm/rust_bridge/test_verify_linux_native_wheel.py b/tests/test_litellm/rust_bridge/test_verify_linux_native_wheel.py
index e449d4392d8..0eae041f535 100644
--- a/tests/test_litellm/rust_bridge/test_verify_linux_native_wheel.py
+++ b/tests/test_litellm/rust_bridge/test_verify_linux_native_wheel.py
@@ -56,10 +56,11 @@ def _write_wheel(
metadata_tags: tuple[str, ...] | None = (_EXPECTED_TAG,),
dist_info: str = _DIST_INFO,
duplicate_wheel: bool = False,
+ native_bytes: bytes = b"synthetic native extension",
) -> Path:
wheel: Final = tmp_path / f"litellm-1.100.0-{filename_tag}.whl"
with zipfile.ZipFile(wheel, "w", compression=zipfile.ZIP_DEFLATED) as archive:
- archive.writestr(_NATIVE_MEMBER, b"synthetic native extension")
+ archive.writestr(_NATIVE_MEMBER, native_bytes)
archive.writestr(
f"{dist_info}/METADATA",
"Metadata-Version: 2.1\nName: litellm\nVersion: 1.100.0\n",
@@ -195,3 +196,17 @@ def test_rejects_production_module_exposing_panic_hook(tmp_path: Path) -> None:
wheel: Final = _write_wheel(tmp_path, filename_tag=_EXPECTED_TAG)
assert _run_verifier(wheel, exposes_panic=True) == 1
+
+
[email protected]("embedded", (False, True))
+def test_vocabulary_is_packaged_once(tmp_path: Path, embedded: bool) -> None:
+ ranks: Final = b"AA== 0\nAQ== 1\nAg== 2\n"
+ wheel: Final = _write_wheel(
+ tmp_path,
+ filename_tag=_EXPECTED_TAG,
+ native_bytes=b"native engine" + (ranks if embedded else b""),
+ )
+ with zipfile.ZipFile(wheel, "a") as archive:
+ archive.writestr("litellm/litellm_core_utils/tokenizers/" + "a" * 40, ranks)
+
+ assert _run_verifier(wheel) == (1 if embedded else 0)
diff --git a/tests/test_litellm_rust/test_fork_guard.py b/tests/test_litellm_rust/test_fork_guard.py
index 086397bab5c..2a8fb6f9fca 100644
--- a/tests/test_litellm_rust/test_fork_guard.py
+++ b/tests/test_litellm_rust/test_fork_guard.py
@@ -1,5 +1,6 @@
import os
import textwrap
+from typing import Final
import pytest
@@ -144,3 +145,76 @@ def test_sdk_call_in_a_child_forked_after_native_use_raises_instead_of_hanging()
result = run_child_interpreter(_SDK_CONTRACT, env=env, timeout=120)
assert result.returncode == 0, result.stderr
+
+
[email protected](not hasattr(os, "fork"), reason="fork only")
[email protected]("warm_fast_counter", (False, True))
+def test_tokenizers_share_the_native_process_guard(warm_fast_counter: bool) -> None:
+ script: Final = """
+import asyncio
+import os
+import litellm
+from litellm.proxy.spend_tracking.input_tokens import count_input_tokens
+from litellm.rust_bridge import _native
+from litellm.litellm_core_utils.tokenizer import HuggingFaceTokenizer
+from litellm.utils import claude_json_str
+
+litellm.anthropic_models = {*litellm.anthropic_models, "tokenizer-fork-fixture"}
+_native.reserve_process_for_forking()
+for create in (
+ lambda: _native.Tokenizer.from_tiktoken("cl100k_base"),
+ lambda: _native.Tokenizer.from_json(claude_json_str),
+ lambda: litellm.token_counter(model="tokenizer-fork-fixture", text="hello"),
+):
+ try:
+ create()
+ except _native.ProcessReservedForForking:
+ pass
+ else:
+ raise AssertionError("reserved parent ran a native tokenizer")
+assert not _native.process_state_started()
+
+pid = os.fork()
+if pid == 0:
+ tokenizer = HuggingFaceTokenizer.from_str(claude_json_str)
+ encoding = _native.Tokenizer.from_tiktoken("cl100k_base")
+ if os.environ["WARM_FAST_COUNTER"] == "True":
+ _native.TokenCounter.from_tokenizer(encoding, fast=True)
+ expected = [item.ids for item in tokenizer.encode_batch(["hello", "world"])]
+ assert _native.process_state_started()
+ grandchild = os.fork()
+ if grandchild == 0:
+ for call in (
+ lambda: tokenizer.encode_batch(["hello", "world"]),
+ lambda: tokenizer.encode("hello"),
+ lambda: encoding.count("hello"),
+ lambda: encoding.count("hello", fast=True),
+ lambda: _native.TokenCounter.from_tokenizer(encoding),
+ lambda: _native.TokenCounter.from_tokenizer(encoding, fast=True),
+ lambda: _native.Tokenizer.from_tiktoken("cl100k_base"),
+ lambda: asyncio.run(count_input_tokens({"prompt": "hello"}, b'{"prompt": "hello"}', ("counter-fork-fixture",))),
+ ):
+ try:
+ call()
+ except _native.ForkedAfterNativeRuntimeStarted:
+ pass
+ else:
+ os._exit(1)
+ os._exit(0)
+ assert os.waitpid(grandchild, 0)[1] == 0
+ assert [item.ids for item in tokenizer.encode_batch(["hello", "world"])] == expected
+ os._exit(0)
+assert os.waitpid(pid, 0)[1] == 0
+"""
+ result: Final = run_child_interpreter(
+ script,
+ env={
+ **os.environ,
+ "OBJC_DISABLE_INITIALIZE_FORK_SAFETY": "YES",
+ "LITELLM_LOCAL_MODEL_COST_MAP": "True",
+ "WARM_FAST_COUNTER": str(warm_fast_counter),
+ },
+ timeout=30,
+ )
+
+ assert result.returncode == 0, result.stderr
diff --git a/tests/test_litellm_rust/test_tokenizer.py b/tests/test_litellm_rust/test_tokenizer.py
new file mode 100644
index 00000000000..98d5259b652
--- /dev/null
+++ b/tests/test_litellm_rust/test_tokenizer.py
@@ -0,0 +1,130 @@
+import json
+from typing import Final
+
+import pytest
+import tiktoken
+from tokenizers import Tokenizer as ReferenceTokenizer
+
+from litellm.rust_bridge import _native
+from litellm.utils import claude_json_str
+from tests.test_litellm.litellm_core_utils.test_decode_special_tokens import TOKENIZER_JSON
+
+pytestmark = pytest.mark.requires_rust_extension
+
+
+def test_tiktoken_codec_round_trips_and_counts() -> None:
+ tokenizer: Final = _native.Tokenizer.from_tiktoken("cl100k_base")
+ encoded: Final = tokenizer.encode("hello world")
+
+ assert tokenizer.name == "cl100k_base"
+ assert tokenizer.count("hello world") == len(encoded)
+ assert tokenizer.decode(encoded) == "hello world"
+
+
+def test_huggingface_codec_skips_special_tokens() -> None:
+ tokenizer: Final = _native.Tokenizer.from_json(claude_json_str)
+ encoded: Final = tokenizer.encode("<SOS>hello<EOT>")
+
+ assert "<SOS>" in tokenizer.decode(encoded, skip_special_tokens=False)
+ assert tokenizer.decode(encoded, skip_special_tokens=True) == "hello"
+
+
+def test_tiktoken_codec_keeps_the_requested_encoding_name() -> None:
+ assert _native.Tokenizer.from_tiktoken("gpt2").name == "gpt2"
+ assert _native.Tokenizer.from_tiktoken("r50k_base").name == "r50k_base"
+ assert _native.Tokenizer.from_tiktoken("gpt2").encode("hi") == _native.Tokenizer.from_tiktoken("r50k_base").encode(
+ "hi"
+ )
+
+
+def test_tiktoken_codec_exposes_its_vocabulary() -> None:
+ reference: Final = tiktoken.get_encoding("cl100k_base")
+ tokenizer: Final = _native.Tokenizer.from_tiktoken("cl100k_base")
+
+ assert tokenizer.special_tokens() == reference._special_tokens
+ assert tokenizer.max_token_value() == reference.max_token_value
+ assert tokenizer.token_byte_values() == reference.token_byte_values()
+ assert tokenizer.encode_single_token(b"hello") == reference.encode_single_token("hello")
+ assert tokenizer.is_special_token(reference.eot_token) and not tokenizer.is_special_token(0)
+ with pytest.raises(KeyError):
+ tokenizer.encode_single_token(b"<|not-a-token|>")
+
+
+def test_huggingface_codec_rejects_tiktoken_only_calls() -> None:
+ tokenizer: Final = _native.Tokenizer.from_json(claude_json_str)
+ with pytest.raises(ValueError, match="requires a tiktoken encoding"):
+ tokenizer.token_byte_values()
+ with pytest.raises(ValueError, match="requires a Hugging Face tokenizer"):
+ _native.Tokenizer.from_tiktoken("cl100k_base").get_vocab()
+
+
+def test_unknown_tiktoken_encoding_raises_value_error() -> None:
+ with pytest.raises(ValueError, match="unsupported tokenizer"):
+ _native.Tokenizer.from_tiktoken("unknown-encoding")
+
+
+def test_tiktoken_codec_decodes_truncated_unicode_like_python() -> None:
+ reference: Final = tiktoken.get_encoding("cl100k_base")
+ tokenizer: Final = _native.Tokenizer.from_tiktoken(reference.name)
+ encoded: Final = reference.encode("🙂漢字")
+
+ assert tuple(tokenizer.decode(encoded[:end]) for end in range(1, len(encoded) + 1)) == tuple(
+ reference.decode(encoded[:end]) for end in range(1, len(encoded) + 1)
+ )
+
+
+FAST_TEXTS: Final = (
+ "",
+ "hello world <|endoftext|>",
+ "café 漢字 ع 🙂 line\r\n indented 123456789",
+ "<SOS>x<EOT> a\u0301 fi",
+)
+
+
+def test_fast_counting_is_an_opt_in_over_the_same_loaded_tokenizer() -> None:
+ for tokenizer in (
+ _native.Tokenizer.from_tiktoken("cl100k_base"),
+ _native.Tokenizer.from_tiktoken("o200k_base"),
+ _native.Tokenizer.from_json(claude_json_str),
+ ):
+ assert [tokenizer.count(text, fast=True) for text in FAST_TEXTS] == [
+ tokenizer.count(text) for text in FAST_TEXTS
+ ]
+
+
+ "name", ("cl100k_base", "o200k_base", "o200k_harmony", "p50k_base", "p50k_edit", "r50k_base", "gpt2")
+)
+async def test_token_counter_counts_over_a_shared_tokenizer(name: str) -> None:
+ messages: Final = [{"role": "user", "content": "hello wide world"}, {"role": "assistant", "content": "ok"}]
+ body: Final = json.dumps({"model": "gpt-4", "messages": messages}).encode()
+ tokenizer: Final = _native.Tokenizer.from_tiktoken(name)
+ reference: Final = tiktoken.get_encoding(name)
+ for text in FAST_TEXTS:
+ assert tokenizer.count(text, fast=True) == tokenizer.count(text) == len(reference.encode_ordinary(text))
+
+ exact: Final = await _native.TokenCounter.from_tokenizer(tokenizer).acount_request(body)
+ fast: Final = await _native.TokenCounter.from_tokenizer(tokenizer, fast=True).acount_request(body)
+
+ assert exact == fast
+ assert exact["input_tokens"] == 3 + sum(
+ 3 + len(reference.encode_ordinary(message["role"])) + len(reference.encode_ordinary(message["content"]))
+ for message in messages
+ )
+
+
[email protected]("configured", (False, True))
+async def test_fast_count_preserves_huggingface_configuration(configured: bool) -> None:
+ reference: Final = ReferenceTokenizer.from_str(TOKENIZER_JSON)
+ if configured:
+ reference.enable_truncation(max_length=3)
+ reference.enable_padding(pad_id=0, pad_token="[UNK]", length=5)
+ tokenizer: Final = _native.Tokenizer.from_json(reference.to_str())
+ counter: Final = _native.TokenCounter.from_tokenizer(tokenizer, fast=True)
+ for text in ("", "Hello", "Hello World Hello World", "[BOS] Hello"):
+ expected: Final = len(reference.encode(text))
+ assert tokenizer.count(text, fast=True) == tokenizer.count(text) == expected
+ result: Final = await counter.acount_request(json.dumps({"prompt": text}).encode())
+ assert result["input_tokens"] == expected
diff --git a/uv.lock b/uv.lock
index 18d57aa991b..32d7580f61b 100644
--- a/uv.lock
+++ b/uv.lock
@@ -1162,14 +1162,11 @@ wheels = [
[[package]]
name = "click"
-version = "8.3.3"
+version = "8.5.0"
source = { registry = "https://pypi.org/simple" }
-dependencies = [
- { name = "colorama", marker = "sys_platform == 'win32'" },
-]
-sdist = { url = "https://files.pythonhosted.org/packages/bb/63/f9e1ea081ce35720d8b92acde70daaedace594dc93b693c869e0d5910718/click-8.3.3.tar.gz", hash = "sha256:398329ad4837b2ff7cbe1dd166a4c0f8900c3ca3a218de04466f38f6497f18a2", size = 328061, upload-time = "2026-04-22T15:11:27.506Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/c7/0e/7fa0ef50764b67090eca4114772a2abf8b6148198475e54c660b97caeee6/click-8.5.0.tar.gz", hash = "sha256:ba0d2089de75ea0310e2dde03160e6ca10009947fb95a182f9b54021bb272e34", size = 382235, upload-time = "2026-08-26T13:33:14.56Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/ae/44/c1221527f6a71a01ec6fbad7fa78f1d50dfa02217385cf0fa3eec7087d59/click-8.3.3-py3-none-any.whl", hash = "sha256:a2bf429bb3033c89fa4936ffb35d5cb471e3719e1f3c8a7c3fff0b8314305613", size = 110502, upload-time = "2026-04-22T15:11:25.044Z" },
+ { url = "https://files.pythonhosted.org/packages/58/50/6c0d534c5f134586a8e1ba4e330569e32f057e33372ae556463212fb4cd3/click-8.5.0-py3-none-any.whl", hash = "sha256:255bc9599cf7748b4b1a446ccc735421bd08a2ae529a8b88597d3de5664ee360", size = 125251, upload-time = "2026-08-26T13:33:12.928Z" },
]
[[package]]
@@ -1989,11 +1986,11 @@ wheels = [
[[package]]
name = "filelock"
-version = "3.29.0"
+version = "3.32.7"
source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/b5/fe/997687a931ab51049acce6fa1f23e8f01216374ea81374ddee763c493db5/filelock-3.29.0.tar.gz", hash = "sha256:69974355e960702e789734cb4871f884ea6fe50bd8404051a3530bc07809cf90", size = 57571, upload-time = "2026-04-19T15:39:10.068Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/0f/59/e19834834cb01a32febfbb0f8a23a9088088f5d45991824ff2bc3b5e8acb/filelock-3.32.7.tar.gz", hash = "sha256:37b8a3d9811b0f9aef7e5ec5c71bb320de52df51e6ca9bcd6f5ad81187660da7", size = 225154, upload-time = "2026-09-16T00:24:20.907Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/81/47/dd9a212ef6e343a6857485ffe25bba537304f1913bdbed446a23f7f592e1/filelock-3.29.0-py3-none-any.whl", hash = "sha256:96f5f6344709aa1572bbf631c640e4ebeeb519e08da902c39a001882f30ac258", size = 39812, upload-time = "2026-04-19T15:39:08.752Z" },
+ { url = "https://files.pythonhosted.org/packages/15/df/31098c5aeb4d966b553641472bd55fcf5fdfac953549894b8a765ba44e91/filelock-3.32.7-py3-none-any.whl", hash = "sha256:65ff0d0190ea42038b32bda4b77834fb05be2cad4c5b9b01aa4dfb3614536e52", size = 100157, upload-time = "2026-09-16T00:24:19.543Z" },
]
[[package]]
@@ -2225,11 +2222,11 @@ wheels = [
[[package]]
name = "fsspec"
-version = "2026.4.0"
+version = "2026.7.0"
source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/d5/8d/1c51c094345df128ca4a990d633fe1a0ff28726c9e6b3c41ba65087bba1d/fsspec-2026.4.0.tar.gz", hash = "sha256:301d8ac70ae90ef3ad05dcf94d6c3754a097f9b5fe4667d2787aa359ec7df7e4", size = 312760, upload-time = "2026-04-29T20:42:38.635Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/00/78/f34251dadb8f3921264a1d9b8946f5e542014ee2614b285261b4e40e6775/fsspec-2026.7.0.tar.gz", hash = "sha256:c803c40f4cf860b49dea58ee3e1c33cb9c790520e233537e1340049f89b82a88", size = 317040, upload-time = "2026-07-28T16:34:51.052Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/d5/0c/043d5e551459da400957a1395e0febbf771446ff34291afcbe3d8be2a279/fsspec-2026.4.0-py3-none-any.whl", hash = "sha256:11ef7bb35dab8a394fde6e608221d5cf3e8499401c249bebaeaad760a1a8dec2", size = 203402, upload-time = "2026-04-29T20:42:36.842Z" },
+ { url = "https://files.pythonhosted.org/packages/fd/3c/6a2bf344106328fd04963664a60b9bb6496fc25df8e962fcdc1367285fb9/fsspec-2026.7.0-py3-none-any.whl", hash = "sha256:b57ddbafedfaef7018c1ecab32aa200a9d7ca26b77965f64e48b70061249d279", size = 206583, upload-time = "2026-07-28T16:34:49.538Z" },
]
[[package]]
@@ -3146,34 +3143,26 @@ wheels = [
[[package]]
name = "hf-xet"
-version = "1.5.0"
+version = "1.6.0"
source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/74/d8/5c06fc76461418326a7decf8367480c35be11a41fd938633929c60a9ec6b/hf_xet-1.5.0.tar.gz", hash = "sha256:e0fb0a34d9f406eed88233e829a67ec016bec5af19e480eac65a233ea289a948", size = 837196, upload-time = "2026-05-06T06:18:15.583Z" }
-wheels = [
- { url = "https://files.pythonhosted.org/packages/68/9b/6912c99070915a4f28119e3c5b52a9abd1eec0ad5cb293b8c967a0c6f5a2/hf_xet-1.5.0-cp313-cp313t-macosx_10_12_x86_64.whl", hash = "sha256:7d70fe2ce97b9db73b9c9b9c81fe3693640aec83416a966c446afea54acfae3c", size = 4023383, upload-time = "2026-05-06T06:17:53.947Z" },
- { url = "https://files.pythonhosted.org/packages/0f/6d/9563cfde59b5d8128a9c7ec972a087f4c782e4f7bac5a85234edfd5d5e49/hf_xet-1.5.0-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:73a0dae8c71de3b0633a45c73f4a4a5ed09e94b43441d82981a781d4f12baa42", size = 3792751, upload-time = "2026-05-06T06:17:51.791Z" },
- { url = "https://files.pythonhosted.org/packages/07/a5/ed5a0cf35b49a0571af5a8f53416dad1877a718c021c9937c3a53cb45781/hf_xet-1.5.0-cp313-cp313t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a60290ec57e9b71767fba7c3645ddafdd0759974b540441510c629c6db6db24a", size = 4456058, upload-time = "2026-05-06T06:17:40.735Z" },
- { url = "https://files.pythonhosted.org/packages/60/fb/3ae8bf2a7a37a4197d0195d7247fd25b3952e15cb8a599e285dfaa6f52b3/hf_xet-1.5.0-cp313-cp313t-manylinux_2_28_aarch64.whl", hash = "sha256:e5de0f6deada0dada870bb376a11bcd1f08abf3a968a6d118f33e72d1b1eb480", size = 4250783, upload-time = "2026-05-06T06:17:38.412Z" },
- { url = "https://files.pythonhosted.org/packages/a2/9b/8bae40d4d91525085137196e84eb0ed49cf65b5e96e5c3ecdadd8bd0fac2/hf_xet-1.5.0-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:c799d49f1a5544a0ef7591c0ee75e0d6b93d6f56dc7a4979f59f7518d2872216", size = 4445594, upload-time = "2026-05-06T06:18:04.219Z" },
- { url = "https://files.pythonhosted.org/packages/13/59/c74efbbd4e8728172b2cc72a2bc014d2947a4b7bdced932fbd3f5da1a4e5/hf_xet-1.5.0-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:2baea1b0b989e5c152fe81425f7745ddc8901280ba3d97c98d8cdece7b706c60", size = 4663995, upload-time = "2026-05-06T06:18:06.1Z" },
- { url = "https://files.pythonhosted.org/packages/73/32/8e1e0410af64cda9b139d1dcebdc993a8ff9c8c7c0e2696ae356d75ccc0d/hf_xet-1.5.0-cp313-cp313t-win_amd64.whl", hash = "sha256:526345b3ed45f374f6317349df489167606736c876241ba984105afe7fd4839d", size = 3966608, upload-time = "2026-05-06T06:18:19.74Z" },
- { url = "https://files.pythonhosted.org/packages/fc/34/a8febc8f4edbea8b3e21b02ebc8b628679b84ba7e45cde624a7736b51500/hf_xet-1.5.0-cp313-cp313t-win_arm64.whl", hash = "sha256:786d28e2eb8315d5035544b9d137b4a842d600c434bb91bf7d0d953cce906ad4", size = 3796946, upload-time = "2026-05-06T06:18:17.568Z" },
- { url = "https://files.pythonhosted.org/packages/2a/20/8fc8996afe5815fa1a6be8e9e5c02f24500f409d599e905800d498a4e14d/hf_xet-1.5.0-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:872d5601e6deea30d15865ede55d29eac6daf5a534ab417b99b6ef6b076dd96c", size = 4023495, upload-time = "2026-05-06T06:18:01.94Z" },
- { url = "https://files.pythonhosted.org/packages/32/6a/93d84463c00cecb561a7508aa6303e35ee2894294eac14245526924415fe/hf_xet-1.5.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:9929561f5abf4581c8ea79587881dfef6b8abb2a0d8a51915936fc2a614f4e73", size = 3792731, upload-time = "2026-05-06T06:18:00.021Z" },
- { url = "https://files.pythonhosted.org/packages/9d/5a/8ec8e0c863b382d00b3c2e2af6ded6b06371be617144a625903a6d562f4b/hf_xet-1.5.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:f7b7bbae318e583a86fb21e5a4a175d6721d628a2874f4bd022d0e660c32a682", size = 4456738, upload-time = "2026-05-06T06:17:49.574Z" },
- { url = "https://files.pythonhosted.org/packages/c5/ca/f7effa1a67717da2bcc6b6c28f71c6ca648c77acaec4e2c32f40cbe16d85/hf_xet-1.5.0-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:cf7b2dc6f31a4ea754bb50f74cde482dcf5d366d184076d8530b9872787f3761", size = 4251622, upload-time = "2026-05-06T06:17:47.096Z" },
- { url = "https://files.pythonhosted.org/packages/65/f2/19247dba3e231cf77dec59ddfb878f00057635ff773d099c9b59d37812c3/hf_xet-1.5.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:8dbcbab554c9ef158ef2c991545c3e970ddd8cc7acdcd0a78c5a41095dab4ded", size = 4445667, upload-time = "2026-05-06T06:18:11.983Z" },
- { url = "https://files.pythonhosted.org/packages/7f/64/6f116801a3bcfb6f59f5c251f48cadc47ea54026441c4a385079286a94fa/hf_xet-1.5.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:5906bf7718d3636dc13402914736abe723492cb730f744834f5f5b67d3a12702", size = 4664619, upload-time = "2026-05-06T06:18:13.771Z" },
- { url = "https://files.pythonhosted.org/packages/5c/e8/069542d37946ed08669b127e1496fa99e78196d71de8d41eda5e9f1b7a58/hf_xet-1.5.0-cp314-cp314t-win_amd64.whl", hash = "sha256:5f3dc2248fc01cc0a00cd392ab497f1ca373fcbc7e3f2da1f452480b384e839e", size = 3966802, upload-time = "2026-05-06T06:18:28.162Z" },
- { url = "https://files.pythonhosted.org/packages/f9/91/fc6fdec27b14d04e88c386ac0a0129732b53fa23f7c4a78f4b83a039c567/hf_xet-1.5.0-cp314-cp314t-win_arm64.whl", hash = "sha256:b285cea1b5bab46b758772716ba8d6854a1a0310fed1c249d678a8b38601e5a0", size = 3797168, upload-time = "2026-05-06T06:18:26.287Z" },
- { url = "https://files.pythonhosted.org/packages/3d/fb/69ff198a82cae7eb1a69fb84d93b3a3e4816564d76817fe541ddc96874eb/hf_xet-1.5.0-cp37-abi3-macosx_10_12_x86_64.whl", hash = "sha256:dad0dc84e941b8ba3c860659fe1fdc35c049d47cce293f003287757e971a8f56", size = 4030814, upload-time = "2026-05-06T06:17:57.933Z" },
- { url = "https://files.pythonhosted.org/packages/9b/ff/edcc2b40162bef3ff78e14ab637e5f3b89243d6aee72f5949d3bb6a5af83/hf_xet-1.5.0-cp37-abi3-macosx_11_0_arm64.whl", hash = "sha256:fd6e5a9b0fdac4ed03ed45ef79254a655b1aaab514a02202617fbf643f5fdf7a", size = 3798444, upload-time = "2026-05-06T06:17:55.79Z" },
- { url = "https://files.pythonhosted.org/packages/49/4d/103f76b04310e5e57656696cc184690d20c466af0bca3ca88f8c8ea5d4f3/hf_xet-1.5.0-cp37-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:3531b1823a0e6d77d80f9ed15ca0e00f0d115094f8ac033d5cae88f4564cc949", size = 4465986, upload-time = "2026-05-06T06:17:44.886Z" },
- { url = "https://files.pythonhosted.org/packages/c4/a2/546f47f464737b3edbab6f8ddb57f2599b93d2cbb66f06abb475ccb48651/hf_xet-1.5.0-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:9a0ee58cd18d5ea799f7ed11290bbccbe56bdd8b1d97ca74b9cc49a3945d7a3b", size = 4259865, upload-time = "2026-05-06T06:17:42.639Z" },
- { url = "https://files.pythonhosted.org/packages/95/7f/1be593c1f28613be2e196473481cd81bfc5910795e30a34e8f744f6cac4f/hf_xet-1.5.0-cp37-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:1e60df5a42e9bed8628b6416af2cba4cba57ae9f02de226a06b020d98e1aab18", size = 4459835, upload-time = "2026-05-06T06:18:08.026Z" },
- { url = "https://files.pythonhosted.org/packages/aa/b2/703569fc881f3284487e68cda7b42179978480da3c438042a6bbbb4a671c/hf_xet-1.5.0-cp37-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:4b35549ce62601b84da4ff9b24d970032ace3d4430f52d91bcbb26c901d6c690", size = 4672414, upload-time = "2026-05-06T06:18:09.864Z" },
- { url = "https://files.pythonhosted.org/packages/af/37/1b6def445c567286b50aa3b33828158e135b1be44938dde59f11382a500c/hf_xet-1.5.0-cp37-abi3-win_amd64.whl", hash = "sha256:2806c7c17b4d23f8d88f7c4814f838c3b6150773fe339c20af23e1cfaf2797e4", size = 3977238, upload-time = "2026-05-06T06:18:23.621Z" },
- { url = "https://files.pythonhosted.org/packages/62/94/3b66b148778ee100dcfd69c2ca22b57b41b44d3063ceec934f209e9184ce/hf_xet-1.5.0-cp37-abi3-win_arm64.whl", hash = "sha256:b6c9df403040248c76d808d3e047d64db2d923bae593eb244c41e425cf6cd7be", size = 3806916, upload-time = "2026-05-06T06:18:21.7Z" },
+sdist = { url = "https://files.pythonhosted.org/packages/1b/ab/522a2ab67f27971a9d48ca666d4fca85ef7d5282d142e31fd087e27b1bbe/hf_xet-1.6.0.tar.gz", hash = "sha256:2e58454a340b3556dfa4972d5451aff4fba8dd42a236600ba1a1d2b1514f0fef", size = 920527, upload-time = "2026-08-03T22:33:13.243Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/41/62/3c062f593bd92ef4e77a0ef39541e3d82a0a1d3947c8a777a02a13a27828/hf_xet-1.6.0-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:70cbb9c896901600128cb9b6f06e132954fbede1db30f31f7c6c63f84cb7c31d", size = 4074584, upload-time = "2026-08-03T22:32:47.364Z" },
+ { url = "https://files.pythonhosted.org/packages/bb/1e/c0ad437dd267a8e435bef594acf781bbc3874ff0b6435b4962d03ecf7cc4/hf_xet-1.6.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:23379c2f9ec8696d952b16414a2bae72cad86a52df869b050698ba60f538c675", size = 3867381, upload-time = "2026-08-03T22:32:49.049Z" },
+ { url = "https://files.pythonhosted.org/packages/d5/ee/7c0d7b6ab336167531b1c30af2af003f054af4c749becbd7209ae33a77c3/hf_xet-1.6.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:f2f7278c05c22fd60cb436cda1269649b3e81db65ecdc8496e5e164aa4143e7b", size = 4453982, upload-time = "2026-08-03T22:32:50.568Z" },
+ { url = "https://files.pythonhosted.org/packages/63/06/ad8eab1c9525246650cbaa821caa3cdbaca734ab1a5b8c91bea09cbd8d69/hf_xet-1.6.0-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:948f15d3a9545cfe5932f6bd8b440f6ae630aee108f14b7bd6c561f7c2dcc522", size = 4249445, upload-time = "2026-08-03T22:32:52.391Z" },
+ { url = "https://files.pythonhosted.org/packages/d8/26/1eee8aedb0dafc1ab9717dc9ac602cde33361b232dc06803f1f6ed18b58c/hf_xet-1.6.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:5153e6bb103ad49d6ea9f1b2e230db5a2ea32551ad09a706d2f61d7c7c80d80e", size = 4451099, upload-time = "2026-08-03T22:32:54.114Z" },
+ { url = "https://files.pythonhosted.org/packages/67/57/0b88af1f194ab6c9c650547d9cc06bfeaab836ae4dcdb331676bfb8be95a/hf_xet-1.6.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:35cec30d75c6f9eb9c16a77cef68e85a103b72e24d4b473714ec9ff06428bab9", size = 4664712, upload-time = "2026-08-03T22:32:55.547Z" },
+ { url = "https://files.pythonhosted.org/packages/53/a0/26b717a9d1840e8abf48dcec64b5ed8fbe472671d38ad28d30e147132b33/hf_xet-1.6.0-cp314-cp314t-win_amd64.whl", hash = "sha256:5789835d7c6bc9436962853192082374297fb72d7eff7e7762ec25ceb7e25338", size = 4025906, upload-time = "2026-08-03T22:32:57.391Z" },
+ { url = "https://files.pythonhosted.org/packages/49/f6/4a9966633c6fef83af997e2cff68ec1963676d412bdfd096df2a93b8e185/hf_xet-1.6.0-cp314-cp314t-win_arm64.whl", hash = "sha256:75765820ce4700db3750c94acc8fe27c5fae4c9ec000a0dbac3ca082acf97765", size = 3849221, upload-time = "2026-08-03T22:32:59.123Z" },
+ { url = "https://files.pythonhosted.org/packages/a2/50/7afa2c9c787405864fc47a0d1bbc02c62e9101947ed43c1f43899fc7d91d/hf_xet-1.6.0-cp38-abi3-macosx_10_12_x86_64.whl", hash = "sha256:633dc0cd71d32da58ab8c03ad38e2fac452c15c2b0a2866ebf6ededfe0a5061d", size = 4071729, upload-time = "2026-08-03T22:33:00.721Z" },
+ { url = "https://files.pythonhosted.org/packages/4b/69/55b8dcf636142ae660fec1869fcac14c4da2e8412e14d6eee1523be77e9f/hf_xet-1.6.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:f0906082d9932ae0c0057fa194041c22b4e2cdb46b2592ef3b91f020d62a081a", size = 3876287, upload-time = "2026-08-03T22:33:02.251Z" },
+ { url = "https://files.pythonhosted.org/packages/67/4e/a28359bf1c1ecf11eba22123168c138698f7cb576ac678f5a2e16cd5da08/hf_xet-1.6.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:d62671bb130879cef0ee4c9ebe47a14af6c66ec53e6d84dc15936e5ffdfac82f", size = 4464663, upload-time = "2026-08-03T22:33:03.802Z" },
+ { url = "https://files.pythonhosted.org/packages/9a/69/1f0cbc2fb22ae6082d094f743d1b8945a3f36f6089cb95f42b7ee348cda7/hf_xet-1.6.0-cp38-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:0e6e21fa3cdfcdcd76748564bf593870a5e013f47d97cf10aed63aa222cff5b7", size = 4262538, upload-time = "2026-08-03T22:33:05.287Z" },
+ { url = "https://files.pythonhosted.org/packages/d1/3a/4f4f2301ade26e404462d3336fa11f7958d914cabbabdd6e03c3c5d5658c/hf_xet-1.6.0-cp38-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:4fc74352a17015bd0ee90038bc9efe38db894cde45f268b6712b04fce8cd0acb", size = 4460520, upload-time = "2026-08-03T22:33:06.81Z" },
+ { url = "https://files.pythonhosted.org/packages/ab/5f/311725e2a905534dfee2dcb5b08414f249147f1f12252bfc2bd24caa075c/hf_xet-1.6.0-cp38-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:8fb4f71cba6129110c3374a33f919001ff130488fc23553698e34cc1c2a1198c", size = 4675937, upload-time = "2026-08-03T22:33:08.616Z" },
+ { url = "https://files.pythonhosted.org/packages/98/b7/8c59a66d15205024662f1d66968136f13893f96df1ddc5087e2e281fc95f/hf_xet-1.6.0-cp38-abi3-win_amd64.whl", hash = "sha256:fb4fadde1b2b70bf4c0c14a6dccbe7194b1c28947fefd5bbe3fed9d940676c3b", size = 4033128, upload-time = "2026-08-03T22:33:10.171Z" },
+ { url = "https://files.pythonhosted.org/packages/73/63/ca511b6f802f28cf3489b280fe77475bcca8de85e81a6299d7916b5b5555/hf_xet-1.6.0-cp38-abi3-win_arm64.whl", hash = "sha256:3dc3e35441ba395006af5aaacc40ef2e603c51ef46c3530b9156185f00935ea3", size = 3859359, upload-time = "2026-08-03T22:33:11.725Z" },
]
[[package]]
@@ -3381,22 +3370,23 @@ wheels = [
[[package]]
name = "huggingface-hub"
-version = "1.14.0"
+version = "1.32.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
+ { name = "click" },
{ name = "filelock" },
{ name = "fsspec" },
{ name = "hf-xet", marker = "platform_machine == 'AMD64' or platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'arm64' or platform_machine == 'x86_64'" },
{ name = "httpx" },
{ name = "packaging" },
{ name = "pyyaml" },
+ { name = "tomli", marker = "python_full_version < '3.11'" },
{ name = "tqdm" },
- { name = "typer" },
{ name = "typing-extensions" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/39/40/43109e943fd718b0ccd0cd61eb4f1c347df22bf81f5874c6f22adf44bcff/huggingface_hub-1.14.0.tar.gz", hash = "sha256:d6d2c9cd6be1d02ae9ec6672d5587d10a427f377db688e82528f426a041622c2", size = 782365, upload-time = "2026-05-06T14:14:34.278Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/fe/0f/e83fdd856da8fca26bf78d71709ebd120432a0ce535e72b9597cab1eb5bf/huggingface_hub-1.32.0.tar.gz", hash = "sha256:ed70a45498abe86039df7c2f4e5f7575de524be908d3840e8f828d5525eafd6a", size = 1038662, upload-time = "2026-09-17T10:27:48.049Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/89/a5/33b49ba7bea7c41bb37f74ec0f8beea0831e052330196633fe2c77516ea6/huggingface_hub-1.14.0-py3-none-any.whl", hash = "sha256:efe075535c62e130b30e836b138e13785f6f043d1f0539e0a39aa411a99e90b8", size = 661479, upload-time = "2026-05-06T14:14:32.029Z" },
+ { url = "https://files.pythonhosted.org/packages/1b/cf/d98dd561d6d0d7b7d7a64d1563f8aaaa7c235daee41c1c9bcc3da62420ed/huggingface_hub-1.32.0-py3-none-any.whl", hash = "sha256:b0c7c80561969d9cdacdd55fce67ba9584cca0b9d4ea80957a3a5c1445fac5c8", size = 842906, upload-time = "2026-09-17T10:27:46.102Z" },
]
[[package]]
@@ -4518,6 +4508,7 @@ dependencies = [
{ name = "fastuuid" },
{ name = "filelock" },
{ name = "httpx", extra = ["http2"] },
+ { name = "huggingface-hub" },
{ name = "importlib-metadata" },
{ name = "jinja2" },
{ name = "jsonschema" },
@@ -4526,6 +4517,7 @@ dependencies = [
{ name = "pydantic" },
{ name = "pydantic-settings" },
{ name = "python-dotenv" },
+ { name = "pyyaml" },
{ name = "tiktoken" },
{ name = "tokenizers" },
]
@@ -4650,6 +4642,12 @@ utils = [
]
[package.dev-dependencies]
+benchmarks = [
+ { name = "a2a-sdk" },
+ { name = "mcp" },
+ { name = "pytest" },
+ { name = "pytest-codspeed" },
+]
ci = [
{ name = "aiodynamo" },
{ name = "anthropic" },
@@ -4689,6 +4687,8 @@ dev = [
{ name = "keyring" },
{ name = "langfuse" },
{ name = "mypy" },
+ { name = "numpy", version = "1.26.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" },
+ { name = "numpy", version = "2.4.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" },
{ name = "openapi-core" },
{ name = "opentelemetry-api" },
{ name = "opentelemetry-exporter-otlp" },
@@ -4787,6 +4787,7 @@ requires-dist = [
{ name = "httpx", extras = ["http2"], specifier = ">=0.28.0,<1.0" },
{ name = "httpx2", marker = "extra == 'mcp'", specifier = ">=2.5.0,<3" },
{ name = "httpx2", marker = "extra == 'proxy'", specifier = ">=2.5.0,<3" },
+ { name = "huggingface-hub", specifier = ">=0.34.0,<2.0" },
{ name = "importlib-metadata", specifier = ">=8.0.0,<9.0" },
{ name = "inquirerpy", marker = "extra == 'cli'", specifier = ">=0.3.4,<1.0" },
{ name = "inquirerpy", marker = "extra == 'proxy'", specifier = ">=0.3.4,<1.0" },
@@ -4805,12 +4806,12 @@ requires-dist = [
{ name = "numpydoc", marker = "extra == 'utils'", specifier = ">=1.8.0,<2.0" },
{ name = "nvidia-riva-client", marker = "extra == 'stt-nvidia-riva'", specifier = ">=2.15.0" },
{ name = "openai", specifier = ">=2.20.0,<3.0.0" },
- { name = "packaging", specifier = ">=24.0" },
{ name = "opentelemetry-api", marker = "extra == 'proxy-runtime'", specifier = "==1.28.0" },
{ name = "opentelemetry-exporter-otlp", marker = "extra == 'proxy-runtime'", specifier = "==1.28.0" },
{ name = "opentelemetry-instrumentation-fastapi", marker = "extra == 'proxy-runtime'", specifier = "==0.49b0" },
{ name = "opentelemetry-sdk", marker = "extra == 'proxy-runtime'", specifier = "==1.28.0" },
{ name = "orjson", marker = "extra == 'proxy'", specifier = ">=3.11.6,<4.0" },
+ { name = "packaging", specifier = ">=24.0" },
{ name = "polars", marker = "extra == 'proxy'", specifier = ">=1.38.1,<2.0" },
{ name = "prisma", marker = "extra == 'extra-proxy'", specifier = ">=0.11.0,<1.0" },
{ name = "prometheus-client", marker = "extra == 'proxy-runtime'", specifier = ">=0.20.0,<1.0" },
@@ -4828,6 +4829,7 @@ requires-dist = [
{ name = "python-dotenv", specifier = ">=1.0.0,<2.0" },
{ name = "python-multipart", marker = "extra == 'proxy'", specifier = ">=0.0.27,<1.0" },
{ name = "python3-saml", marker = "extra == 'saml'", specifier = ">=1.16.0,<2.0" },
+ { name = "pyyaml", specifier = ">=6.0.3,<7.0" },
{ name = "pyyaml", marker = "extra == 'cli'", specifier = ">=6.0.3,<7.0" },
{ name = "pyyaml", marker = "extra == 'proxy'", specifier = ">=6.0.3,<7.0" },
{ name = "redisvl", marker = "extra == 'extra-proxy'", specifier = ">=0.4.1,<1.0" },
@@ -4854,6 +4856,12 @@ requires-dist = [
provides-extras = ["proxy", "cli", "extra-proxy", "utils", "caching", "mcp", "saml", "semantic-router", "mlflow", "grpc", "stt-vertex-chirp", "stt-nvidia-riva", "google", "bedrock-realtime", "proxy-runtime"]
[package.metadata.requires-dev]
+benchmarks = [
+ { name = "a2a-sdk", specifier = "==1.1.0" },
+ { name = "mcp", specifier = ">=2.2.0,<3" },
+ { name = "pytest", specifier = "==9.0.3" },
+ { name = "pytest-codspeed", specifier = "==4.3.0" },
+]
ci = [
{ name = "aiodynamo", specifier = "==24.7" },
{ name = "anthropic", specifier = "==0.84.0" },
@@ -4893,6 +4901,7 @@ dev = [
{ name = "keyring", specifier = "==25.7.0" },
{ name = "langfuse", specifier = "==2.59.7" },
{ name = "mypy", specifier = "==1.20.1" },
+ { name = "numpy", specifier = ">=1.26.0,<3.0" },
{ name = "openapi-core", specifier = "==0.22.0" },
{ name = "opentelemetry-api", specifier = "==1.28.0" },
{ name = "opentelemetry-exporter-otlp", specifier = "==1.28.0" },
@@ -9171,15 +9180,6 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/5d/40/e1e72872c6354b306daef1703549e8e83b4d43cfea356311bf722a043752/setuptools-83.0.0-py3-none-any.whl", hash = "sha256:29b23c360f22f414dc7336bb39178cc7bcbf6021ed2733cde173f09dba19abb3", size = 1008090, upload-time = "2026-07-04T15:31:20.885Z" },
]
-[[package]]
-name = "shellingham"
-version = "1.5.4"
-source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/58/15/8b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e/shellingham-1.5.4.tar.gz", hash = "sha256:8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de", size = 10310, upload-time = "2023-10-24T04:13:40.426Z" }
-wheels = [
- { url = "https://files.pythonhosted.org/packages/e0/f9/0595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822/shellingham-1.5.4-py2.py3-none-any.whl", hash = "sha256:7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686", size = 9755, upload-time = "2023-10-24T04:13:38.866Z" },
-]
-
[[package]]
name = "simple-websocket"
version = "1.1.0"
@@ -9920,21 +9920,6 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/19/97/56608b2249fe206a67cd573bc93cd9896e1efb9e98bce9c163bcdc704b88/truststore-0.10.4-py3-none-any.whl", hash = "sha256:adaeaecf1cbb5f4de3b1959b42d41f6fab57b2b1666adb59e89cb0b53361d981", size = 18660, upload-time = "2025-08-12T18:49:01.46Z" },
]
-[[package]]
-name = "typer"
-version = "0.25.1"
-source = { registry = "https://pypi.org/simple" }
-dependencies = [
- { name = "annotated-doc" },
- { name = "click" },
- { name = "rich" },
- { name = "shellingham" },
-]
-sdist = { url = "https://files.pythonhosted.org/packages/e4/51/9aed62104cea109b820bbd6c14245af756112017d309da813ef107d42e7e/typer-0.25.1.tar.gz", hash = "sha256:9616eb8853a09ffeabab1698952f33c6f29ffdbceb4eaeecf571880e8d7664cc", size = 122276, upload-time = "2026-04-30T19:32:16.964Z" }
-wheels = [
- { url = "https://files.pythonhosted.org/packages/3f/f9/2b3ff4e56e5fa7debfaf9eb135d0da96f3e9a1d5b27222223c7296336e5f/typer-0.25.1-py3-none-any.whl", hash = "sha256:75caa44ed46a03fb2dab8808753ffacdbfea88495e74c85a28c5eefcf5f39c89", size = 58409, upload-time = "2026-04-30T19:32:18.271Z" },
-]
-
[[package]]
name = "types-awscrt"
version = "0.34.1"