Skip to content

Conversation

@jdidzik
Copy link

@jdidzik jdidzik commented Jan 19, 2026

We noticed that fetching a trace_url for recording within an existing span would throw an exception re the api attribute not existing if Langfuse tracing was disabled (all of our unit testing).

I've added a guard here to return None early before attempting to get the project_id, trace_id, etc, ultimately avoiding that exception.

On our end, its a bit annoying to guard: if span._langfuse_client._tracing_enabled:, so I figure its worth guarding on this end.

I've added a test verifying that exact behavior.


Important

Add guard clause in get_trace_url() to return None when tracing is disabled, preventing exceptions, and add a test for this behavior.

  • Behavior:
    • Add guard clause in get_trace_url() in client.py to return None if _tracing_enabled is False, preventing exceptions when tracing is disabled.
  • Tests:
    • Add test_generate_trace_url_client_disabled() in test_core_sdk.py to verify get_trace_url() returns None when tracing is disabled.

This description was created by Ellipsis for 3bd86a5. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Summary

This PR adds a defensive guard to the get_trace_url() method to return None early when tracing is disabled, preventing potential downstream issues.

The fix:

  • Adds if not self._tracing_enabled: return None check at the beginning of get_trace_url() in langfuse/_client/client.py:2432-2433
  • Follows the established pattern used by similar methods (get_current_trace_id(), get_current_observation_id())
  • Includes a test case verifying the behavior when tracing_enabled=False

The change is minimal, semantically correct, and improves the robustness of the API when tracing is disabled.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The change is a simple defensive guard that follows established patterns in the codebase. The fix is minimal (3 lines), well-tested, and semantically correct. The return type already supports None, and the docstring accommodates this case.
  • No files require special attention

Important Files Changed

Filename Overview
langfuse/_client/client.py Added early return guard to prevent issues when tracing is disabled
tests/test_core_sdk.py Added test coverage for get_trace_url() when tracing is disabled

Sequence Diagram

sequenceDiagram
    participant User
    participant Langfuse
    participant get_trace_url
    participant _get_project_id
    participant get_current_trace_id
    participant API

    User->>Langfuse: get_trace_url(trace_id=None)
    Langfuse->>get_trace_url: call method
    
    alt tracing_enabled is False
        get_trace_url-->>Langfuse: return None (early exit)
        Langfuse-->>User: None
    else tracing_enabled is True
        get_trace_url->>_get_project_id: fetch project ID
        _get_project_id->>API: api.projects.get()
        API-->>_get_project_id: project data
        _get_project_id-->>get_trace_url: project_id
        
        get_trace_url->>get_current_trace_id: get current trace
        get_current_trace_id-->>get_trace_url: trace_id
        
        alt project_id and trace_id exist
            get_trace_url-->>Langfuse: formatted URL
            Langfuse-->>User: trace URL string
        else missing project_id or trace_id
            get_trace_url-->>Langfuse: None
            Langfuse-->>User: None
        end
    end
Loading

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 19, 2026

Greptile found no issues!

From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

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.

1 participant