From 7c4fae477aa5feef0a6da3bb9b9655fef5dea3e7 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 3 May 2025 21:31:05 -0700 Subject: [PATCH] [Bug Fix] UnicodeDecodeError: 'charmap' on Windows during litellm import (#10542) * test using litellm on windows * fix using litellm on windows * test using litellm on windows * fix change on ci/cd * fix test on ci/cd --- .circleci/config.yml | 34 +++++++++++++++++- litellm/utils.py | 2 +- .../windows_tests/test_litellm_on_windows.py | 35 +++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/windows_tests/test_litellm_on_windows.py diff --git a/.circleci/config.yml b/.circleci/config.yml index 95984e4e68..9fb1aeaf4f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,6 +2,7 @@ version: 2.1 orbs: codecov: codecov/codecov@4.0.1 node: circleci/node@5.1.0 # Add this line to declare the node orb + win: circleci/windows@5.0 # Add Windows orb commands: setup_google_dns: @@ -17,6 +18,31 @@ commands: echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf jobs: + # Add Windows testing job + using_litellm_on_windows: + executor: + name: win/default + shell: powershell.exe + working_directory: ~/project + steps: + - checkout + - run: + name: Install Python + command: | + choco install python --version=3.11.0 -y + refreshenv + python --version + - run: + name: Install Dependencies + command: | + python -m pip install --upgrade pip + pip install pytest + pip install . + - run: + name: Run Windows-specific test + command: | + python -m pytest tests/windows_tests/test_litellm_on_windows.py -v + local_testing: docker: - image: cimg/python:3.11 @@ -2680,6 +2706,12 @@ workflows: version: 2 build_and_test: jobs: + - using_litellm_on_windows: + filters: + branches: + only: + - main + - /litellm_.*/ - local_testing: filters: branches: @@ -2937,4 +2969,4 @@ workflows: - proxy_pass_through_endpoint_tests - check_code_and_doc_quality - publish_proxy_extras - + diff --git a/litellm/utils.py b/litellm/utils.py index 90f7b14151..2403ded78c 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -183,7 +183,7 @@ try: # Python 3.9+ with resources.files("litellm.litellm_core_utils.tokenizers").joinpath( "anthropic_tokenizer.json" - ).open("r") as f: + ).open("r", encoding="utf-8") as f: json_data = json.load(f) except (ImportError, AttributeError, TypeError): with resources.open_text( diff --git a/tests/windows_tests/test_litellm_on_windows.py b/tests/windows_tests/test_litellm_on_windows.py new file mode 100644 index 0000000000..6f1e2ad1cd --- /dev/null +++ b/tests/windows_tests/test_litellm_on_windows.py @@ -0,0 +1,35 @@ + +import asyncio +import os +import subprocess +import sys +import time +import traceback +import platform + +import pytest + +sys.path.insert( + 0, os.path.abspath("../..") +) # Adds the parent directory to the system path + +def test_using_litellm_on_windows(): + """Test that LiteLLM can be imported on Windows systems.""" + + try: + import litellm + print(f"litellm imported successfully on Windows ({platform.system()} {platform.release()})") + + response = litellm.completion( + model="gpt-4o", + messages=[ + {"role": "user", "content": "This should never fail. Email ishaan@berri.ai if this test ever fails."} + ], + mock_response="Hello, how are you?" + ) + print(response) + except Exception as e: + pytest.fail( + f"Error occurred on Windows: {e}. Installing litellm on Windows failed." + ) +