Skip to content

Commit 53e4e96

Browse files
committed
fix: raise LangfuseAuthCheckError instead of bare Exception in auth_check()
auth_check() previously raised a bare Exception when no project was found for the provided credentials, which is easy to swallow with a broad `except Exception` at the call site and hides real bugs (issue #906).
1 parent 11a232d commit 53e4e96

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

langfuse/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@
9696
)
9797

9898
Langfuse = _client_module.Langfuse
99+
LangfuseAuthCheckError = _client_module.LangfuseAuthCheckError
99100

100101
__all__ = [
101102
"Langfuse",
103+
"LangfuseAuthCheckError",
102104
"LangfuseMedia",
103105
"LangfuseMediaReference",
104106
"get_client",

langfuse/_client/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ def _serialize_evaluations(evaluations: List[Evaluation]) -> List[Dict[str, Any]
176176
]
177177

178178

179+
class LangfuseAuthCheckError(Exception):
180+
"""Raised by `Langfuse.auth_check()` when no project is found for the provided credentials."""
181+
182+
179183
class Langfuse:
180184
"""Main client for Langfuse tracing and platform features.
181185
@@ -3474,7 +3478,7 @@ def auth_check(self) -> bool:
34743478
"""Check if the provided credentials (public and secret key) are valid.
34753479
34763480
Raises:
3477-
Exception: If no projects were found for the provided credentials.
3481+
LangfuseAuthCheckError: If no projects were found for the provided credentials.
34783482
34793483
Note:
34803484
This method is blocking. It is discouraged to use it in production code.
@@ -3485,9 +3489,10 @@ def auth_check(self) -> bool:
34853489
f"Auth check successful, found {len(projects.data)} projects"
34863490
)
34873491
if len(projects.data) == 0:
3488-
raise Exception(
3492+
no_project_message = (
34893493
"Auth check failed, no project found for the keys provided."
34903494
)
3495+
raise LangfuseAuthCheckError(no_project_message)
34913496
return True
34923497

34933498
except AttributeError as e:

tests/unit/test_auth_check.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Tests for Langfuse.auth_check() error behavior.
2+
3+
See https://github.com/langfuse/langfuse/issues/906 - auth_check() used to
4+
raise a bare `Exception` when no project was found for the provided
5+
credentials, which is easy to swallow with a broad `except Exception` at the
6+
call site.
7+
"""
8+
9+
from unittest.mock import Mock
10+
11+
import pytest
12+
13+
from langfuse import Langfuse, LangfuseAuthCheckError
14+
from langfuse._client.resource_manager import LangfuseResourceManager
15+
16+
17+
@pytest.fixture
18+
def langfuse():
19+
langfuse_instance = Langfuse()
20+
21+
if langfuse_instance._resources is None:
22+
langfuse_instance._resources = Mock(spec=LangfuseResourceManager)
23+
24+
langfuse_instance.api = Mock()
25+
26+
return langfuse_instance
27+
28+
29+
def test_auth_check_raises_langfuse_auth_check_error_when_no_projects(langfuse):
30+
langfuse.api.projects.get.return_value = Mock(data=[])
31+
32+
with pytest.raises(LangfuseAuthCheckError):
33+
langfuse.auth_check()
34+
35+
36+
def test_auth_check_returns_true_when_projects_found(langfuse):
37+
langfuse.api.projects.get.return_value = Mock(data=[Mock()])
38+
39+
assert langfuse.auth_check() is True

0 commit comments

Comments
 (0)