Skip to content

Commit

Permalink
feat: use projectid when generating trace urls (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeichmann authored Dec 5, 2024
1 parent 916412a commit 17a2f4e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion langfuse/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class Langfuse(object):
host: str
"""Host of Langfuse API."""

project_id: Optional[str]
"""Project ID of the Langfuse project associated with the API keys provided."""

def __init__(
self,
public_key: Optional[str] = None,
Expand Down Expand Up @@ -311,6 +314,7 @@ def __init__(
self.task_manager = TaskManager(**args)

self.trace_id = None
self.project_id = None

self.release = self._get_release_value(release)

Expand All @@ -330,7 +334,14 @@ def get_trace_id(self) -> str:

def get_trace_url(self) -> str:
"""Get the URL of the current trace to view it in the Langfuse UI."""
return f"{self.base_url}/trace/{self.trace_id}"
if not self.project_id:
proj = self.client.projects.get()
if not proj.data or not proj.data[0].id:
return f"{self.base_url}/trace/{self.trace_id}"

self.project_id = proj.data[0].id

return f"{self.base_url}/project/{self.project_id}/traces/{self.trace_id}"

def get_dataset(
self, name: str, *, fetch_items_page_size: Optional[int] = 50
Expand Down
14 changes: 14 additions & 0 deletions tests/test_core_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,3 +1536,17 @@ def faulty_mask_func(data):
fetched_trace = api_wrapper.get_trace(trace.id)
assert fetched_trace["input"] == "<fully masked due to failed mask function>"
assert fetched_trace["output"] == "<fully masked due to failed mask function>"


def test_generate_trace_id():
langfuse = Langfuse(debug=False)
trace_id = create_uuid()

langfuse.trace(id=trace_id, name="test_trace")
langfuse.flush()

trace_url = langfuse.get_trace_url()
assert (
trace_url
== f"http://localhost:3000/project/7a88fb47-b4e2-43b8-a06c-a5ce950dc53a/traces/{trace_id}"
)

0 comments on commit 17a2f4e

Please sign in to comment.