From 48a65ce585c682ac84a3cef2f407c577fafda7be Mon Sep 17 00:00:00 2001 From: Sid13SST <220563227+Sid13SST@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:21:07 +0000 Subject: [PATCH] fix(client): don't double the slash after a base_url with a trailing slash A base_url such as "http://localhost:3000/" was joined with f"{base_url}/...", so the OTLP exporter posted spans to "//api/public/otel/v1/traces" and get_trace_url() / experiment_url returned "http://localhost:3000//project/...". The generated API client already normalizes this via urljoin; these three URLs bypass it. Strip the trailing slash where the SDK builds these URLs. _base_url itself is unchanged. --- langfuse/_client/client.py | 4 ++-- langfuse/_client/span_processor.py | 6 ++++-- tests/unit/test_experiment.py | 18 ++++++++++++++++++ tests/unit/test_initialization.py | 15 +++++++++++++++ tests/unit/test_span_processor.py | 23 ++++++++++++++++++++++- 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index fd4928316..7dba52cc3 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -2538,7 +2538,7 @@ def get_trace_url(self, *, trace_id: Optional[str] = None) -> Optional[str]: project_id = self._get_project_id() return ( - f"{self._base_url}/project/{project_id}/traces/{final_trace_id}" + f"{self._base_url.rstrip('/')}/project/{project_id}/traces/{final_trace_id}" if project_id and final_trace_id else None ) @@ -2935,7 +2935,7 @@ async def process_item(item: ExperimentItem) -> ExperimentItemResult: langfuse_logger.error("Run evaluator failed: %s", e) experiment_url = ( - f"{self._base_url}/project/{project_id}/experiments/results?baseline={urllib.parse.quote(experiment_id, safe='')}" + f"{self._base_url.rstrip('/')}/project/{project_id}/experiments/results?baseline={urllib.parse.quote(experiment_id, safe='')}" if project_id else None ) diff --git a/langfuse/_client/span_processor.py b/langfuse/_client/span_processor.py index b6ebbbce6..35d3711ea 100644 --- a/langfuse/_client/span_processor.py +++ b/langfuse/_client/span_processor.py @@ -199,10 +199,12 @@ def __init__( traces_export_path = os.environ.get(LANGFUSE_OTEL_TRACES_EXPORT_PATH, None) + # Avoid "//" in the path when base_url has a trailing slash. + endpoint_base_url = base_url.rstrip("/") endpoint = ( - f"{base_url}/{traces_export_path}" + f"{endpoint_base_url}/{traces_export_path}" if traces_export_path - else f"{base_url}/api/public/otel/v1/traces" + else f"{endpoint_base_url}/api/public/otel/v1/traces" ) span_exporter = OTLPSpanExporter( diff --git a/tests/unit/test_experiment.py b/tests/unit/test_experiment.py index 0224ab58e..5593b1365 100644 --- a/tests/unit/test_experiment.py +++ b/tests/unit/test_experiment.py @@ -785,6 +785,24 @@ def test_supplied_id_is_url_encoded_in_the_experiment_url( "http://test-host/project/p/experiments/results?baseline=team%20a%2Frun%231" ) + def test_trailing_slash_in_base_url_is_not_doubled_in_the_experiment_url( + self, langfuse_memory_client, monkeypatch + ): + monkeypatch.setattr(langfuse_memory_client, "create_score", MagicMock()) + monkeypatch.setattr(langfuse_memory_client, "_get_project_id", lambda: "p") + monkeypatch.setattr(langfuse_memory_client, "_base_url", "http://test-host/") + + result = langfuse_memory_client.run_experiment( + name="exp", + experiment_id="e", + data=[{"input": "a"}], + task=lambda *, item, **kwargs: item["input"], + ) + + assert result.experiment_url == ( + "http://test-host/project/p/experiments/results?baseline=e" + ) + def test_supplied_id_is_used_when_called_inside_a_running_event_loop( self, langfuse_memory_client, find_spans, monkeypatch ): diff --git a/tests/unit/test_initialization.py b/tests/unit/test_initialization.py index 1f682d3fe..d8fa56eda 100644 --- a/tests/unit/test_initialization.py +++ b/tests/unit/test_initialization.py @@ -297,6 +297,21 @@ def test_trailing_slash_handling(self, cleanup_env_vars): # The SDK should accept the URL as-is (API client will handle normalization) assert client1._base_url == "http://test.com/" + def test_trailing_slash_is_not_doubled_in_trace_url(self, cleanup_env_vars): + """Test that a trailing slash in base_url does not produce '//' in UI links.""" + client = Langfuse( + base_url="http://test.com/", + public_key="test_pk", + secret_key="test_sk", + ) + client._project_id = "project-id" + trace_id = "a" * 32 + + assert ( + client.get_trace_url(trace_id=trace_id) + == f"http://test.com/project/project-id/traces/{trace_id}" + ) + def test_urls_with_paths(self, cleanup_env_vars): """Test that URLs with paths work correctly.""" client = Langfuse( diff --git a/tests/unit/test_span_processor.py b/tests/unit/test_span_processor.py index 97d53ff58..9a23499f8 100644 --- a/tests/unit/test_span_processor.py +++ b/tests/unit/test_span_processor.py @@ -86,6 +86,7 @@ def test_span_processor_uses_env_flush_settings_when_constructor_omits_them( class _RecordedRequest(NamedTuple): content_encoding: Optional[str] body: bytes + path: str class _RecordingOTLPHandler(BaseHTTPRequestHandler): @@ -94,7 +95,12 @@ class _RecordingOTLPHandler(BaseHTTPRequestHandler): def do_POST(self): body = self.rfile.read(int(self.headers.get("Content-Length", 0))) self.received_requests.append( - _RecordedRequest(self.headers.get("Content-Encoding"), body) + _RecordedRequest( + self.headers.get("Content-Encoding"), + body, + # self.path collapses a leading "//", so read the raw request line. + self.requestline.split()[1], + ) ) self.send_response(200) self.end_headers() @@ -182,6 +188,21 @@ def test_default_exporter_enforces_max_batch_size_bytes_at_boundary( ) +def test_default_exporter_ignores_trailing_slash_in_base_url(otlp_http_server): + base_url, received_requests = otlp_http_server + processor = _default_exporter_processor(base_url + "/") + + try: + result = processor._batch_processor._exporter.export(_finished_spans("ok")) + finally: + processor.shutdown() + + assert result == SpanExportResult.SUCCESS + assert [request.path for request in received_requests] == [ + "/api/public/otel/v1/traces" + ] + + def test_oversized_batch_is_dropped_on_flush_without_blocking_later_batches( monkeypatch, caplog, otlp_http_server ):