Can not get ToolMessage in astream_events if excepiton occures in tool call #945

Closed
opened 2026-02-20 17:42:28 -05:00 by yindo · 2 comments
Owner

Originally created by @781574155 on GitHub (Aug 26, 2025).

Originally assigned to: @casparb on GitHub.

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

async for event in graph.astream_events(input, config=config):
        kind = event["event"]
        if kind == "on_tool_end":
            output = event['data']['output']
            pass

Error Message and Stack Trace (if applicable)


Description

In on_tool_end event, I can get ToolMessage on tool call finish.
However if exception occures, there are an tool message to send to LLM, but I can't get it.

Image

I think we should be able to get ToolMessage on on_tool_end event even exception occures in tool call.

System Info

no

Originally created by @781574155 on GitHub (Aug 26, 2025). Originally assigned to: @casparb on GitHub. ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/). - [x] I added a clear and detailed title that summarizes the issue. - [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python async for event in graph.astream_events(input, config=config): kind = event["event"] if kind == "on_tool_end": output = event['data']['output'] pass ``` ### Error Message and Stack Trace (if applicable) ```shell ``` ### Description In `on_tool_end` event, I can get ToolMessage on tool call finish. However if exception occures, there are an tool message to send to LLM, but I can't get it. <img width="1559" height="998" alt="Image" src="https://github.com/user-attachments/assets/a0a70978-d15e-45e8-ba65-59df98d1f503" /> I think we should be able to get ToolMessage on `on_tool_end` event even exception occures in tool call. ### System Info no
yindo added the bugpending labels 2026-02-20 17:42:28 -05:00
yindo closed this issue 2026-02-20 17:42:28 -05:00
Author
Owner

@casparb commented on GitHub (Sep 11, 2025):

Hi @781574155. It is not obvious what you are asking because your code example does not clearly demonstrate the issue, so I'm sorry if I misunderstand:

It seems like you want to capture the ToolMessage that contains the error while streaming. You can use the .astream() API - you'll see the following event if a tool errors:

{'tools': {'messages': [ToolMessage(content='Error: ValueError("...")\n Please fix your mistakes.', ..., status='error')]}}

You should be favoring the .stream() or .astream() APIs instead of.astream_events() API, which is largely unnecessary these days for development. There is also a known bug in core (langchain-ai/langchain#30708) where on_tool_error does not appear in the .astream_events() events stream. If you must use .astream_events() over .astream(), then keep an eye on that PR - a fix is coming soon.

Closing as resolved, reply here if you're still having issues (with a clear, self-contained, minimal example) and I'll reopen.

@casparb commented on GitHub (Sep 11, 2025): Hi @781574155. It is not obvious what you are asking because your code example does not clearly demonstrate the issue, so I'm sorry if I misunderstand: It seems like you want to capture the ToolMessage that contains the error while streaming. You can use the [`.astream()` API](https://docs.langchain.com/oss/python/langgraph/streaming) - you'll see the following event if a tool errors: ```python {'tools': {'messages': [ToolMessage(content='Error: ValueError("...")\n Please fix your mistakes.', ..., status='error')]}} ``` You should be favoring the `.stream()` or `.astream()` APIs instead of`.astream_events()` API, which is largely unnecessary these days for development. There is also a known bug in core (langchain-ai/langchain#30708) where `on_tool_error` does not appear in the `.astream_events()` events stream. If you must use `.astream_events()` over `.astream()`, then keep an eye on that PR - a fix is coming soon. Closing as resolved, reply here if you're still having issues (with a clear, self-contained, minimal example) and I'll reopen.
Author
Owner

@781574155 commented on GitHub (Sep 11, 2025):

Thank you @casparb. https://github.com/langchain-ai/langchain/issues/30708 is what I want to express

@781574155 commented on GitHub (Sep 11, 2025): Thank you @casparb. https://github.com/langchain-ai/langchain/issues/30708 is what I want to express
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#945