fixes importlib

This commit is contained in:
Ishaan Jaff 2025-04-22 14:18:34 -07:00
parent 08e5bdf542
commit 6328018c05

View file

@ -180,10 +180,18 @@ from litellm.types.utils import (
all_litellm_params, all_litellm_params,
) )
with resources.files("litellm.litellm_core_utils.tokenizers") \ try:
.joinpath("anthropic_tokenizer.json") \ # Python 3.9+
.open("r") as f: with resources.files("litellm.litellm_core_utils.tokenizers").joinpath(
"anthropic_tokenizer.json"
).open("r") as f:
json_data = json.load(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) # Convert to str (if necessary)
claude_json_str = json.dumps(json_data) claude_json_str = json.dumps(json_data)
import importlib.metadata import importlib.metadata
@ -516,9 +524,9 @@ def function_setup( # noqa: PLR0915
function_id: Optional[str] = kwargs["id"] if "id" in kwargs else None function_id: Optional[str] = kwargs["id"] if "id" in kwargs else None
## DYNAMIC CALLBACKS ## ## DYNAMIC CALLBACKS ##
dynamic_callbacks: Optional[ dynamic_callbacks: Optional[List[Union[str, Callable, CustomLogger]]] = (
List[Union[str, Callable, CustomLogger]] kwargs.pop("callbacks", None)
] = kwargs.pop("callbacks", None) )
all_callbacks = get_dynamic_callbacks(dynamic_callbacks=dynamic_callbacks) all_callbacks = get_dynamic_callbacks(dynamic_callbacks=dynamic_callbacks)
if len(all_callbacks) > 0: if len(all_callbacks) > 0:
@ -1202,9 +1210,9 @@ def client(original_function): # noqa: PLR0915
exception=e, exception=e,
retry_policy=kwargs.get("retry_policy"), retry_policy=kwargs.get("retry_policy"),
) )
kwargs[ kwargs["retry_policy"] = (
"retry_policy" reset_retry_policy()
] = reset_retry_policy() # prevent infinite loops ) # prevent infinite loops
litellm.num_retries = ( litellm.num_retries = (
None # set retries to None to prevent infinite loops None # set retries to None to prevent infinite loops
) )
@ -3028,16 +3036,16 @@ def get_optional_params( # noqa: PLR0915
True # so that main.py adds the function call to the prompt True # so that main.py adds the function call to the prompt
) )
if "tools" in non_default_params: if "tools" in non_default_params:
optional_params[ optional_params["functions_unsupported_model"] = (
"functions_unsupported_model" non_default_params.pop("tools")
] = non_default_params.pop("tools") )
non_default_params.pop( non_default_params.pop(
"tool_choice", None "tool_choice", None
) # causes ollama requests to hang ) # causes ollama requests to hang
elif "functions" in non_default_params: elif "functions" in non_default_params:
optional_params[ optional_params["functions_unsupported_model"] = (
"functions_unsupported_model" non_default_params.pop("functions")
] = non_default_params.pop("functions") )
elif ( elif (
litellm.add_function_to_prompt litellm.add_function_to_prompt
): # if user opts to add it to prompt instead ): # if user opts to add it to prompt instead
@ -3060,11 +3068,11 @@ def get_optional_params( # noqa: PLR0915
if "response_format" in non_default_params: if "response_format" in non_default_params:
if provider_config is not None: if provider_config is not None:
non_default_params[ non_default_params["response_format"] = (
"response_format" provider_config.get_json_schema_from_pydantic_object(
] = provider_config.get_json_schema_from_pydantic_object(
response_format=non_default_params["response_format"] response_format=non_default_params["response_format"]
) )
)
else: else:
non_default_params["response_format"] = type_to_response_format_param( non_default_params["response_format"] = type_to_response_format_param(
response_format=non_default_params["response_format"] response_format=non_default_params["response_format"]
@ -4079,9 +4087,9 @@ def _count_characters(text: str) -> int:
def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream]) -> str: def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream]) -> str:
_choices: Union[ _choices: Union[List[Union[Choices, StreamingChoices]], List[StreamingChoices]] = (
List[Union[Choices, StreamingChoices]], List[StreamingChoices] response_obj.choices
] = response_obj.choices )
response_str = "" response_str = ""
for choice in _choices: for choice in _choices: