Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug caused by create_react_agent method and runnable asteam_events #30708

Open
4 tasks done
deershark opened this issue Apr 7, 2025 · 1 comment · May be fixed by #30709
Open
4 tasks done

Bug caused by create_react_agent method and runnable asteam_events #30708

deershark opened this issue Apr 7, 2025 · 1 comment · May be fixed by #30709
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: core Related to langchain-core

Comments

@deershark
Copy link
Contributor

deershark commented Apr 7, 2025

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • 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

import asyncio

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent


# Create an intentionally error-prone tool
@tool
def error_prone_tool(input: str) -> str:
    """A tool that always raises an error for demonstration purposes."""
    return 1 / 0


# Set up the agent
model = ChatOpenAI(model="gpt-4o-mini")
tools = [error_prone_tool]
agent = create_react_agent(model, tools)


async def demonstrate_issue():
    # Stream events to show missing tool_error event
    async for event in agent.astream_events(
        {"messages": "Please demonstrate a tool error"},
        version="v2",
    ):
        print(f"Event type: {event['event']}, Event name: {event['name']} Event data: {event['data']}")
        # Notice there's no 'tool_error' event type in the output

        # This is the issue - when a tool fails, we should get a 'tool_error' event
        # but currently we don't, making error handling difficult


if __name__ == "__main__":
    asyncio.run(demonstrate_issue())

Error Message and Stack Trace (if applicable)

Description

I am using the create_react_agent method to get all output events through astream_events. I want to get the error event to process and display the error information to the user.
If an error occurs in tool, there will be no on_tool_end event !!!!!
And, I see that the documentation of this method does not mention the error type of event (such as on_tool_error).
Is there any way to do this? If you do not use a custom callback.

Here is the output of running:

......
Event type: on_chain_start, Event name: tools Event data: {'input': {'messages': [HumanMessage(content='Please demonstrate a tool error', additional_kwargs={}, response_metadata={}, id='51b97b0a-2f8a-45c2-b6a8-a07f3c892882'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_U8rGfLpsdQAoXhGZ01R4DpWd', 'function': {'arguments': '{"input":"Demonstrate tool error"}', 'name': 'error_prone_tool'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_b705f0c291'}, id='run-00652a81-d2b9-469f-b413-79919c7c7839', tool_calls=[{'name': 'error_prone_tool', 'args': {'input': 'Demonstrate tool error'}, 'id': 'call_U8rGfLpsdQAoXhGZ01R4DpWd', 'type': 'tool_call'}], usage_metadata={'input_tokens': 53, 'output_tokens': 20, 'total_tokens': 66, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0}})], 'is_last_step': False, 'remaining_steps': 23}}

Event type: on_tool_start, Event name: error_prone_tool Event data: {'input': {'input': 'Demonstrate tool error'}}

Event type: on_chain_start, Event name: _write Event data: {'input': {'messages': [ToolMessage(content="Error: ZeroDivisionError('division by zero')\n Please fix your mistakes.", name='error_prone_tool', tool_call_id='call_U8rGfLpsdQAoXhGZ01R4DpWd', status='error')]}}
Event type: on_chain_end, Event name: _write Event data: {'output': {'messages': [ToolMessage(content="Error: ZeroDivisionError('division by zero')\n Please fix your mistakes.", name='error_prone_tool', tool_call_id='call_U8rGfLpsdQAoXhGZ01R4DpWd', status='error')]}, 'input': {'messages': [ToolMessage(content="Error: ZeroDivisionError('division by zero')\n Please fix your mistakes.", name='error_prone_tool', tool_call_id='call_U8rGfLpsdQAoXhGZ01R4DpWd', status='error')]}}
Event type: on_chain_start, Event name: _write Event data: {'input': {'messages': [ToolMessage(content="Error: ZeroDivisionError('division by zero')\n Please fix your mistakes.", name='error_prone_tool', tool_call_id='call_U8rGfLpsdQAoXhGZ01R4DpWd', status='error')]}}
......

System Info

System Information

OS: Linux
OS Version: langchain-ai/langgraph#1 SMP PREEMPT_DYNAMIC Fri Mar 8 11:32:16 CST 2024
Python Version: 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0]

Package Information

langchain_core: 0.3.46
langsmith: 0.3.18
langchain_mcp_adapters: 0.0.5
langgraph_test: Installed. No version info available.

Optional packages not installed

langserve

Other Dependencies

httpx: 0.28.1
jsonpatch<2.0,>=1.33: Installed. No version info available.
langchain-core<0.4,>=0.3.36: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
langsmith<0.4,>=0.1.125: Installed. No version info available.
mcp<1.5,>=1.4.1: Installed. No version info available.
openai-agents: Installed. No version info available.
opentelemetry-api: Installed. No version info available.
opentelemetry-exporter-otlp-proto-http: Installed. No version info available.
opentelemetry-sdk: Installed. No version info available.
orjson: 3.10.15
packaging: 24.2
packaging<25,>=23.2: Installed. No version info available.
pydantic: 2.10.6
pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
pytest: 8.3.4
PyYAML>=5.3: Installed. No version info available.
requests: 2.31.0
requests-toolbelt: 1.0.0
rich: Installed. No version info available.
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
typing-extensions>=4.7: Installed. No version info available.
zstandard: 0.23.0

@vbarda
Copy link
Contributor

vbarda commented Apr 7, 2025

Thanks for reporting - this is an issue in langchain-core, fix incoming

@vbarda vbarda transferred this issue from langchain-ai/langgraph Apr 7, 2025
@dosubot dosubot bot added Ɑ: core Related to langchain-core 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: core Related to langchain-core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants