From 899038a2ce09048b8e62a91aeb9ee227e1d26a4b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 16 May 2026 14:16:09 -0700 Subject: [PATCH] fix(tests): narrow perplexity exception catch to openai.OpenAIError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Greptile feedback on #28087: catching bare Exception in pytest.raises also accepts unrelated KeyError/AttributeError from setup bugs. Narrow to openai.OpenAIError — the root of the openai SDK exception tree. Every LiteLLM exception class (AuthenticationError, InternalServerError, APIError, APIConnectionError, ...) subclasses an openai.* exception, so this covers both the pre-2.34 path (openai.AuthenticationError raised at request time) and the 2.34+ path (litellm.InternalServerError wrapping the construction-time OpenAIError) without admitting unrelated exceptions. --- tests/local_testing/test_exceptions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/local_testing/test_exceptions.py b/tests/local_testing/test_exceptions.py index 8dd180437bd..5a375826e0a 100644 --- a/tests/local_testing/test_exceptions.py +++ b/tests/local_testing/test_exceptions.py @@ -581,6 +581,8 @@ def test_content_policy_violation_error_streaming(): def test_completion_perplexity_exception_on_openai_client(): + import openai + print("perplexity test\n\n") litellm.set_verbose = False old_perplexity_key = os.environ["PERPLEXITYAI_API_KEY"] @@ -588,7 +590,7 @@ def test_completion_perplexity_exception_on_openai_client(): del os.environ["PERPLEXITYAI_API_KEY"] del os.environ["OPENAI_API_KEY"] try: - with pytest.raises(Exception) as exc_info: + with pytest.raises(openai.OpenAIError) as exc_info: completion( model="perplexity/mistral-7b-instruct", messages=[{"role": "user", "content": "hello"}],