From dc968a7213f1871ed8752d5406ae82d79ee9d313 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 16 Jul 2024 20:42:43 -0700 Subject: [PATCH 1/2] anthropic - raise Authentication error when no api key provided --- litellm/llms/anthropic.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index 733cce1e011..af5ccf8289a 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -475,10 +475,12 @@ class AnthropicConfig: # makes headers for API call -def validate_environment(api_key, user_headers): +def validate_environment(api_key, user_headers, model): if api_key is None: - raise ValueError( - "Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params" + raise litellm.AuthenticationError( + message="Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params. Please set `ANTHROPIC_API_KEY` in your environment vars", + llm_provider="anthropic", + model=model, ) headers = { "accept": "application/json", @@ -734,7 +736,7 @@ class AnthropicChatCompletion(BaseLLM): logger_fn=None, headers={}, ): - headers = validate_environment(api_key, headers) + headers = validate_environment(api_key, headers, model) _is_function_call = False messages = copy.deepcopy(messages) optional_params = copy.deepcopy(optional_params) From dbca6bbb9938acf25e4138de7c74a03274a0d879 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 16 Jul 2024 20:44:40 -0700 Subject: [PATCH 2/2] test - raise correct Auth exception for anthropic API --- litellm/tests/test_exceptions.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/litellm/tests/test_exceptions.py b/litellm/tests/test_exceptions.py index 8d059960909..829038fe96b 100644 --- a/litellm/tests/test_exceptions.py +++ b/litellm/tests/test_exceptions.py @@ -388,6 +388,33 @@ def test_completion_openai_exception(): # test_completion_openai_exception() +def test_anthropic_openai_exception(): + # test if anthropic raises litellm.AuthenticationError + try: + litellm.set_verbose = True + ## Test azure call + old_azure_key = os.environ["ANTHROPIC_API_KEY"] + os.environ.pop("ANTHROPIC_API_KEY") + response = completion( + model="anthropic/claude-3-sonnet-20240229", + messages=[{"role": "user", "content": "hello"}], + ) + print(f"response: {response}") + print(response) + except litellm.AuthenticationError as e: + os.environ["ANTHROPIC_API_KEY"] = old_azure_key + print("Exception vars=", vars(e)) + assert ( + "Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params" + in e.message + ) + print( + "ANTHROPIC_API_KEY: good job got the correct error for ANTHROPIC_API_KEY when key not set" + ) + except Exception as e: + pytest.fail(f"Error occurred: {e}") + + def test_completion_mistral_exception(): # test if mistral/mistral-tiny raises openai.AuthenticationError try: