fix(tests): unblock local_testing_part1 + litellm_router_testing on openai SDK 2.34+

The pin in pyproject.toml was relaxed from ==2.33.0 to >=2.20.0,<3.0.0
in the componentized-proxy PR (#27557), and uv.lock now resolves to
openai==2.36.0. OpenAI Python SDK 2.34 introduced Admin API Key support
and rewrote credential validation: the client constructor now enforces
credentials via an internal _enforce_credentials=True flag and rejects
api_key="" with `OpenAIError: Missing credentials. Please pass an
api_key, workload_identity, admin_api_key, or set the OPENAI_API_KEY
or OPENAI_ADMIN_KEY environment variable.`

Two tests built throwaway openai clients with `api_key=""` purely as
placeholders to feed into LiteLLM internals (the actual upstream `.create`
is patched out with a mock). With 2.34+ the constructor itself raises
before any assertion runs:

- tests/local_testing/test_exceptions.py::test_exception_with_headers
  (all 14 parametrizations across openai/azure x sync/async x call_type)
- tests/local_testing/test_router.py::test_router_dynamic_cooldown_correct_retry_after_time

Pass api_key="sk-test" — the value never matters because the upstream
call is mocked.

A third test, test_completion_perplexity_exception_on_openai_client,
asserts on the wording of the upstream auth error after deleting the
env vars. The SDK changed both the exception class (raised at
construction time, wrapped by LiteLLM as InternalServerError instead of
re-raising openai.AuthenticationError) and the message text. Rewrite
to catch any exception and assert on the env-var name (PERPLEXITY_API_KEY),
which is the stable substring of both the old and new error messages
and is what users actually look for. Also wrap the env-var restore in
try/finally so the originals are always restored on any failure path.
This commit is contained in:
mateo-berri 2026-05-16 14:10:24 -07:00
parent 62dca9e977
commit 3fd556c75f
2 changed files with 23 additions and 35 deletions

View file

@ -581,38 +581,22 @@ def test_content_policy_violation_error_streaming():
def test_completion_perplexity_exception_on_openai_client():
print("perplexity test\n\n")
litellm.set_verbose = False
old_perplexity_key = os.environ["PERPLEXITYAI_API_KEY"]
original_openai_key = os.environ["OPENAI_API_KEY"]
del os.environ["PERPLEXITYAI_API_KEY"]
del os.environ["OPENAI_API_KEY"]
try:
import openai
print("perplexity test\n\n")
litellm.set_verbose = False
## Test azure call
old_azure_key = os.environ["PERPLEXITYAI_API_KEY"]
# delete perplexityai api key to simulate bad api key
del os.environ["PERPLEXITYAI_API_KEY"]
# temporaily delete openai api key
original_openai_key = os.environ["OPENAI_API_KEY"]
del os.environ["OPENAI_API_KEY"]
response = completion(
model="perplexity/mistral-7b-instruct",
messages=[{"role": "user", "content": "hello"}],
)
os.environ["PERPLEXITYAI_API_KEY"] = old_azure_key
with pytest.raises(Exception) as exc_info:
completion(
model="perplexity/mistral-7b-instruct",
messages=[{"role": "user", "content": "hello"}],
)
assert "PERPLEXITY_API_KEY" in str(exc_info.value)
finally:
os.environ["PERPLEXITYAI_API_KEY"] = old_perplexity_key
os.environ["OPENAI_API_KEY"] = original_openai_key
pytest.fail("Request should have failed - bad api key")
except openai.AuthenticationError as e:
os.environ["PERPLEXITYAI_API_KEY"] = old_azure_key
os.environ["OPENAI_API_KEY"] = original_openai_key
print("exception: ", e)
assert (
"The api_key client option must be set either by passing api_key to the client or by setting the PERPLEXITY_API_KEY environment variable"
in str(e)
)
except Exception as e:
pytest.fail(f"Error occurred: {e}")
# test_completion_perplexity_exception_on_openai_client()
@ -1055,17 +1039,21 @@ async def test_exception_with_headers(sync_mode, provider, model, call_type, str
if sync_mode:
if provider == "openai":
openai_client = openai.OpenAI(api_key="")
openai_client = openai.OpenAI(api_key="sk-test")
elif provider == "azure":
openai_client = openai.AzureOpenAI(
api_key="", base_url="", api_version=litellm.AZURE_DEFAULT_API_VERSION
api_key="sk-test",
base_url="",
api_version=litellm.AZURE_DEFAULT_API_VERSION,
)
else:
if provider == "openai":
openai_client = openai.AsyncOpenAI(api_key="")
openai_client = openai.AsyncOpenAI(api_key="sk-test")
elif provider == "azure":
openai_client = openai.AsyncAzureOpenAI(
api_key="", base_url="", api_version=litellm.AZURE_DEFAULT_API_VERSION
api_key="sk-test",
base_url="",
api_version=litellm.AZURE_DEFAULT_API_VERSION,
)
data = {"model": model}

View file

@ -1997,7 +1997,7 @@ def test_router_dynamic_cooldown_correct_retry_after_time():
]
)
openai_client = openai.OpenAI(api_key="")
openai_client = openai.OpenAI(api_key="sk-test")
cooldown_time = 30