Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Shreyanshsingh23
Copy link

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
# 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

Add tool_call_id parameter to on_tool_error callback events to enable
linking tool errors to specific tool calls in stateless agent implementations.

- Pass tool_call_id to on_tool_start and on_tool_error callbacks in BaseTool
- Store tool_call_id in event stream handler run info
- Include tool_call_id in on_tool_error event data

Fixes langchain-ai#33597
@github-actions github-actions bot added core Related to the package `langchain-core` fix labels Oct 29, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 29, 2025

CodSpeed Performance Report

Merging #33731 will not alter performance

Comparing Shreyanshsingh23:add-tool-call-id-to-on-tool-error (2119bf3) with master (6a1dca1)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched
⏩ 21 skipped1

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Refactor the on_tool_error callback to utilize a structured event object for improved clarity and maintainability. This change includes the addition of tool_call_id to the event data, enhancing the linkage of tool errors to specific tool calls.

- Updated the on_tool_error method to create a structured event object
- Included tool_call_id in the event data

This change builds on the previous addition of tool_call_id to enhance error tracking in stateless agent implementations.
@Shreyanshsingh23
Copy link
Author

Hey @eyurtsev, should I go ahead and make changes in my code to solve this conflict or you are looking to do? Jusk asking before going ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to the package `langchain-core` fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

langchain 1.0.0: add tool_call_id to on_tool_error event's data

1 participant