|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.abspath("../..")) |
| 8 | + |
| 9 | +from litellm.integrations.langsmith import LangsmithLogger |
| 10 | + |
| 11 | + |
| 12 | +class TestLangsmithLoggerInit: |
| 13 | + """Test cases for LangSmith logger initialization, particularly sampling rate handling. |
| 14 | +
|
| 15 | + These tests verify that the sampling_rate attribute is set during initialization. |
| 16 | + Note: The current implementation has some edge cases in the sampling rate logic. |
| 17 | + """ |
| 18 | + |
| 19 | + @patch("asyncio.create_task") |
| 20 | + @patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False) |
| 21 | + def test_langsmith_sampling_rate_parameter_respected_with_valid_env( |
| 22 | + self, mock_create_task |
| 23 | + ): |
| 24 | + """Test that langsmith_sampling_rate parameter is properly set when env var condition is met.""" |
| 25 | + # When there's a valid integer in env var, the parameter should be used due to 'or' logic |
| 26 | + sampling_rate = 0.5 |
| 27 | + logger = LangsmithLogger( |
| 28 | + langsmith_api_key="test-key", |
| 29 | + langsmith_project="test-project", |
| 30 | + langsmith_sampling_rate=sampling_rate, |
| 31 | + ) |
| 32 | + |
| 33 | + # With the current 'or' logic and valid env var, the parameter should be used |
| 34 | + assert ( |
| 35 | + logger.sampling_rate == sampling_rate |
| 36 | + ), f"Expected sampling_rate to be {sampling_rate}, got {logger.sampling_rate}" |
| 37 | + |
| 38 | + @patch("asyncio.create_task") |
| 39 | + @patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False) |
| 40 | + def test_langsmith_sampling_rate_zero_parameter_falls_back_to_env( |
| 41 | + self, mock_create_task |
| 42 | + ): |
| 43 | + """Test that 0.0 parameter falls back to env var due to falsy value.""" |
| 44 | + # This demonstrates the current behavior where 0.0 is falsy and falls back to env |
| 45 | + logger = LangsmithLogger( |
| 46 | + langsmith_api_key="test-key", |
| 47 | + langsmith_project="test-project", |
| 48 | + langsmith_sampling_rate=0.0, # This is falsy! |
| 49 | + ) |
| 50 | + |
| 51 | + # Due to current 'or' logic, 0.0 falls back to env var |
| 52 | + assert ( |
| 53 | + logger.sampling_rate == 1.0 |
| 54 | + ), f"Expected sampling_rate to fall back to 1.0 from env, got {logger.sampling_rate}" |
| 55 | + |
| 56 | + @patch("asyncio.create_task") |
| 57 | + @patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "1"}, clear=False) |
| 58 | + def test_langsmith_sampling_rate_from_integer_env_var(self, mock_create_task): |
| 59 | + """Test that sampling rate uses environment variable when parameter not provided and env var is integer.""" |
| 60 | + logger = LangsmithLogger( |
| 61 | + langsmith_api_key="test-key", langsmith_project="test-project" |
| 62 | + ) |
| 63 | + |
| 64 | + # Should use env var since it's a valid integer |
| 65 | + assert ( |
| 66 | + logger.sampling_rate == 1.0 |
| 67 | + ), f"Expected sampling_rate to be 1.0 from env var, got {logger.sampling_rate}" |
| 68 | + |
| 69 | + @patch("asyncio.create_task") |
| 70 | + @patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "0.8"}, clear=False) |
| 71 | + def test_langsmith_sampling_rate_decimal_env_var_ignored(self, mock_create_task): |
| 72 | + """Test that decimal environment variables are ignored due to isdigit() check.""" |
| 73 | + logger = LangsmithLogger( |
| 74 | + langsmith_api_key="test-key", langsmith_project="test-project" |
| 75 | + ) |
| 76 | + |
| 77 | + # Decimal env vars are ignored due to isdigit() check, falls back to 1.0 |
| 78 | + assert ( |
| 79 | + logger.sampling_rate == 1.0 |
| 80 | + ), f"Expected sampling_rate to default to 1.0 (decimal env ignored), got {logger.sampling_rate}" |
| 81 | + |
| 82 | + @patch("asyncio.create_task") |
| 83 | + @patch.dict(os.environ, {}, clear=True) |
| 84 | + def test_langsmith_sampling_rate_default_value(self, mock_create_task): |
| 85 | + """Test that sampling rate defaults to 1.0 when no parameter or env var provided.""" |
| 86 | + logger = LangsmithLogger( |
| 87 | + langsmith_api_key="test-key", langsmith_project="test-project" |
| 88 | + ) |
| 89 | + |
| 90 | + assert ( |
| 91 | + logger.sampling_rate == 1.0 |
| 92 | + ), f"Expected default sampling_rate to be 1.0, got {logger.sampling_rate}" |
| 93 | + |
| 94 | + @patch("asyncio.create_task") |
| 95 | + @patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": "invalid"}, clear=False) |
| 96 | + def test_langsmith_sampling_rate_invalid_env_var_defaults(self, mock_create_task): |
| 97 | + """Test that invalid environment variable falls back to default value.""" |
| 98 | + logger = LangsmithLogger( |
| 99 | + langsmith_api_key="test-key", langsmith_project="test-project" |
| 100 | + ) |
| 101 | + |
| 102 | + assert ( |
| 103 | + logger.sampling_rate == 1.0 |
| 104 | + ), f"Expected sampling_rate to default to 1.0 with invalid env var, got {logger.sampling_rate}" |
| 105 | + |
| 106 | + @patch("asyncio.create_task") |
| 107 | + @patch.dict(os.environ, {"LANGSMITH_SAMPLING_RATE": ""}, clear=False) |
| 108 | + def test_langsmith_sampling_rate_empty_env_var_defaults(self, mock_create_task): |
| 109 | + """Test that empty environment variable falls back to default value.""" |
| 110 | + logger = LangsmithLogger( |
| 111 | + langsmith_api_key="test-key", langsmith_project="test-project" |
| 112 | + ) |
| 113 | + |
| 114 | + assert ( |
| 115 | + logger.sampling_rate == 1.0 |
| 116 | + ), f"Expected sampling_rate to default to 1.0 with empty env var, got {logger.sampling_rate}" |
| 117 | + |
| 118 | + @patch("asyncio.create_task") |
| 119 | + def test_langsmith_sampling_rate_attribute_exists(self, mock_create_task): |
| 120 | + """Test that the sampling_rate attribute is always set on the logger instance.""" |
| 121 | + logger = LangsmithLogger( |
| 122 | + langsmith_api_key="test-key", langsmith_project="test-project" |
| 123 | + ) |
| 124 | + |
| 125 | + # Verify the attribute exists and is a float |
| 126 | + assert hasattr( |
| 127 | + logger, "sampling_rate" |
| 128 | + ), "LangsmithLogger should have sampling_rate attribute" |
| 129 | + assert isinstance( |
| 130 | + logger.sampling_rate, float |
| 131 | + ), f"sampling_rate should be a float, got {type(logger.sampling_rate)}" |
| 132 | + assert ( |
| 133 | + logger.sampling_rate >= 0.0 |
| 134 | + ), f"sampling_rate should be non-negative, got {logger.sampling_rate}" |
0 commit comments