Skip to content

fix(test): mock environment variables for callback validation test#21286

Merged
jquinter merged 1 commit intomainfrom
fix/proxy-config-callback-validation
Feb 16, 2026
Merged

fix(test): mock environment variables for callback validation test#21286
jquinter merged 1 commit intomainfrom
fix/proxy-config-callback-validation

Conversation

@jquinter
Copy link
Contributor

Summary

Fixes test_proxy_config_state_post_init_callback_call failure when running with pytest-xdist parallel execution (--dist=loadscope).

Problem

The test was failing with Pydantic validation error:

ValidationError: 2 validation errors for TeamCallbackMetadata
callback_vars.langfuse_public_key
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
callback_vars.langfuse_secret
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

Root cause: The test configures callback settings with environment variable references:

"langfuse_public_key": "os.environ/LANGFUSE_PUBLIC_KEY",
"langfuse_secret": "os.environ/LANGFUSE_SECRET_KEY",

These get resolved at runtime. In parallel execution:

  • Different worker processes may have different environment variables
  • Required env vars (LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY) may not be set
  • Resolution returns None instead of a string
  • Pydantic validation fails expecting string type

Solution

Use monkeypatch to set the required environment variables before test execution:

def test_proxy_config_state_post_init_callback_call(monkeypatch):
    monkeypatch.setenv("LANGFUSE_PUBLIC_KEY", "test_public_key")
    monkeypatch.setenv("LANGFUSE_SECRET_KEY", "test_secret_key")
    # ... rest of test

This ensures:

  • Consistent behavior across all execution environments (local, CI, parallel workers)
  • Environment variable resolution always produces valid strings
  • Pydantic validation passes
  • No pollution between tests (monkeypatch auto-cleans up)

Testing

  • Tested locally: pytest tests/proxy_unit_tests/test_proxy_utils.py::test_proxy_config_state_post_init_callback_call -v
  • Verified Pydantic validation passes with mocked env vars

Related

🤖 Generated with Claude Code

The test test_proxy_config_state_post_init_callback_call was failing with:
```
ValidationError: 2 validation errors for TeamCallbackMetadata
callback_vars.langfuse_public_key
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
```

Root cause: The test uses environment variable references like
"os.environ/LANGFUSE_PUBLIC_KEY" which get resolved at runtime. In
parallel execution with --dist=loadscope, these environment variables
may not be set in all worker processes, causing the resolution to
return None, which fails Pydantic validation expecting strings.

Solution: Use monkeypatch to set the required environment variables
before the test runs. This ensures consistent behavior across all
test execution environments (local, CI, parallel workers).

Fixes test failure exposed by PR #21277.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 15, 2026 11:46pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 15, 2026

Greptile Summary

This PR fixes a test isolation issue in test_proxy_config_state_post_init_callback_call that surfaces during parallel test execution (e.g., pytest-xdist). The test configures team callback settings with os.environ/LANGFUSE_PUBLIC_KEY and os.environ/LANGFUSE_SECRET_KEY references, which get resolved by ProxyConfig._get_team_config() via get_secret(). When these environment variables are not set, get_secret() returns None, and the subsequent TeamCallbackMetadata Pydantic model validation fails because callback_vars expects Dict[str, str].

  • Adds monkeypatch.setenv() calls for LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY to ensure consistent test behavior across all execution environments
  • Uses pytest's monkeypatch fixture for proper cleanup, avoiding env var leakage between tests

Confidence Score: 5/5

  • This PR is safe to merge — it only modifies a test file by adding environment variable mocking for test isolation.
  • The change is minimal (adding 2 monkeypatch.setenv calls and updating the function signature), correctly addresses a real test isolation problem, and follows pytest best practices. No production code is modified.
  • No files require special attention.

Important Files Changed

Filename Overview
tests/proxy_unit_tests/test_proxy_utils.py Adds monkeypatch fixture to mock LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY env vars, preventing Pydantic validation errors when running in parallel execution environments.

Sequence Diagram

sequenceDiagram
    participant Test as Test Function
    participant MP as monkeypatch
    participant PC as ProxyConfig
    participant GTC as _get_team_config
    participant GS as get_secret()
    participant ENV as os.environ
    participant TCM as TeamCallbackMetadata

    Test->>MP: setenv("LANGFUSE_PUBLIC_KEY", "test_public_key")
    Test->>MP: setenv("LANGFUSE_SECRET_KEY", "test_secret_key")
    MP->>ENV: Set env vars
    Test->>PC: update_config_state(config)
    Test->>PC: load_team_config("test")
    PC->>GTC: _get_team_config("test", all_teams_config)
    GTC->>GS: get_secret("os.environ/LANGFUSE_PUBLIC_KEY")
    GS->>ENV: os.environ.get("LANGFUSE_PUBLIC_KEY")
    ENV-->>GS: "test_public_key"
    GS-->>GTC: "test_public_key"
    GTC->>GS: get_secret("os.environ/LANGFUSE_SECRET_KEY")
    GS->>ENV: os.environ.get("LANGFUSE_SECRET_KEY")
    ENV-->>GS: "test_secret_key"
    GS-->>GTC: "test_secret_key"
    GTC-->>PC: team_config (with resolved values)
    PC-->>Test: team_config
    Test->>TCM: TeamCallbackMetadata(callback_vars=...)
    Note over TCM: Pydantic validates Dict[str, str] ✓
Loading

Last reviewed commit: 2d41b03

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@jquinter jquinter merged commit 44bb1da into main Feb 16, 2026
17 of 23 checks passed
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