Repository navigation
fix(client): strip trailing slashes from base_url - #2152
Conversation
Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com>
|
@claude review |
There was a problem hiding this comment.
Claude Code Review
No review was started: this request came from a bot account. Manual reviews can only be requested by someone with write access to this repository. Ask a maintainer to comment @claude review, or have your automation post the comment from a user account with write access.
Tip: disable this comment in your organization's Code Review settings.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
| or os.environ.get(LANGFUSE_BASE_URL) | ||
| or host | ||
| or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com") | ||
| ) | ||
| ).rstrip("/") |
There was a problem hiding this comment.
🟡 (optional) If an app passes base_url or host as a URL object instead of a str, Langfuse() now raises AttributeError at construction. Examples are yarl.URL or pydantic HttpUrl from a settings model. Startup crashes even when keys are missing. .rstrip("/") at client.py:339 runs before the public_key check. On the base branch the value only reached f-strings, which accept any object. Fix: coerce before stripping, e.g. str(...).rstrip("/"), and do the same at span_processor.py:202 so direct LangfuseSpanProcessor callers keep working.
Why this was flagged
This is conditional: I could not rule out callers that pass a non-str URL object, which is common with pydantic-settings or yarl config. That value goes to Langfuse(base_url=...) or Langfuse(host=...). The annotation Optional[str] (client.py:312-313) is not enforced at runtime. The new .rstrip("/") at client.py:339 raises AttributeError for objects without that method, which ends Langfuse.init. It runs before the public_key/secret_key checks at client.py:376-392, so even a client that would otherwise just be disabled now raises. On the base branch, base_url was only used inside f-strings: span_processor.py:204-206, client.py:2541 and 2938, and urljoin(f"{base_url}/", path) in langfuse/api/core/http_client.py:310. All of these stringify any object, so a yarl.URL without a trailing slash worked fully. span_processor.py:202 has the same exposure for users who build LangfuseSpanProcessor themselves.
Verification: The diff changes client.py:334-339 to call .rstrip("/") on the selected value. A truthy URL object is selected first, and .rstrip then raises AttributeError. This line runs before the public_key is None early return at client.py:376-383, so even a client that would otherwise just be disabled now fails in __init__. So construction succeeded on base.
| base_url = base_url.rstrip("/") | ||
| endpoint = ( | ||
| f"{base_url}/{traces_export_path}" |
There was a problem hiding this comment.
🟣 pre-existing, not blocking: pre-existing: Users who set LANGFUSE_OTEL_TRACES_EXPORT_PATH to the documented form "/api/public/otel/v1/traces" still get a double-slash endpoint, which hits the same 308 and loses spans. The base URL is now stripped, but span_processor.py:204 still joins it as f"{base_url}/{traces_export_path}" without removing a leading slash. The documented default in environment_variables.py:62 has one. Fix: normalize both sides of the join, for example f"{base_url}/{traces_export_path.lstrip('/')}", and add a parametrized case for a leading-slash export path.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
The trigger is LANGFUSE_OTEL_TRACES_EXPORT_PATH set to a value with a leading slash. langfuse/_client/environment_variables.py:58-62 documents this variable as "URL path on the configured host" with default /api/public/otel/v1/traces, so users copying that form will set one. LangfuseSpanProcessor.init reads it at span_processor.py:200. Line 202 strips only the base URL, and line 204 builds "http://localhost:3000//api/public/otel/v1/traces". That is the exact double-slash URL the PR says the server answers with a 308 redirect, losing spans. The base branch fails the same way, so this is a sibling path the partial fix leaves broken. Neither the client.py:339 rstrip nor the new tests (tests/unit/test_initialization.py, tests/unit/test_span_processor.py) touch the export-path side of the join.
Verification: Pre-existing. In langfuse/_client/span_processor.py, line 202 (new) only does base_url = base_url.rstrip("/"). Line 204 then joins with f"{base_url}/{traces_export_path}", so the documented path gives http://localhost:3000//api/public/otel/v1/traces. On the base branch the same env value already gave this exact URL. No test in the repo uses this variable.
A
base_urlwith a trailing slash (e.g.http://localhost:3000/) producedhttp://localhost:3000//api/public/otel/v1/traces. The server answers that with a 308 redirect, and the spans are lost.Changes:
Langfuse.__init__strips trailing slashes from the resolved base URL, whichever source it comes from: thebase_urlorhostargument, orLANGFUSE_BASE_URL/LANGFUSE_HOST. That one value feeds the OTLP exporter endpoint, the sync and async API clients, and the trace and experiment UI URLs.LangfuseSpanProcessorstrips the slash too, because users can construct it directly.Tests: the old
test_trailing_slash_handlingasserted the slash was kept, so I replaced it with a test parametrized over all four sources. It checks_base_url, both API client base URLs and the exporter endpoint. A second test coversLangfuseSpanProcessoron its own. Both fail without the fix.Verification:
uv run --frozen pytest -n auto tests/unit: 1041 passed, 2 skippeduv run --frozen ruff check .,ruff format, andmypy langfuse: clean

Confidence Score: 5/5
This PR appears safe to merge.Summary
Strips trailing slashes from the resolved
base_urlbefore creating clients and trace links.Reviews (1) · Last reviewed commit: "fix(client): strip trailing slashes from..." · Reviewed by Greptile