Skip to content

Fix Langfuse logger test mock setup#17588

Merged
AlexsanderHamir merged 3 commits intomainfrom
litellm_ci_fix_4
Dec 6, 2025
Merged

Fix Langfuse logger test mock setup#17588
AlexsanderHamir merged 3 commits intomainfrom
litellm_ci_fix_4

Conversation

@AlexsanderHamir
Copy link
Contributor

@AlexsanderHamir AlexsanderHamir commented Dec 6, 2025

Title

Fix Langfuse logger test mock setup

Relevant issues

Fixes failing test: test_log_langfuse_v2_handles_null_usage_values

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

Test Fix: Langfuse logger mock setup

Fixed test_log_langfuse_v2_handles_null_usage_values test failure caused by incorrect mock configuration.

Problem:

  • Test failed with AssertionError: Expected 'generation' to have been called once. Called 0 times
  • Logger's Langfuse client wasn't using the test's mock despite sys.modules mocking
  • Logger initialization creates its own client instance, bypassing the mock

Solution:

  • Explicitly set logger.Langfuse to mock client after initialization
  • Set logger.langfuse_sdk_version to ensure _supports_* methods work correctly
  • Added mock_langfuse_client.client attribute to prevent init errors
  • Added trace_id to mock_langfuse_generation for proper return value handling
  • Removed redundant mock setup code

Result:

  • Test now passes and correctly verifies _log_langfuse_v2 converts None usage values to 0
  • All tests in TestLangfuseUsageDetails class pass (5/5)

The test was failing because the logger's Langfuse client wasn't properly
mocked. Even though sys.modules was mocked, the logger's __init__ method
creates its own Langfuse client instance that wasn't using the test's mock.

Changes:
- Explicitly set logger.Langfuse to the mock client after initialization
- Set logger.langfuse_sdk_version to ensure _supports_* methods work correctly
- Added mock_langfuse_client.client attribute to prevent errors during init
- Added trace_id to mock_langfuse_generation for proper return value handling
- Removed redundant mock setup code

This ensures the test can properly verify that _log_langfuse_v2 correctly
converts None usage values to 0 by allowing the mock's generation method
to be called and asserted.

Fixes: AssertionError: Expected 'generation' to have been called once. Called 0 times.
@vercel
Copy link

vercel bot commented Dec 6, 2025

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

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Dec 6, 2025 1:56pm

Reverts c6436e1 - the version 0.4.11 doesn't exist on PyPI yet,
causing CI to fail when installing dependencies. Reverting back to 0.4.10
until the package is published.
@AlexsanderHamir AlexsanderHamir changed the title Fix Langfuse logger test mock setup and bump proxy-extras version Fix Langfuse logger test mock setup Dec 6, 2025
@AlexsanderHamir AlexsanderHamir merged commit a885e21 into main Dec 6, 2025
8 of 57 checks passed
AlexsanderHamir added a commit that referenced this pull request Dec 6, 2025
AlexsanderHamir added a commit that referenced this pull request Dec 6, 2025
jquinter added a commit that referenced this pull request Feb 14, 2026
Fixes test_log_langfuse_v2_handles_null_usage_values flaky test failure
by properly cleaning up sys.modules['langfuse'] in tearDown.

Changes:
- Store original langfuse module in setUp before mocking
- Restore original or remove mock in tearDown to prevent state pollution
- Remove invalid print_verbose parameter from log_event_on_langfuse

Root Cause:
The tearDown method was not cleaning up sys.modules['langfuse'] after
each test, causing mock state to leak between tests. This caused
intermittent failures in CI, especially when tests run in parallel or
in different orders.

Impact:
This test has a long history of flakiness with multiple attempted fixes
(#20475, #17599, #17594, #17591, #17588). The missing sys.modules cleanup
was the underlying issue causing continued failures despite those patches.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
jquinter added a commit that referenced this pull request Feb 15, 2026
Fixes test_log_langfuse_v2_handles_null_usage_values flaky test failure
by properly cleaning up sys.modules['langfuse'] in tearDown.

Changes:
- Store original langfuse module in setUp before mocking
- Restore original or remove mock in tearDown to prevent state pollution
- Remove invalid print_verbose parameter from log_event_on_langfuse

Root Cause:
The tearDown method was not cleaning up sys.modules['langfuse'] after
each test, causing mock state to leak between tests. This caused
intermittent failures in CI, especially when tests run in parallel or
in different orders.

Impact:
This test has a long history of flakiness with multiple attempted fixes
(#20475, #17599, #17594, #17591, #17588). The missing sys.modules cleanup
was the underlying issue causing continued failures despite those patches.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
sameetn pushed a commit to sameetn/litellm that referenced this pull request Feb 16, 2026
Fixes test_log_langfuse_v2_handles_null_usage_values flaky test failure
by properly cleaning up sys.modules['langfuse'] in tearDown.

Changes:
- Store original langfuse module in setUp before mocking
- Restore original or remove mock in tearDown to prevent state pollution
- Remove invalid print_verbose parameter from log_event_on_langfuse

Root Cause:
The tearDown method was not cleaning up sys.modules['langfuse'] after
each test, causing mock state to leak between tests. This caused
intermittent failures in CI, especially when tests run in parallel or
in different orders.

Impact:
This test has a long history of flakiness with multiple attempted fixes
(BerriAI#20475, BerriAI#17599, BerriAI#17594, BerriAI#17591, BerriAI#17588). The missing sys.modules cleanup
was the underlying issue causing continued failures despite those patches.

Co-Authored-By: Claude Sonnet 4.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