Skip to content

fix(sdk): anchor /api suffix strip to path, not raw URL string - #7009

Closed
naikvaib wants to merge 1 commit into
Agenta-AI:mainfrom
naikvaib:fix/sdk-trace-endpoint-host-collision
Closed

naikvaib wants to merge 1 commit into
Agenta-AI:mainfrom
naikvaib:fix/sdk-trace-endpoint-host-collision

Conversation

@naikvaib

Copy link
Copy Markdown

Fixes #6787.

Root cause: sdks/python/agenta/sdk/utils/init.py did _api_url.rsplit("/api", 1)[0] on the raw URL string, unanchored to a path segment. For a host literally named api (e.g. http://api:8000), the substring /api matches inside http://api:8000 itself (the // + api), splitting it into http:/ + :8000 — losing the port and corrupting the host. The trace endpoint became http://api/otlp/v1/traces instead of http://api:8000/api/otlp/v1/traces.

Fix: new strip_api_suffix(url) helper in utils/helpers.py using urllib.parse.urlsplit/urlunsplit — only strips a trailing /api segment from the path component (checked via path.endswith("/api"), naturally segment-anchored), leaving scheme/host/port untouched. init.py now calls this instead of the raw rsplit.

Added regression tests in sdks/python/oss/tests/pytest/unit/test_trace_endpoint_url.py: 9 parametrized cases on strip_api_suffix directly (including the exact http://api:8000 repro, host+suffix combos, and a non-anchored /myapi case that must NOT be stripped), plus 4 cases reproducing the full host→trace-endpoint derivation from init.py.

Verified: 17/17 tests pass (uv run pytest). ruff format/ruff check clean on all three changed files.

AgentaSingleton.init() derived self.host from AGENTA_API_URL /
AGENTA_API_INTERNAL_URL using the unanchored `_api_url.rsplit("/api", 1)[0]`.
This matched the literal substring "/api" anywhere in the URL, including
inside the host itself. For an internal base URL whose host is literally
"api" (e.g. "http://api:8000"), this corrupted the host - dropping the port -
and broke the OTLP trace endpoint built from it.

Add strip_api_suffix() in sdk/utils/helpers.py, which uses
urllib.parse.urlsplit/urlunsplit to only ever trim a trailing "/api"
segment from the URL's path component, leaving scheme/host/port untouched
regardless of what the host is named. init.py now calls this helper instead
of the raw string split.

Add regression tests covering a host literally named "api", a host
containing "api" as a substring with and without a trailing /api segment,
a host with a port, and existing normal cases (with/without trailing /api).

Fixes Agenta-AI#6787
@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mmabrouk

Copy link
Copy Markdown
Member

🤖 The AI agent says: Thank you, Vaibhav Naik, for fixing how the /api suffix is stripped for issue #6787. PR #6806 fixes the same issue and is further along in review, so we are closing this one in favor of #6806. You are welcome to contribute again.

@mmabrouk mmabrouk closed this Oct 23, 2026
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.

[SDK] Unanchored "/api" split in init.py breaks the trace endpoint for a host named api

3 participants