Skip to content

Commit 04590cb

Browse files
feat(python-sdk): introduce LangfuseAuthException for auth_check failures
Bare Exception raises are bad practice in Python because they cannot be cleanly caught without also masking unrelated bugs (ImportError, NameError, SyntaxError, etc.). This commit introduces LangfuseAuthException(Exception), a dedicated exception class for authentication failures, and replaces the bare raise Exception(...) in Langfuse.auth_check() with it. Users can now guard specifically against auth failures: from langfuse import Langfuse, LangfuseAuthException try: Langfuse().auth_check() except LangfuseAuthException as e: logger.warning('Langfuse auth failed: %s', e) LangfuseAuthException is exported from the top-level langfuse package. Closes langfuse/langfuse#906 Co-authored-by: nik464 <nik464@users.noreply.github.com>
1 parent 8bcc8fa commit 04590cb

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

langfuse/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
LangfuseTool,
3030
)
3131
from ._version import __version__
32+
from ._utils.request import LangfuseAuthException
3233
from .span_filter import (
3334
KNOWN_LLM_INSTRUMENTATION_SCOPE_PREFIXES,
3435
is_default_export_span,
@@ -66,6 +67,7 @@
6667
"RunnerContext",
6768
"RegressionError",
6869
"__version__",
70+
"LangfuseAuthException",
6971
"is_default_export_span",
7072
"is_langfuse_span",
7173
"is_genai_span",

langfuse/_client/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
from langfuse._utils.environment import get_common_release_envs
8787
from langfuse._utils.parse_error import handle_fern_exception
8888
from langfuse._utils.prompt_cache import PromptCache
89+
from langfuse._utils.request import LangfuseAuthException
8990
from langfuse.api import (
9091
CreateChatPromptRequest,
9192
CreateChatPromptType,
@@ -3166,7 +3167,7 @@ def auth_check(self) -> bool:
31663167
"""Check if the provided credentials (public and secret key) are valid.
31673168
31683169
Raises:
3169-
Exception: If no projects were found for the provided credentials.
3170+
LangfuseAuthException: If no projects were found for the provided credentials.
31703171
31713172
Note:
31723173
This method is blocking. It is discouraged to use it in production code.
@@ -3177,7 +3178,7 @@ def auth_check(self) -> bool:
31773178
f"Auth check successful, found {len(projects.data)} projects"
31783179
)
31793180
if len(projects.data) == 0:
3180-
raise Exception(
3181+
raise LangfuseAuthException(
31813182
"Auth check failed, no project found for the keys provided."
31823183
)
31833184
return True

langfuse/_utils/request.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,25 @@ def __str__(self) -> str:
134134
errors = ", ".join(str(error) for error in self.errors)
135135

136136
return f"[Langfuse] {errors}"
137+
138+
139+
class LangfuseAuthException(Exception):
140+
"""Raised when Langfuse authentication fails.
141+
142+
This exception is raised when the provided credentials (public key and
143+
secret key) are valid but no projects are accessible with them. Catching
144+
this specific exception type allows callers to distinguish authentication
145+
failures from other errors and handle them explicitly.
146+
147+
Example::
148+
149+
from langfuse import Langfuse, LangfuseAuthException
150+
151+
try:
152+
Langfuse().auth_check()
153+
except LangfuseAuthException as e:
154+
# Handle auth failure cleanly without masking unrelated bugs
155+
logger.warning("Langfuse auth failed: %s", e)
156+
"""
157+
158+
pass

0 commit comments

Comments
 (0)