Skip to content

fix(langchain): deregister langfuse prompts when a chain run errors - #2030

Open
anishgurjar wants to merge 4 commits into
langfuse:mainfrom
anishgurjar:fix-langchain-prompt-deregister
Open

anishgurjar wants to merge 4 commits into
langfuse:mainfrom
anishgurjar:fix-langchain-prompt-deregister

Conversation

@anishgurjar

@anishgurjar anishgurjar commented Oct 20, 2026 •

Copy link
Copy Markdown

What does this PR do?

on_chain_end deregisters the run's entry in _prompt_to_parent_run_map, but on_chain_error does not. A chain run that errors after a Langfuse prompt was registered for it - a nested run that inherited a registration, or a run whose child prompt chain registered one keyed to it - leaks the entry permanently. In a service that retries failing chains, the map grows without bound.

on_chain_error now deregisters the run in its finally block, so the cleanup runs even when the error update itself raises. This matches the deregistration on_chain_end performs on the success path. Same error-path cleanup class as #1978.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Refactor
  • Documentation update
  • Tooling, CI, or repo maintenance

Verification

uv run --frozen pytest tests/unit/test_langchain.py -q   # 21 passed, 5 skipped
uv run --frozen pytest -n auto --dist worksteal tests/unit   # 928 passed, 7 skipped (test_prompt.py errors pre-existing, see #1976)
uv run --frozen ruff check langfuse/langchain/CallbackHandler.py tests/unit/test_langchain.py
uv run --frozen ruff format --check langfuse/langchain/CallbackHandler.py tests/unit/test_langchain.py
uv run --frozen mypy langfuse --no-error-summary   # only pre-existing langgraph stub errors (optional dep)

Both new tests fail on main (the registration survives on_chain_error) and pass with this change.

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. (not 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.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Removes a failed chain’s prompt registration in on_chain_error, including when updating the observation raises.

  • Failed LangChain chains now clear their own prompt registrations.

Reviews (1) · Last reviewed commit: "style: trim trailing blank line for ruff..." · Reviewed by Greptile

@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.

@anishgurjar

Copy link
Copy Markdown
Author

@hassiebp This is the same error-path cleanup class as #1978, one level up in the prompt-linkage map. Both regression tests fail on main and pass here. Happy to adjust if you would rather see the deregistration shared between the end and error paths.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
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.

2 participants