# Add `tool_call_id` to `on_tool_error` event data ## Summary This PR addresses issue #33597 by adding `tool_call_id` to the `on_tool_error` callback event data. This enables users to link tool errors to specific tool calls in stateless agent implementations, which is essential for building OpenAI-compatible APIs and tracking tool execution flows. ## Problem When streaming events using `astream_events` with `version="v2"`, the `on_tool_error` event only included the error and input data, but lacked the `tool_call_id`. This made it difficult to: - Link errors to specific tool calls in stateless agent scenarios - Implement OpenAI-compatible APIs that require tool call tracking - Track tool execution flows when using `run_id` is not sufficient ## Solution The fix adds `tool_call_id` propagation through the callback chain: 1. **Pass `tool_call_id` to callbacks**: Updated `BaseTool.run()` and `BaseTool.arun()` to pass `tool_call_id` to both `on_tool_start` and `on_tool_error` callbacks 2. **Store in event stream handler**: Modified `_AstreamEventsCallbackHandler` to store `tool_call_id` in run info during `on_tool_start` 3. **Include in error events**: Updated `on_tool_error` handler to extract and include `tool_call_id` in the event data ## Changes - **`libs/core/langchain_core/tools/base.py`**: - Pass `tool_call_id` to `on_tool_start` in both sync and async methods - Pass `tool_call_id` to `on_tool_error` when errors occur - **`libs/core/langchain_core/tracers/event_stream.py`**: - Store `tool_call_id` in run info during `on_tool_start` - Extract `tool_call_id` from kwargs or run info in `on_tool_error` - Include `tool_call_id` in the `on_tool_error` event data ## Testing The fix was verified by: 1. Direct tool invocation: Confirmed `tool_call_id` appears in `on_tool_error` event data when calling tools directly 2. Agent integration: Tested with `create_agent` to ensure `tool_call_id` is present in error events during agent execution ```python # Example verification async for event in agent.astream_events( {"messages": "Please demonstrate a tool error"}, version="v2", ): if event["event"] == "on_tool_error": assert "tool_call_id" in event["data"] # ✓ Now passes print(event["data"]["tool_call_id"]) ``` ## Backward Compatibility - ✅ Fully backward compatible: `tool_call_id` is optional (can be `None`) - ✅ No breaking changes: All changes are additive - ✅ Existing code continues to work without modification ## Related Issues Fixes #33597 --------- Co-authored-by: Mason Daugherty <github@mdrxy.com>
🦜🍎️ LangChain Core
Looking for the JS/TS version? Check out LangChain.js.
To help you ship LangChain apps to production faster, check out LangSmith. LangSmith is a unified developer platform for building, testing, and monitoring LLM applications.
Quick Install
pip install langchain-core
🤔 What is this?
LangChain Core contains the base abstractions that power the LangChain ecosystem.
These abstractions are designed to be as modular and simple as possible.
The benefit of having these abstractions is that any provider can implement the required interface and then easily be used in the rest of the LangChain ecosystem.
⛰️ Why build on top of LangChain Core?
The LangChain ecosystem is built on top of langchain-core. Some of the benefits:
- Modularity: We've designed Core around abstractions that are independent of each other, and not tied to any specific model provider.
- Stability: We are committed to a stable versioning scheme, and will communicate any breaking changes with advance notice and version bumps.
- Battle-tested: Core components have the largest install base in the LLM ecosystem, and are used in production by many companies.
📖 Documentation
For full documentation, see the API reference. For conceptual guides, tutorials, and examples on using LangChain, see the LangChain Docs.
📕 Releases & Versioning
See our Releases and Versioning policies.
💁 Contributing
As an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.
For detailed information on how to contribute, see the Contributing Guide.