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]

chore(deps): address dependency review notes

usere96d850b8423
diff --git a/litellm/utils.py b/litellm/utils.py
index ec198616f05..0c5b694bd77 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -2243,12 +2243,30 @@ def encode(model="", text="", custom_tokenizer: Optional[dict] = None):
return enc
-def decode(model="", tokens: List[int] = [], custom_tokenizer: Optional[dict] = None):
+def decode(
+ model="",
+ tokens: List[int] = [],
+ custom_tokenizer: Optional[dict] = None,
+ skip_special_tokens: bool = True,
+):
+ """
+ Decodes token ids using the selected tokenizer.
+
+ Args:
+ skip_special_tokens: For HuggingFace tokenizers, keep the historical
+ LiteLLM round-trip behavior by omitting special tokens by default.
+ Set to False to inspect decoded BOS/EOS tokens.
+ """
tokenizer_json = custom_tokenizer or _select_tokenizer(model=model)
if tokenizer_json["type"] == "huggingface_tokenizer":
- tokens = _strip_huggingface_special_token_ids(
- tokenizer_json["tokenizer"], tokens
+ 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
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
new file mode 100644
index 00000000000..f0f2b8e7f5d
--- /dev/null
+++ b/tests/test_litellm/litellm_core_utils/test_decode_special_tokens.py
@@ -0,0 +1,17 @@
+from litellm import decode, encode
+
+
+def test_decode_can_preserve_huggingface_special_tokens():
+ sample_text = "Hello World, this is my input string!"
+ tokens = encode(model="meta-llama/Llama-2-7b-chat", text=sample_text)
+
+ decoded_text = decode(model="meta-llama/Llama-2-7b-chat", tokens=tokens)
+ decoded_text_with_special_tokens = decode(
+ model="meta-llama/Llama-2-7b-chat",
+ tokens=tokens,
+ skip_special_tokens=False,
+ )
+
+ assert decoded_text == sample_text
+ assert sample_text in decoded_text_with_special_tokens
+ assert decoded_text_with_special_tokens != sample_text
diff --git a/ui/litellm-dashboard/tests/setupTests.ts b/ui/litellm-dashboard/tests/setupTests.ts
index 62eb16c66b6..6bc2e7775a1 100644
--- a/ui/litellm-dashboard/tests/setupTests.ts
+++ b/ui/litellm-dashboard/tests/setupTests.ts
@@ -12,14 +12,23 @@ const ensureTestLocalStorage = () => {
return;
}
- const store = new Map<string, string>();
+ const storageStores = new WeakMap<Storage, Map<string, string>>();
const storagePrototype = window.Storage.prototype;
+ const getStore = (storage: Storage) => {
+ let store = storageStores.get(storage);
+ if (store === undefined) {
+ store = new Map<string, string>();
+ storageStores.set(storage, store);
+ }
+ return store;
+ };
Object.defineProperties(storagePrototype, {
getItem: {
configurable: true,
writable: true,
- value(key: string) {
+ value(this: Storage, key: string) {
+ const store = getStore(this);
const normalizedKey = String(key);
return store.has(normalizedKey) ? store.get(normalizedKey)! : null;
},
@@ -27,38 +36,43 @@ const ensureTestLocalStorage = () => {
setItem: {
configurable: true,
writable: true,
- value(key: string, value: string) {
+ value(this: Storage, key: string, value: string) {
+ const store = getStore(this);
store.set(String(key), String(value));
},
},
removeItem: {
configurable: true,
writable: true,
- value(key: string) {
+ value(this: Storage, key: string) {
+ const store = getStore(this);
store.delete(String(key));
},
},
clear: {
configurable: true,
writable: true,
- value() {
+ value(this: Storage) {
+ const store = getStore(this);
store.clear();
},
},
key: {
configurable: true,
writable: true,
- value(index: number) {
+ value(this: Storage, index: number) {
+ const store = getStore(this);
return Array.from(store.keys())[index] ?? null;
},
},
});
const localStorage = Object.create(storagePrototype);
+ storageStores.set(localStorage, new Map<string, string>());
Object.defineProperty(localStorage, "length", {
configurable: true,
get() {
- return store.size;
+ return getStore(localStorage).size;
},
});
@@ -102,7 +116,15 @@ vi.mock("@tremor/react", async (importOriginal) => {
return React.createElement(React.Fragment, null, children);
},
// Render as a plain checkbox so toggle interactions are testable without Tremor internals
- Switch: ({ checked, onChange, className }: { checked?: boolean; onChange?: (v: boolean) => void; className?: string }) =>
+ Switch: ({
+ checked,
+ onChange,
+ className,
+ }: {
+ checked?: boolean;
+ onChange?: (v: boolean) => void;
+ className?: string;
+ }) =>
React.createElement("input", {
type: "checkbox",
role: "switch",