|
| 1 | +"""Test suite for Langfuse client initialization with LANGFUSE_HOST and LANGFUSE_BASE_URL. |
| 2 | +
|
| 3 | +This test suite verifies that both LANGFUSE_HOST (deprecated) and LANGFUSE_BASE_URL |
| 4 | +environment variables work correctly for initializing the Langfuse client. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from langfuse import Langfuse |
| 12 | + |
| 13 | + |
| 14 | +class TestClientInitialization: |
| 15 | + """Tests for Langfuse client initialization with different URL configurations.""" |
| 16 | + |
| 17 | + @pytest.fixture |
| 18 | + def cleanup_env_vars(self): |
| 19 | + """Fixture to clean up environment variables before and after each test.""" |
| 20 | + # Store original values |
| 21 | + original_base_url = os.environ.get("LANGFUSE_BASE_URL") |
| 22 | + original_host = os.environ.get("LANGFUSE_HOST") |
| 23 | + original_public_key = os.environ.get("LANGFUSE_PUBLIC_KEY") |
| 24 | + original_secret_key = os.environ.get("LANGFUSE_SECRET_KEY") |
| 25 | + |
| 26 | + # Remove them for the test |
| 27 | + for key in ["LANGFUSE_BASE_URL", "LANGFUSE_HOST"]: |
| 28 | + if key in os.environ: |
| 29 | + del os.environ[key] |
| 30 | + |
| 31 | + yield |
| 32 | + |
| 33 | + # Restore original values |
| 34 | + for key, value in [ |
| 35 | + ("LANGFUSE_BASE_URL", original_base_url), |
| 36 | + ("LANGFUSE_HOST", original_host), |
| 37 | + ("LANGFUSE_PUBLIC_KEY", original_public_key), |
| 38 | + ("LANGFUSE_SECRET_KEY", original_secret_key), |
| 39 | + ]: |
| 40 | + if value is not None: |
| 41 | + os.environ[key] = value |
| 42 | + elif key in os.environ: |
| 43 | + del os.environ[key] |
| 44 | + |
| 45 | + def test_base_url_parameter_takes_precedence(self, cleanup_env_vars): |
| 46 | + """Test that base_url parameter takes highest precedence.""" |
| 47 | + os.environ["LANGFUSE_BASE_URL"] = "http://env-base-url.com" |
| 48 | + os.environ["LANGFUSE_HOST"] = "http://env-host.com" |
| 49 | + |
| 50 | + client = Langfuse( |
| 51 | + base_url="http://param-base-url.com", |
| 52 | + host="http://param-host.com", |
| 53 | + public_key="test_pk", |
| 54 | + secret_key="test_sk", |
| 55 | + ) |
| 56 | + |
| 57 | + assert client._base_url == "http://param-base-url.com" |
| 58 | + |
| 59 | + def test_env_base_url_takes_precedence_over_host_param(self, cleanup_env_vars): |
| 60 | + """Test that LANGFUSE_BASE_URL env var takes precedence over host parameter.""" |
| 61 | + os.environ["LANGFUSE_BASE_URL"] = "http://env-base-url.com" |
| 62 | + |
| 63 | + client = Langfuse( |
| 64 | + host="http://param-host.com", |
| 65 | + public_key="test_pk", |
| 66 | + secret_key="test_sk", |
| 67 | + ) |
| 68 | + |
| 69 | + assert client._base_url == "http://env-base-url.com" |
| 70 | + |
| 71 | + def test_host_parameter_fallback(self, cleanup_env_vars): |
| 72 | + """Test that host parameter works as fallback when base_url is not set.""" |
| 73 | + client = Langfuse( |
| 74 | + host="http://param-host.com", |
| 75 | + public_key="test_pk", |
| 76 | + secret_key="test_sk", |
| 77 | + ) |
| 78 | + |
| 79 | + assert client._base_url == "http://param-host.com" |
| 80 | + |
| 81 | + def test_env_host_fallback(self, cleanup_env_vars): |
| 82 | + """Test that LANGFUSE_HOST env var works as fallback.""" |
| 83 | + os.environ["LANGFUSE_HOST"] = "http://env-host.com" |
| 84 | + |
| 85 | + client = Langfuse( |
| 86 | + public_key="test_pk", |
| 87 | + secret_key="test_sk", |
| 88 | + ) |
| 89 | + |
| 90 | + assert client._base_url == "http://env-host.com" |
| 91 | + |
| 92 | + def test_default_base_url(self, cleanup_env_vars): |
| 93 | + """Test that default base_url is used when nothing is set.""" |
| 94 | + client = Langfuse( |
| 95 | + public_key="test_pk", |
| 96 | + secret_key="test_sk", |
| 97 | + ) |
| 98 | + |
| 99 | + assert client._base_url == "https://cloud.langfuse.com" |
| 100 | + |
| 101 | + def test_base_url_env_var(self, cleanup_env_vars): |
| 102 | + """Test that LANGFUSE_BASE_URL environment variable is used correctly.""" |
| 103 | + os.environ["LANGFUSE_BASE_URL"] = "http://test-base-url.com" |
| 104 | + |
| 105 | + client = Langfuse( |
| 106 | + public_key="test_pk", |
| 107 | + secret_key="test_sk", |
| 108 | + ) |
| 109 | + |
| 110 | + assert client._base_url == "http://test-base-url.com" |
| 111 | + |
| 112 | + def test_host_env_var(self, cleanup_env_vars): |
| 113 | + """Test that LANGFUSE_HOST environment variable is used correctly (deprecated).""" |
| 114 | + os.environ["LANGFUSE_HOST"] = "http://test-host.com" |
| 115 | + |
| 116 | + client = Langfuse( |
| 117 | + public_key="test_pk", |
| 118 | + secret_key="test_sk", |
| 119 | + ) |
| 120 | + |
| 121 | + assert client._base_url == "http://test-host.com" |
| 122 | + |
| 123 | + def test_base_url_parameter(self, cleanup_env_vars): |
| 124 | + """Test that base_url parameter is used correctly.""" |
| 125 | + client = Langfuse( |
| 126 | + base_url="http://param-base-url.com", |
| 127 | + public_key="test_pk", |
| 128 | + secret_key="test_sk", |
| 129 | + ) |
| 130 | + |
| 131 | + assert client._base_url == "http://param-base-url.com" |
| 132 | + |
| 133 | + def test_precedence_order_all_set(self, cleanup_env_vars): |
| 134 | + """Test complete precedence order: base_url param > env > host param > env > default.""" |
| 135 | + os.environ["LANGFUSE_BASE_URL"] = "http://env-base-url.com" |
| 136 | + os.environ["LANGFUSE_HOST"] = "http://env-host.com" |
| 137 | + |
| 138 | + # Case 1: base_url parameter wins |
| 139 | + client1 = Langfuse( |
| 140 | + base_url="http://param-base-url.com", |
| 141 | + host="http://param-host.com", |
| 142 | + public_key="test_pk", |
| 143 | + secret_key="test_sk", |
| 144 | + ) |
| 145 | + assert client1._base_url == "http://param-base-url.com" |
| 146 | + |
| 147 | + # Case 2: LANGFUSE_BASE_URL env var wins when base_url param not set |
| 148 | + client2 = Langfuse( |
| 149 | + host="http://param-host.com", |
| 150 | + public_key="test_pk", |
| 151 | + secret_key="test_sk", |
| 152 | + ) |
| 153 | + assert client2._base_url == "http://env-base-url.com" |
| 154 | + |
| 155 | + def test_precedence_without_base_url(self, cleanup_env_vars): |
| 156 | + """Test precedence when base_url options are not set.""" |
| 157 | + os.environ["LANGFUSE_HOST"] = "http://env-host.com" |
| 158 | + |
| 159 | + # Case 1: host parameter wins |
| 160 | + client1 = Langfuse( |
| 161 | + host="http://param-host.com", |
| 162 | + public_key="test_pk", |
| 163 | + secret_key="test_sk", |
| 164 | + ) |
| 165 | + assert client1._base_url == "http://param-host.com" |
| 166 | + |
| 167 | + # Case 2: LANGFUSE_HOST env var is used |
| 168 | + client2 = Langfuse( |
| 169 | + public_key="test_pk", |
| 170 | + secret_key="test_sk", |
| 171 | + ) |
| 172 | + assert client2._base_url == "http://env-host.com" |
| 173 | + |
| 174 | + def test_url_used_in_api_client(self, cleanup_env_vars): |
| 175 | + """Test that the resolved base_url is correctly passed to API clients.""" |
| 176 | + test_url = "http://test-unique-api.com" |
| 177 | + # Use a unique public key to avoid singleton conflicts |
| 178 | + client = Langfuse( |
| 179 | + base_url=test_url, |
| 180 | + public_key=f"test_pk_{test_url}", |
| 181 | + secret_key="test_sk", |
| 182 | + ) |
| 183 | + |
| 184 | + # Check that the API client has the correct base_url |
| 185 | + assert client.api._client_wrapper._base_url == test_url |
| 186 | + assert client.async_api._client_wrapper._base_url == test_url |
| 187 | + |
| 188 | + def test_url_used_in_trace_url_generation(self, cleanup_env_vars): |
| 189 | + """Test that the resolved base_url is stored correctly for trace URL generation.""" |
| 190 | + test_url = "http://test-trace-api.com" |
| 191 | + # Use a unique public key to avoid singleton conflicts |
| 192 | + client = Langfuse( |
| 193 | + base_url=test_url, |
| 194 | + public_key=f"test_pk_{test_url}", |
| 195 | + secret_key="test_sk", |
| 196 | + ) |
| 197 | + |
| 198 | + # Verify that the base_url is stored correctly and will be used for URL generation |
| 199 | + # We can't test the full URL generation without making network calls to get project_id |
| 200 | + # but we can verify the base_url is correctly set |
| 201 | + assert client._base_url == test_url |
| 202 | + |
| 203 | + def test_both_base_url_and_host_params(self, cleanup_env_vars): |
| 204 | + """Test that base_url parameter takes precedence over host parameter.""" |
| 205 | + client = Langfuse( |
| 206 | + base_url="http://base-url.com", |
| 207 | + host="http://host.com", |
| 208 | + public_key="test_pk", |
| 209 | + secret_key="test_sk", |
| 210 | + ) |
| 211 | + |
| 212 | + assert client._base_url == "http://base-url.com" |
| 213 | + |
| 214 | + def test_both_env_vars_set(self, cleanup_env_vars): |
| 215 | + """Test that LANGFUSE_BASE_URL takes precedence over LANGFUSE_HOST.""" |
| 216 | + os.environ["LANGFUSE_BASE_URL"] = "http://base-url.com" |
| 217 | + os.environ["LANGFUSE_HOST"] = "http://host.com" |
| 218 | + |
| 219 | + client = Langfuse( |
| 220 | + public_key="test_pk", |
| 221 | + secret_key="test_sk", |
| 222 | + ) |
| 223 | + |
| 224 | + assert client._base_url == "http://base-url.com" |
| 225 | + |
| 226 | + def test_localhost_urls(self, cleanup_env_vars): |
| 227 | + """Test that localhost URLs work correctly.""" |
| 228 | + # Test with base_url |
| 229 | + client1 = Langfuse( |
| 230 | + base_url="http://localhost:3000", |
| 231 | + public_key="test_pk", |
| 232 | + secret_key="test_sk", |
| 233 | + ) |
| 234 | + assert client1._base_url == "http://localhost:3000" |
| 235 | + |
| 236 | + # Test with host (deprecated) |
| 237 | + client2 = Langfuse( |
| 238 | + host="http://localhost:3000", |
| 239 | + public_key="test_pk", |
| 240 | + secret_key="test_sk", |
| 241 | + ) |
| 242 | + assert client2._base_url == "http://localhost:3000" |
| 243 | + |
| 244 | + # Test with env var |
| 245 | + os.environ["LANGFUSE_BASE_URL"] = "http://localhost:3000" |
| 246 | + client3 = Langfuse( |
| 247 | + public_key="test_pk", |
| 248 | + secret_key="test_sk", |
| 249 | + ) |
| 250 | + assert client3._base_url == "http://localhost:3000" |
| 251 | + |
| 252 | + def test_trailing_slash_handling(self, cleanup_env_vars): |
| 253 | + """Test that URLs with trailing slashes are handled correctly.""" |
| 254 | + # URLs with trailing slashes should work |
| 255 | + client1 = Langfuse( |
| 256 | + base_url="http://test.com/", |
| 257 | + public_key="test_pk", |
| 258 | + secret_key="test_sk", |
| 259 | + ) |
| 260 | + # The SDK should accept the URL as-is (API client will handle normalization) |
| 261 | + assert client1._base_url == "http://test.com/" |
| 262 | + |
| 263 | + def test_urls_with_paths(self, cleanup_env_vars): |
| 264 | + """Test that URLs with paths work correctly.""" |
| 265 | + client = Langfuse( |
| 266 | + base_url="http://test.com/api/v1", |
| 267 | + public_key="test_pk", |
| 268 | + secret_key="test_sk", |
| 269 | + ) |
| 270 | + assert client._base_url == "http://test.com/api/v1" |
| 271 | + |
| 272 | + def test_https_and_http_urls(self, cleanup_env_vars): |
| 273 | + """Test that both HTTPS and HTTP URLs work.""" |
| 274 | + # HTTPS |
| 275 | + client1 = Langfuse( |
| 276 | + base_url="https://secure.com", |
| 277 | + public_key="test_pk", |
| 278 | + secret_key="test_sk", |
| 279 | + ) |
| 280 | + assert client1._base_url == "https://secure.com" |
| 281 | + |
| 282 | + # HTTP |
| 283 | + client2 = Langfuse( |
| 284 | + base_url="http://insecure.com", |
| 285 | + public_key="test_pk", |
| 286 | + secret_key="test_sk", |
| 287 | + ) |
| 288 | + assert client2._base_url == "http://insecure.com" |
0 commit comments