Skip to content

Commit 48ca028

Browse files
authored
chore(client): move to LANGFUSE_BASE_URL (#1418)
1 parent f0619ca commit 48ca028

File tree

15 files changed

+482
-110
lines changed

15 files changed

+482
-110
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
runs-on: ubuntu-latest
5959
timeout-minutes: 30
6060
env:
61-
LANGFUSE_HOST: "http://localhost:3000"
61+
LANGFUSE_BASE_URL: "http://localhost:3000"
6262
LANGFUSE_PUBLIC_KEY: "pk-lf-1234567890"
6363
LANGFUSE_SECRET_KEY: "sk-lf-1234567890"
6464
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

langfuse/_client/client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
)
4848
from langfuse._client.datasets import DatasetClient, DatasetItemClient
4949
from langfuse._client.environment_variables import (
50+
LANGFUSE_BASE_URL,
5051
LANGFUSE_DEBUG,
5152
LANGFUSE_HOST,
5253
LANGFUSE_PUBLIC_KEY,
@@ -134,7 +135,8 @@ class Langfuse:
134135
Parameters:
135136
public_key (Optional[str]): Your Langfuse public API key. Can also be set via LANGFUSE_PUBLIC_KEY environment variable.
136137
secret_key (Optional[str]): Your Langfuse secret API key. Can also be set via LANGFUSE_SECRET_KEY environment variable.
137-
host (Optional[str]): The Langfuse API host URL. Defaults to "https://cloud.langfuse.com". Can also be set via LANGFUSE_HOST environment variable.
138+
base_url (Optional[str]): The Langfuse API base URL. Defaults to "https://cloud.langfuse.com". Can also be set via LANGFUSE_BASE_URL environment variable.
139+
host (Optional[str]): Deprecated. Use base_url instead. The Langfuse API host URL. Defaults to "https://cloud.langfuse.com".
138140
timeout (Optional[int]): Timeout in seconds for API requests. Defaults to 5 seconds.
139141
httpx_client (Optional[httpx.Client]): Custom httpx client for making non-tracing HTTP requests. If not provided, a default client will be created.
140142
debug (bool): Enable debug logging. Defaults to False. Can also be set via LANGFUSE_DEBUG environment variable.
@@ -195,6 +197,7 @@ def __init__(
195197
*,
196198
public_key: Optional[str] = None,
197199
secret_key: Optional[str] = None,
200+
base_url: Optional[str] = None,
198201
host: Optional[str] = None,
199202
timeout: Optional[int] = None,
200203
httpx_client: Optional[httpx.Client] = None,
@@ -211,7 +214,12 @@ def __init__(
211214
additional_headers: Optional[Dict[str, str]] = None,
212215
tracer_provider: Optional[TracerProvider] = None,
213216
):
214-
self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
217+
self._base_url = (
218+
base_url
219+
or os.environ.get(LANGFUSE_BASE_URL)
220+
or host
221+
or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
222+
)
215223
self._environment = environment or cast(
216224
str, os.environ.get(LANGFUSE_TRACING_ENVIRONMENT)
217225
)
@@ -269,7 +277,7 @@ def __init__(
269277
self._resources = LangfuseResourceManager(
270278
public_key=public_key,
271279
secret_key=secret_key,
272-
host=self._host,
280+
base_url=self._base_url,
273281
timeout=timeout,
274282
environment=self._environment,
275283
release=release,
@@ -2413,7 +2421,7 @@ def get_trace_url(self, *, trace_id: Optional[str] = None) -> Optional[str]:
24132421
final_trace_id = trace_id or self.get_current_trace_id()
24142422

24152423
return (
2416-
f"{self._host}/project/{project_id}/traces/{final_trace_id}"
2424+
f"{self._base_url}/project/{project_id}/traces/{final_trace_id}"
24172425
if project_id and final_trace_id
24182426
else None
24192427
)
@@ -2712,7 +2720,7 @@ async def process_item(item: ExperimentItem) -> ExperimentItemResult:
27122720
project_id = self._get_project_id()
27132721

27142722
if project_id:
2715-
dataset_run_url = f"{self._host}/project/{project_id}/datasets/{dataset_id}/runs/{dataset_run_id}"
2723+
dataset_run_url = f"{self._base_url}/project/{project_id}/datasets/{dataset_id}/runs/{dataset_run_id}"
27162724

27172725
except Exception:
27182726
pass # URL generation is optional

langfuse/_client/environment_variables.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@
3535
Secret API key of Langfuse project
3636
"""
3737

38+
LANGFUSE_BASE_URL = "LANGFUSE_BASE_URL"
39+
"""
40+
.. envvar:: LANGFUSE_BASE_URL
41+
42+
Base URL of Langfuse API. Can be set via `LANGFUSE_BASE_URL` environment variable.
43+
44+
**Default value:** ``"https://cloud.langfuse.com"``
45+
"""
46+
3847
LANGFUSE_HOST = "LANGFUSE_HOST"
3948
"""
4049
.. envvar:: LANGFUSE_HOST
4150
42-
Host of Langfuse API. Can be set via `LANGFUSE_HOST` environment variable.
51+
Deprecated. Use LANGFUSE_BASE_URL instead. Host of Langfuse API. Can be set via `LANGFUSE_HOST` environment variable.
4352
4453
**Default value:** ``"https://cloud.langfuse.com"``
4554
"""

langfuse/_client/get_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _create_client_from_instance(
4040
return Langfuse(
4141
public_key=public_key or instance.public_key,
4242
secret_key=instance.secret_key,
43-
host=instance.host,
43+
base_url=instance.base_url,
4444
tracing_enabled=instance.tracing_enabled,
4545
environment=instance.environment,
4646
timeout=instance.timeout,

langfuse/_client/observe.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,17 @@ async def async_wrapper(*args: Tuple[Any], **kwargs: Dict[str, Any]) -> Any:
313313
)
314314

315315
# handle starlette.StreamingResponse
316-
if type(result).__name__ == "StreamingResponse" and hasattr(result, "body_iterator"):
316+
if type(result).__name__ == "StreamingResponse" and hasattr(
317+
result, "body_iterator"
318+
):
317319
is_return_type_generator = True
318320

319-
result.body_iterator = self._wrap_async_generator_result(
320-
langfuse_span_or_generation,
321-
result.body_iterator,
322-
transform_to_string,
321+
result.body_iterator = (
322+
self._wrap_async_generator_result(
323+
langfuse_span_or_generation,
324+
result.body_iterator,
325+
transform_to_string,
326+
)
323327
)
324328

325329
langfuse_span_or_generation.update(output=result)
@@ -427,13 +431,17 @@ def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
427431
)
428432

429433
# handle starlette.StreamingResponse
430-
if type(result).__name__ == "StreamingResponse" and hasattr(result, "body_iterator"):
434+
if type(result).__name__ == "StreamingResponse" and hasattr(
435+
result, "body_iterator"
436+
):
431437
is_return_type_generator = True
432438

433-
result.body_iterator = self._wrap_async_generator_result(
434-
langfuse_span_or_generation,
435-
result.body_iterator,
436-
transform_to_string,
439+
result.body_iterator = (
440+
self._wrap_async_generator_result(
441+
langfuse_span_or_generation,
442+
result.body_iterator,
443+
transform_to_string,
444+
)
437445
)
438446

439447
langfuse_span_or_generation.update(output=result)

langfuse/_client/resource_manager.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __new__(
8383
*,
8484
public_key: str,
8585
secret_key: str,
86-
host: str,
86+
base_url: str,
8787
environment: Optional[str] = None,
8888
release: Optional[str] = None,
8989
timeout: Optional[int] = None,
@@ -115,7 +115,7 @@ def __new__(
115115
instance._initialize_instance(
116116
public_key=public_key,
117117
secret_key=secret_key,
118-
host=host,
118+
base_url=base_url,
119119
timeout=timeout,
120120
environment=environment,
121121
release=release,
@@ -142,7 +142,7 @@ def _initialize_instance(
142142
*,
143143
public_key: str,
144144
secret_key: str,
145-
host: str,
145+
base_url: str,
146146
environment: Optional[str] = None,
147147
release: Optional[str] = None,
148148
timeout: Optional[int] = None,
@@ -160,7 +160,7 @@ def _initialize_instance(
160160
self.public_key = public_key
161161
self.secret_key = secret_key
162162
self.tracing_enabled = tracing_enabled
163-
self.host = host
163+
self.base_url = base_url
164164
self.mask = mask
165165
self.environment = environment
166166

@@ -183,7 +183,7 @@ def _initialize_instance(
183183
langfuse_processor = LangfuseSpanProcessor(
184184
public_key=self.public_key,
185185
secret_key=secret_key,
186-
host=host,
186+
base_url=base_url,
187187
timeout=timeout,
188188
flush_at=flush_at,
189189
flush_interval=flush_interval,
@@ -212,7 +212,7 @@ def _initialize_instance(
212212
self.httpx_client = httpx.Client(timeout=timeout, headers=client_headers)
213213

214214
self.api = FernLangfuse(
215-
base_url=host,
215+
base_url=base_url,
216216
username=self.public_key,
217217
password=secret_key,
218218
x_langfuse_sdk_name="python",
@@ -222,7 +222,7 @@ def _initialize_instance(
222222
timeout=timeout,
223223
)
224224
self.async_api = AsyncFernLangfuse(
225-
base_url=host,
225+
base_url=base_url,
226226
username=self.public_key,
227227
password=secret_key,
228228
x_langfuse_sdk_name="python",
@@ -233,7 +233,7 @@ def _initialize_instance(
233233
score_ingestion_client = LangfuseClient(
234234
public_key=self.public_key,
235235
secret_key=secret_key,
236-
base_url=host,
236+
base_url=base_url,
237237
version=langfuse_version,
238238
timeout=timeout or 20,
239239
session=self.httpx_client,
@@ -290,7 +290,7 @@ def _initialize_instance(
290290
langfuse_logger.info(
291291
f"Startup: Langfuse tracer successfully initialized | "
292292
f"public_key={self.public_key} | "
293-
f"host={host} | "
293+
f"base_url={base_url} | "
294294
f"environment={environment or 'default'} | "
295295
f"sample_rate={sample_rate if sample_rate is not None else 1.0} | "
296296
f"media_threads={media_upload_thread_count or 1}"

langfuse/_client/span.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ def __init__(
190190
{k: v for k, v in attributes.items() if v is not None}
191191
)
192192
# Set OTEL span status if level is ERROR
193-
self._set_otel_span_status_if_error(level=level, status_message=status_message)
193+
self._set_otel_span_status_if_error(
194+
level=level, status_message=status_message
195+
)
194196

195197
def end(self, *, end_time: Optional[int] = None) -> "LangfuseObservationWrapper":
196198
"""End the span, marking it as completed.
@@ -544,7 +546,7 @@ def _process_media_in_attribute(
544546
return data
545547

546548
def _set_otel_span_status_if_error(
547-
self, *, level: Optional[SpanLevel] = None, status_message: Optional[str] = None
549+
self, *, level: Optional[SpanLevel] = None, status_message: Optional[str] = None
548550
) -> None:
549551
"""Set OpenTelemetry span status to ERROR if level is ERROR.
550552

langfuse/_client/span_processor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
*,
5353
public_key: str,
5454
secret_key: str,
55-
host: str,
55+
base_url: str,
5656
timeout: Optional[int] = None,
5757
flush_at: Optional[int] = None,
5858
flush_interval: Optional[float] = None,
@@ -94,9 +94,9 @@ def __init__(
9494
traces_export_path = os.environ.get(LANGFUSE_OTEL_TRACES_EXPORT_PATH, None)
9595

9696
endpoint = (
97-
f"{host}/{traces_export_path}"
97+
f"{base_url}/{traces_export_path}"
9898
if traces_export_path
99-
else f"{host}/api/public/otel/v1/traces"
99+
else f"{base_url}/api/public/otel/v1/traces"
100100
)
101101

102102
langfuse_span_exporter = OTLPSpanExporter(

tests/api_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, username=None, password=None, base_url=None):
99
username = username if username else os.environ["LANGFUSE_PUBLIC_KEY"]
1010
password = password if password else os.environ["LANGFUSE_SECRET_KEY"]
1111
self.auth = (username, password)
12-
self.BASE_URL = base_url if base_url else os.environ["LANGFUSE_HOST"]
12+
self.BASE_URL = base_url if base_url else os.environ["LANGFUSE_BASE_URL"]
1313

1414
def get_observation(self, observation_id):
1515
sleep(1)

tests/test_additional_headers_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_span_processor_has_additional_headers_in_otel_exporter(self):
143143
processor = LangfuseSpanProcessor(
144144
public_key="test-public-key",
145145
secret_key="test-secret-key",
146-
host="https://mock-host.com",
146+
base_url="https://mock-host.com",
147147
additional_headers=additional_headers,
148148
)
149149

@@ -170,7 +170,7 @@ def test_span_processor_none_additional_headers_works(self):
170170
processor = LangfuseSpanProcessor(
171171
public_key="test-public-key",
172172
secret_key="test-secret-key",
173-
host="https://mock-host.com",
173+
base_url="https://mock-host.com",
174174
additional_headers=None,
175175
)
176176

0 commit comments

Comments
 (0)