Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down
6 changes: 4 additions & 2 deletions langfuse/_client/span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/test_span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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
):
Expand Down
Loading