[issue-7393] [SDK] fix: use direct project-scoped URL for dataset in get_or_create_dataset log - #7440
Conversation
… in get_or_create_dataset Resolves comet-ml#7393
| def get_dataset_url_by_id(dataset_id: str, base_url: str, workspace: str) -> str: | ||
| domain_root = get_base_url(base_url) | ||
| return f"{domain_root}opik/{workspace}/datasets/{dataset_id}/" |
There was a problem hiding this comment.
get_dataset_url_by_id has two issues: get_base_url() strips the path prefix down to netloc, so subpath installs like https://example.com/custom/api/ produce host-root links that 404 behind reverse-proxy deployments; and workspace/dataset_id are interpolated raw, so values with /, spaces, or ? produce malformed URLs — should we derive the UI root the same way as get_ui_url() and URL-encode both path segments?
Want Baz to fix this for you? Activate Fixer
Other fix methods
Prompt for AI Agents
Before applying, verify this suggestion against the current code. In
sdks/python/src/opik/url_helpers.py around lines 63-65, in
`get_dataset_url_by_id(dataset_id: str, base_url: str, workspace: str)`: 1. Stop using
`get_base_url(base_url)` because it drops path prefixes (everything after the domain).
Refactor to derive the UI root the same way as `get_ui_url()` and other UI URL helpers
(preserving any prefix before `/api`), then build the dataset path under that UI root so
it matches existing UI routing. 2. URL-quote `workspace` and `dataset_id` as individual
path segments (using the same allowed/safe character policy as the rest of the module)
before formatting them into the path, to prevent characters like `/`, spaces, or `?`
from producing malformed or misrouted links. Ensure the returned URL still ends with a
trailing slash and remains consistent with the module's existing quoting/encoding
approach.
| dataset_url = url_helpers.get_dataset_url_by_id( | ||
| dataset_id, self._config.url_override | ||
| dataset_id, self._config.url_override, self._workspace |
There was a problem hiding this comment.
Default workspace leaks into dataset URL
self._workspace is passed straight into get_dataset_url_by_id(), but OpikConfig.workspace can still be the "default" sentinel or blank since get_from_user_inputs() only strips None, so cloud users can log .../opik/default/datasets/<id>/ or .../opik//datasets/<id>/ instead of the real workspace URL — should we reuse the get_project_url() dereference via self._rest_client.check.get_workspace_name().workspace_name here?
Want Baz to fix this for you? Activate Fixer
Other fix methods
Prompt for AI Agents
Before applying, verify this suggestion against the current code. In
sdks/python/src/opik/api_objects/opik_client.py around lines 235-236 in the
_display_created_dataset_url method, don’t pass self._workspace verbatim into
url_helpers.get_dataset_url_by_id. Instead, reuse the same logic used by
get_project_url() to dereference the sentinel/default workspace to the actual workspace
slug via self._rest_client.check.get_workspace_name().workspace_name, and ensure
blank/empty workspace input is normalized so the URL path doesn’t contain double
slashes. Refactor to compute a single resolved workspace value once (e.g., a small
helper or local variable) and pass that resolved workspace into get_dataset_url_by_id
before logging the created dataset URL.
Details
get_dataset_url_by_idwas generating a session-redirect URL(
v1/session/redirect/datasets/?dataset_id=...) that routes throughthe user's session context and surfaces the dataset under "Default Project"
instead of the actual project it belongs to. This meant the URL logged by
create_dataset/get_or_create_datasetopened the wrong view in the UI.The fix replaces the session-redirect URL with the same direct URL pattern
used by
get_project_url_by_id:{domain_root}opik/{workspace}/datasets/{dataset_id}/The
workspaceis already available on the client asself._workspace.Change checklist
Issues
AI-WATERMARK
AI-WATERMARK: yes
Testing
Existing tests pass (no change to their expectations).
New test added:
test_get_dataset_url_by_id__returns_direct_project_scoped_urlvalidates 4 parameter combinations (localhost + comet.com, with and without
trailing slash on the base URL).
Run with:
Documentation
No documentation change required — this fixes a log message URL, not a
public API.