[GH-ISSUE #935] Save AIMessage object based on agent error type #120

Closed
opened 2026-02-17 17:19:13 -05:00 by yindo · 4 comments
Owner

Originally created by @dosnshcu on GitHub (Oct 15, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/935

When using create_react_agent, if an error occurs during execution, how can you save different AIMessage objects based on the error type and ensure successful execution next time?

Originally created by @dosnshcu on GitHub (Oct 15, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/935 When using `create_react_agent`, if an error occurs during execution, how can you save different `AIMessage` objects based on the error type and ensure successful execution next time?
yindo closed this issue 2026-02-17 17:19:13 -05:00
Author
Owner

@eyurtsev commented on GitHub (Oct 15, 2025):

Are you referring to an error in executing a tool?

@eyurtsev commented on GitHub (Oct 15, 2025): Are you referring to an error in executing a tool?
Author
Owner

@dosnshcu commented on GitHub (Oct 15, 2025):

yes,

@dosnshcu commented on GitHub (Oct 15, 2025): yes,
Author
Owner

@dosnshcu commented on GitHub (Oct 16, 2025):

[2025-10-16 14:31:02] ERROR basic_agent_service: Agent execution failed for session 1704384780671352818: Found AIMessages with tool_calls that do not have a corresponding ToolMessage. Here are the first few of those tool calls: [{'name': 'scrape_url', 'args': {'request': {'url': 'https://baike.baidu.com/item/%E6%9D%8E%E7%99%BD/1043'}}, 'id': 'call_0d250ee206dc432e9df0df', 'type': 'tool_call'}].
Every tool call (LLM requesting to call a tool) in the message history MUST have a corresponding ToolMessage (result of a tool invocation to return to the LLM) - this is required by most LLM providers.
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/INVALID_CHAT_HISTORY
Traceback (most recent call last):
File "D:\pythonProject\grt-ai\app\services\agents\basic_agent_service.py", line 181, in invoke_graph
async for chunks in graph.astream_events(
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1543, in astream_events
async for event in event_stream:
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 1032, in _astream_events_implementation_v2
await task
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 987, in consume_astream
async for _ in event_streamer.tap_output_aiter(run_id, stream):
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 190, in tap_output_aiter
first = await py_anext(output, default=sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\utils\aiter.py", line 78, in anext_impl
return await anext(iterator)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel\main.py", line 2976, in astream
async for _ in runner.atick(
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel_runner.py", line 401, in atick
_panic_or_proceed(
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel_runner.py", line 511, in _panic_or_proceed
raise exc
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel_retry.py", line 132, in arun_with_retry
async for _ in task.proc.astream(task.input, config):
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph_internal_runnable.py", line 840, in astream
output = await asyncio.create_task(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph_internal_runnable.py", line 905, in _consume_aiter
async for chunk in it:
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 190, in tap_output_aiter
first = await py_anext(output, default=sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\utils\aiter.py", line 78, in anext_impl
return await anext(iterator)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1616, in atransform
async for ichunk in input:
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1616, in atransform
async for ichunk in input:
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1176, in astream
yield await self.ainvoke(input, config, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph_internal_runnable.py", line 465, in ainvoke
ret = await asyncio.create_task(coro, context=context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\prebuilt\chat_agent_executor.py", line 647, in acall_model
model_input = _get_model_input_state(state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\prebuilt\chat_agent_executor.py", line 599, in _get_model_input_state
_validate_chat_history(messages)
File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\prebuilt\chat_agent_executor.py", line 245, in _validate_chat_history
raise ValueError(error_message)
ValueError: Found AIMessages with tool_calls that do not have a corresponding ToolMessage. Here are the first few of those tool calls: [{'name': 'scrape_url', 'args': {'request': {'url': 'https://baike.baidu.com/item/%E6%9D%8E%E7%99%BD/1043'}}, 'id': 'call_0d250ee206dc432e9df0df', 'type': 'tool_call'}].
Every tool call (LLM requesting to call a tool) in the message history MUST have a corresponding ToolMessage (result of a tool invocation to return to the LLM) - this is required by most LLM providers.
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/INVALID_CHAT_HISTORY
During task with name 'agent' and id '865b2742-7d72-add6-8500-98541855a6ef'

@dosnshcu commented on GitHub (Oct 16, 2025): [2025-10-16 14:31:02] ERROR basic_agent_service: Agent execution failed for session 1704384780671352818: Found AIMessages with tool_calls that do not have a corresponding ToolMessage. Here are the first few of those tool calls: [{'name': 'scrape_url', 'args': {'request': {'url': 'https://baike.baidu.com/item/%E6%9D%8E%E7%99%BD/1043'}}, 'id': 'call_0d250ee206dc432e9df0df', 'type': 'tool_call'}]. Every tool call (LLM requesting to call a tool) in the message history MUST have a corresponding ToolMessage (result of a tool invocation to return to the LLM) - this is required by most LLM providers. For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/INVALID_CHAT_HISTORY Traceback (most recent call last): File "D:\pythonProject\grt-ai\app\services\agents\basic_agent_service.py", line 181, in invoke_graph async for chunks in graph.astream_events( File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1543, in astream_events async for event in event_stream: File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 1032, in _astream_events_implementation_v2 await task File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 987, in consume_astream async for _ in event_streamer.tap_output_aiter(run_id, stream): File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 190, in tap_output_aiter first = await py_anext(output, default=sentinel) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\utils\aiter.py", line 78, in anext_impl return await __anext__(iterator) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel\main.py", line 2976, in astream async for _ in runner.atick( File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel\_runner.py", line 401, in atick _panic_or_proceed( File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel\_runner.py", line 511, in _panic_or_proceed raise exc File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\pregel\_retry.py", line 132, in arun_with_retry async for _ in task.proc.astream(task.input, config): File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\_internal\_runnable.py", line 840, in astream output = await asyncio.create_task( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\_internal\_runnable.py", line 905, in _consume_aiter async for chunk in it: File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\tracers\event_stream.py", line 190, in tap_output_aiter first = await py_anext(output, default=sentinel) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\utils\aiter.py", line 78, in anext_impl return await __anext__(iterator) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1616, in atransform async for ichunk in input: File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1616, in atransform async for ichunk in input: File "D:\Anaconda\envs\env_1009\Lib\site-packages\langchain_core\runnables\base.py", line 1176, in astream yield await self.ainvoke(input, config, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\_internal\_runnable.py", line 465, in ainvoke ret = await asyncio.create_task(coro, context=context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\prebuilt\chat_agent_executor.py", line 647, in acall_model model_input = _get_model_input_state(state) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\prebuilt\chat_agent_executor.py", line 599, in _get_model_input_state _validate_chat_history(messages) File "D:\Anaconda\envs\env_1009\Lib\site-packages\langgraph\prebuilt\chat_agent_executor.py", line 245, in _validate_chat_history raise ValueError(error_message) ValueError: Found AIMessages with tool_calls that do not have a corresponding ToolMessage. Here are the first few of those tool calls: [{'name': 'scrape_url', 'args': {'request': {'url': 'https://baike.baidu.com/item/%E6%9D%8E%E7%99%BD/1043'}}, 'id': 'call_0d250ee206dc432e9df0df', 'type': 'tool_call'}]. Every tool call (LLM requesting to call a tool) in the message history MUST have a corresponding ToolMessage (result of a tool invocation to return to the LLM) - this is required by most LLM providers. For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/INVALID_CHAT_HISTORY During task with name 'agent' and id '865b2742-7d72-add6-8500-98541855a6ef'
Author
Owner

@dosnshcu commented on GitHub (Oct 16, 2025):

How should I fix this error?How do I continue using this thread for conversation?

@dosnshcu commented on GitHub (Oct 16, 2025): How should I fix this error?How do I continue using this thread for conversation?
yindo changed title from Save `AIMessage` object based on agent error type to [GH-ISSUE #935] Save `AIMessage` object based on agent error type 2026-06-05 17:25:10 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#120