Skip to content

test(client): clear the endpoint variables around the lifecycle tests - #243

Draft
jeffdupont wants to merge 3 commits into
mainfrom
jdupont/test-lifecycle-env-leak
Draft

jeffdupont wants to merge 3 commits into
mainfrom
jdupont/test-lifecycle-env-leak

Conversation

@jeffdupont

@jeffdupont jeffdupont commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Two tests in packages/client/tests/test_lifecycle.py fail locally when the whole suite runs, and pass on their own and in CI:

  • TestInitClientSDKKeyPath::test_uses_options_sdk_key_over_env_var
  • TestInitClientSDKKeyPath::test_retries_after_a_failed_init_instead_of_replaying_it

Both assert the exact Config(...) call, and got staging base_uri / stream_uri / events_uri keyword arguments they did not expect.

Cause

litellm/__init__.py calls load_dotenv() on import when LITELLM_MODE is DEV, its default. pytest imports the litellm package tests during collection, before any test runs. find_dotenv searches upward from the calling file, which is litellm's own install directory, so a .venv inside a checkout finds that checkout's .env. A .env that sets LD_BASE_URI, LD_STREAM_URI or LD_EVENTS_URI therefore puts them in os.environ for the rest of the run.

Neither failing test clears those variables. test_retries_after_a_failed_init_instead_of_replaying_it copies os.environ minus only LD_SDK_KEY, and test_uses_options_sdk_key_over_env_var patches os.environ without clearing it. init_client passes all three variables to Config.

CI has no .env, so it never sees this.

Checked by importing each provider SDK in a clean process: only litellm sets LD_BASE_URI.

Fix

An autouse clear_endpoint_env fixture in test_lifecycle.py deletes the three variables around each test with monkeypatch, the same way clear_endpoint_env in test_skills_fdv2.py already does for its two.

Verification

  • Full suite with a parent .env setting the three variables: 2 failed before, 3714 passed, 18 skipped after (on the current main).
  • make lint format-check: clean.

Not in this PR

litellm loading a .env on import also affects customers of launchdarkly-ai-litellm-*, not only tests: a project whose virtualenv sits inside it loads that project's .env, whatever the working directory. The SDK itself never loads one (test_never_loads_a_dotenv_file), but the dependency does. That is a separate question.

🤖 Generated with Claude Code

jeffdupont and others added 3 commits October 28, 2026 15:18
Two TestInitClientSDKKeyPath tests assert the exact Config call, and
init_client passes LD_BASE_URI, LD_STREAM_URI and LD_EVENTS_URI to it.
Importing litellm during collection runs load_dotenv(), which walks up
from the working directory, so a checkout with a .env setting those
variables failed both tests locally while CI passed. An autouse fixture
now clears all three, matching clear_endpoint_env in test_skills_fdv2.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
load_dotenv() searches upward from the calling file, litellm's own
install directory, not from the working directory. That is why a .venv
inside a checkout finds that checkout's .env.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant