Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions langfuse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@
)

Langfuse = _client_module.Langfuse
LangfuseAuthCheckError = _client_module.LangfuseAuthCheckError

__all__ = [
"Langfuse",
"LangfuseAuthCheckError",
"LangfuseMedia",
"LangfuseMediaReference",
"get_client",
Expand Down
9 changes: 7 additions & 2 deletions langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def _serialize_evaluations(evaluations: List[Evaluation]) -> List[Dict[str, Any]
]


class LangfuseAuthCheckError(Exception):
"""Raised by `Langfuse.auth_check()` when no project is found for the provided credentials."""


class Langfuse:
"""Main client for Langfuse tracing and platform features.

Expand Down Expand Up @@ -3474,7 +3478,7 @@ def auth_check(self) -> bool:
"""Check if the provided credentials (public and secret key) are valid.

Raises:
Exception: If no projects were found for the provided credentials.
LangfuseAuthCheckError: If no projects were found for the provided credentials.

Note:
This method is blocking. It is discouraged to use it in production code.
Expand All @@ -3485,9 +3489,10 @@ def auth_check(self) -> bool:
f"Auth check successful, found {len(projects.data)} projects"
)
if len(projects.data) == 0:
raise Exception(
no_project_message = (
"Auth check failed, no project found for the keys provided."
)
raise LangfuseAuthCheckError(no_project_message)
return True

except AttributeError as e:
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/test_auth_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Tests for Langfuse.auth_check() error behavior.

See https://github.com/langfuse/langfuse/issues/906 - auth_check() used to
raise 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.
"""

from unittest.mock import Mock

import pytest

from langfuse import Langfuse, LangfuseAuthCheckError
from langfuse._client.resource_manager import LangfuseResourceManager


@pytest.fixture
def langfuse():
langfuse_instance = Langfuse()

if langfuse_instance._resources is None:
langfuse_instance._resources = Mock(spec=LangfuseResourceManager)

langfuse_instance.api = Mock()

return langfuse_instance


def test_auth_check_raises_langfuse_auth_check_error_when_no_projects(langfuse):
langfuse.api.projects.get.return_value = Mock(data=[])

with pytest.raises(LangfuseAuthCheckError):
langfuse.auth_check()


def test_auth_check_returns_true_when_projects_found(langfuse):
langfuse.api.projects.get.return_value = Mock(data=[Mock()])

assert langfuse.auth_check() is True