# 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>
Packages
Important
This repository is structured as a monorepo, with various packages located in this libs/ directory. Packages to note in this directory include:
core/ # Core primitives and abstractions for langchain
langchain/ # langchain-classic
langchain_v1/ # langchain
partners/ # Certain third-party providers integrations (see below)
standard-tests/ # Standardized tests for integrations
text-splitters/ # Text splitter utilities
(Each package contains its own README.md file with specific details about that package.)
Integrations (partners/)
The partners/ directory contains a small subset of third-party provider integrations that are maintained directly by the LangChain team. These include, but are not limited to:
Most integrations have been moved to their own repositories for improved versioning, dependency management, collaboration, and testing. This includes packages from popular providers such as Google and AWS. Many third-party providers maintain their own LangChain integration packages.
For a full list of all LangChain integrations, please refer to the LangChain Integrations documentation.