feat(python-sdk): Introduce custom error types instead of bare exception
Author: norton120Created Jan 17, 2024Updated Sep 29, 2026
Labelsprio-lowprio-mediumsdk-pythonstale
Describe the bug
Exceptions are used as part of flow control in Python (EAFP). Bare exceptions (raising Exception directly) are considered bad practice because they can't be cleanly caught and handled, and make it really easy to ship broken code.
With a bare exception lots of bugs can hide:
- langfuse package (or dependency) didn't import
- name error
langfuz.auth_check() - syntax error
langfuse.auth_check(] - value error
langfuse.auth_check(1) - attribute error
langchain.auth_check()
... the list goes on
Maybe as a user you decide against include LF in testing, so you do something like this:
def initialize():
langfuse.auth_check(:w # oops! Vim user strikes again!
try:
initialize()
except Exception as e:
if environment.is_non_prod:
logger.info('langfuse not started because development')
raise e
# catches silently :( Aren't you surprised when you merge code that passes all the tests, passes CI, and then you app fails to start in production because the LF exception crashed it? That's why we don't use bare Exceptions. Now with a scoped exception:
def initialize():
langfuse.auth_check(:w # oops! Vim user strikes again!
try:
initialize()
except LangfuseAuthException as e:
if environment.is_non_prod:
logger.info('langfuse not started because development')
raise e
# Raises SyntaxError! you'll (appropriately) fail on your local. Crashed deploy averted.
To reproduce
Catch bare Exception.
Additional information
No response
Source: langfuse/langfuse