fix(langchain): mark handled tool errors as errors#1718
Merged
hassiebp merged 1 commit intoJun 22, 2026
Conversation
Contributor
Author
|
I signed the CLA. The GitHub Actions workflows are awaiting maintainer approval. |
3d5b967 to
19f8284
Compare
hassiebp
approved these changes
Jun 22, 2026
Collaborator
|
Thanks for your contribution, @vismaytiwari ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes handled LangChain tool errors being recorded as successful tool observations.
When a LangChain tool is configured with
handle_tool_error=True, LangChain can callon_tool_endinstead ofon_tool_error, while still passing aToolMessagewithstatus="error". The callback handler previously ended the observation without checking that status, so Langfuse did not mark the observation as an error.This updates
on_tool_endto inspectToolMessage.statusand set the observation level toERRORwhen the message status is"error". It also stores the handled error content as the observation status message.Fixes langfuse/langfuse#12867
Type of change
Verification
List the main commands you ran:
uv run --frozen pytest tests/unit/test_langchain.py::test_handled_tool_error_marks_observation_error uv run --frozen pytest tests/unit/test_langchain.py uv run --frozen ruff check . uv run --frozen mypy langfuse --no-error-summary uv run --frozen pytest -n auto --dist worksteal tests/unitI also reproduced the issue locally before the fix. Before this change, a handled tool error with
ToolMessage(status="error")had no observation level or status message. After the change, the same path recordslevel=ERRORandstatus_messagefrom the handled tool error content.Checklist
code_review.md..env.templateif needed — N/A, this is covered by the LangChain callback unit test and does not change documented setup or examples.Greptile Summary
This PR fixes a bug where LangChain tool errors handled via
handle_tool_error=Truewere silently recorded as successful observations. LangChain routes these throughon_tool_endwith aToolMessage(status="error")rather than callingon_tool_error, so the previous code never set an error level.on_tool_endnow widens itsoutputparameter fromstrtoAnyand inspects the value: if it is aToolMessagewithstatus="error", the observation is updated withlevel="ERROR"andstatus_messageset from the message content.on_tool_start→on_tool_end(ToolMessage(status="error"))and asserts the resulting span has the correct level, status message, and preserved output fields.Confidence Score: 4/5
The change is a small, well-targeted fix with a direct unit test. It does not alter any existing code paths — only the new ToolMessage branch is affected.
The fix is correct and the test covers the new path end-to-end. The only open question is whether cost_details should be zeroed in the new error branch to match on_tool_error behavior; if it should, users could see non-zero costs attributed to errored tool spans until that is addressed.
langfuse/langchain/CallbackHandler.py — specifically whether the new error branch in on_tool_end should zero out cost_details to match on_tool_error
Sequence Diagram
%%{init: {'theme': 'neutral'}}%% sequenceDiagram participant LC as LangChain participant CB as CallbackHandler participant OB as LangfuseObservation Note over LC,OB: Normal tool success path LC->>CB: on_tool_start(tool_name, input) CB->>OB: create tool observation LC->>CB: on_tool_end(output: str) CB->>OB: "update(output=output).end()" Note over LC,OB: Handled tool error path (handle_tool_error=True) NEW LC->>CB: on_tool_start(tool_name, input) CB->>OB: create tool observation LC->>CB: "on_tool_end(ToolMessage(status=error, content=...))" CB->>CB: "check isinstance(output, ToolMessage) AND status==error" CB->>OB: "update(output=msg, level=ERROR, status_message=msg.content).end()" Note over LC,OB: Unhandled tool error path (pre-existing) LC->>CB: on_tool_start(tool_name, input) CB->>OB: create tool observation LC->>CB: on_tool_error(exception) CB->>OB: "update(level=..., status_message=...).end()"%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% sequenceDiagram participant LC as LangChain participant CB as CallbackHandler participant OB as LangfuseObservation Note over LC,OB: Normal tool success path LC->>CB: on_tool_start(tool_name, input) CB->>OB: create tool observation LC->>CB: on_tool_end(output: str) CB->>OB: "update(output=output).end()" Note over LC,OB: Handled tool error path (handle_tool_error=True) NEW LC->>CB: on_tool_start(tool_name, input) CB->>OB: create tool observation LC->>CB: "on_tool_end(ToolMessage(status=error, content=...))" CB->>CB: "check isinstance(output, ToolMessage) AND status==error" CB->>OB: "update(output=msg, level=ERROR, status_message=msg.content).end()" Note over LC,OB: Unhandled tool error path (pre-existing) LC->>CB: on_tool_start(tool_name, input) CB->>OB: create tool observation LC->>CB: on_tool_error(exception) CB->>OB: "update(level=..., status_message=...).end()"Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(langchain): mark handled tool errors..." | Re-trigger Greptile