Skip to content

fix: raise LangfuseAuthCheckError instead of bare Exception in auth_check()#1771

Open
mittalpk wants to merge 1 commit into
langfuse:mainfrom
mittalpk:fix/auth-check-bare-exception
Open

fix: raise LangfuseAuthCheckError instead of bare Exception in auth_check()#1771
mittalpk wants to merge 1 commit into
langfuse:mainfrom
mittalpk:fix/auth-check-bare-exception

Conversation

@mittalpk

@mittalpk mittalpk commented Jul 21, 2026

Copy link
Copy Markdown

Fixes #906

What does this PR do?

Langfuse.auth_check() raised a bare Exception when no project was found for the provided credentials. A bare Exception is easy to swallow accidentally with a broad except Exception: at the call site (a common defensive-coding pattern in application code), which hides a genuine "your API keys don't correspond to a project" failure instead of letting the caller distinguish it from other errors — exactly the scenario described in #906.

  • Added LangfuseAuthCheckError(Exception) in langfuse/_client/client.py, exported it publicly in langfuse/__init__.py (following the existing precedent of RegressionError from experiment.py).
  • auth_check() now raises LangfuseAuthCheckError instead of a bare Exception in the "no project found" case; updated the docstring accordingly.
  • This is purely additive/backwards-compatible: LangfuseAuthCheckError subclasses Exception directly, so any existing except Exception: call site still catches it.

Type of change

  • Bug fix

Verification

uv run --frozen ruff check .
uv run --frozen ruff format --check .
uv run --frozen mypy langfuse --no-error-summary
uv run --frozen pytest -n auto --dist worksteal tests/unit

All clean; full unit suite passing with no regressions. Added tests/unit/test_auth_check.py covering both the new exception being raised when no projects are found, and the happy path being unaffected. Confirmed the new test fails against the pre-fix code (ImportError: cannot import name 'LangfuseAuthCheckError') before the fix is applied.

Checklist

  • I self-reviewed the diff using code_review.md.
  • I added or updated tests for behavior changes.
  • I updated docs, examples, or .env.template if needed. (docstring updated; no example/.env.template changes needed)
  • I did not hand-edit generated files; if generated files changed, I used the upstream regeneration path.
  • I did not commit secrets or credentials.

Greptile Summary

This PR introduces a specific error for failed authentication checks. The main changes are:

  • Adds and publicly exports LangfuseAuthCheckError.
  • Raises the new error when credentials return no projects.
  • Adds tests for empty and non-empty project responses.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Reviews (1): Last reviewed commit: "fix: raise LangfuseAuthCheckError instea..." | Re-trigger Greptile

Context used:

  • Rule used - Move imports to the top of the module instead of p... (source)

Learned From
langfuse/langfuse-python#1387

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

…heck()

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 langfuse#906).
@mittalpk
mittalpk force-pushed the fix/auth-check-bare-exception branch from ddf722c to 53e4e96 Compare July 21, 2026 22:41
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