From b96d2ea422aaa744314809a83e75b1f51dd25076 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 22 Apr 2025 18:29:56 -0700 Subject: [PATCH] Bug Fix - Address deprecation of open_text (#10208) * Update utils.py (#10201) * fixes importlib --------- Co-authored-by: Nathan Brake <33383515+njbrake@users.noreply.github.com> --- litellm/utils.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index 0150c4f43f..98a9c34b47 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -180,10 +180,18 @@ from litellm.types.utils import ( all_litellm_params, ) -with resources.open_text( - "litellm.litellm_core_utils.tokenizers", "anthropic_tokenizer.json" -) as f: - json_data = json.load(f) +try: + # Python 3.9+ + with resources.files("litellm.litellm_core_utils.tokenizers").joinpath( + "anthropic_tokenizer.json" + ).open("r") as f: + json_data = json.load(f) +except (ImportError, AttributeError, TypeError): + with resources.open_text( + "litellm.litellm_core_utils.tokenizers", "anthropic_tokenizer.json" + ) as f: + json_data = json.load(f) + # Convert to str (if necessary) claude_json_str = json.dumps(json_data) import importlib.metadata