litellm/tests/proxy_unit_tests/test_proxy_encrypt_decrypt.py
jaideeppyne ab78459097 fix(cohere): handle multimodal inputs in token counting and guardrails
Response transformation crashed with TypeError when counting tokens for
multimodal (dict-shaped) embedding inputs, since encoding.encode() was
called directly on the dict instead of its text parts. Also removed a
dead, equally-buggy duplicate of the same loop in _transform_response.

Separately, the embeddings guardrail handler silently skipped multimodal
dict inputs instead of scanning them, letting prohibited text/image
content bypass the configured guardrail. It now extracts and scans the
text parts while leaving image parts untouched.
2026-08-13 09:01:47 +05:30

50 lines
1.7 KiB
Python

import os
import sys
import pytest
from dotenv import load_dotenv
load_dotenv()
import io
import os
sys.path.insert(
0, os.path.abspath("../..")
) # Adds-the parent directory to the system path
from litellm.proxy import proxy_server
from litellm.proxy.common_utils.encrypt_decrypt_utils import (
decrypt_value_helper,
encrypt_value_helper,
)
def test_encrypt_decrypt_with_master_key():
setattr(proxy_server, "master_key", "sk-1234")
assert decrypt_value_helper(encrypt_value_helper("test"), key="test_key") == "test"
assert decrypt_value_helper(encrypt_value_helper(10), key="test_key") == 10
assert decrypt_value_helper(encrypt_value_helper(True), key="test_key") is True
assert decrypt_value_helper(encrypt_value_helper(None), key="test_key") is None
assert decrypt_value_helper(encrypt_value_helper({"rpm": 10}), key="test_key") == {
"rpm": 10
}
# encryption should actually occur for strings
assert encrypt_value_helper("test") != "test"
def test_encrypt_decrypt_with_salt_key():
os.environ["LITELLM_SALT_KEY"] = "sk-salt-key2222"
print(f"LITELLM_SALT_KEY: {os.environ['LITELLM_SALT_KEY']}")
assert decrypt_value_helper(encrypt_value_helper("test"), key="test_key") == "test"
assert decrypt_value_helper(encrypt_value_helper(10), key="test_key") == 10
assert decrypt_value_helper(encrypt_value_helper(True), key="test_key") is True
assert decrypt_value_helper(encrypt_value_helper(None), key="test_key") is None
assert decrypt_value_helper(encrypt_value_helper({"rpm": 10}), key="test_key") == {
"rpm": 10
}
# encryption should actually occur for strings
assert encrypt_value_helper("test") != "test"
os.environ.pop("LITELLM_SALT_KEY", None)