Skip to content

fix(client): strip trailing slashes from base_url - #2152

Merged
hassiebp merged 1 commit into
mainfrom
hassiebbot/strip-base-url-trailing-slash-e712
Nov 24, 2026
Merged

hassiebp merged 1 commit into
mainfrom
hassiebbot/strip-base-url-trailing-slash-e712

Conversation

@hassiebp

@hassiebp hassiebp commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

A base_url with a trailing slash (e.g. http://localhost:3000/) produced http://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: the base_url or host argument, or LANGFUSE_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.
  • LangfuseSpanProcessor strips the slash too, because users can construct it directly.

Tests: the old test_trailing_slash_handling asserted 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 covers LangfuseSpanProcessor on its own. Both fail without the fix.

Verification:

  • uv run --frozen pytest -n auto tests/unit: 1041 passed, 2 skipped
  • uv run --frozen ruff check ., ruff format, and mypy langfuse: clean
Open in Web Open in Cursor 

RetriggerView in GreptileConfidence Score: 5/5

This PR appears safe to merge.

Summary

Strips trailing slashes from the resolved base_url before creating clients and trace links.

  • Langfuse removes trailing slashes before building API and trace URLs.

Reviews (1) · Last reviewed commit: "fix(client): strip trailing slashes from..." · Reviewed by Greptile

Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T07:55:15.342599Z 95bac2a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment on lines 336 to +339
or os.environ.get(LANGFUSE_BASE_URL)
or host
or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
)
).rstrip("/")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (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.

Comment on lines +202 to 204
base_url = base_url.rstrip("/")
endpoint = (
f"{base_url}/{traces_export_path}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟣 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.

@hassiebp
hassiebp enabled auto-merge (squash) November 24, 2026 07:57
@hassiebp
hassiebp disabled auto-merge November 24, 2026 08:24
@hassiebp
hassiebp merged commit 2232ea3 into main Nov 24, 2026
26 checks passed
@hassiebp
hassiebp deleted the hassiebbot/strip-base-url-trailing-slash-e712 branch November 24, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants