Skip to content

fix(core): include tool_call_id in on_tool_start and on_tool_end events#35370

Closed
Balaji Seshadri (gitbalaji) wants to merge 1 commit intolangchain-ai:masterfrom
gitbalaji:fix/core-tool-call-id-in-events
Closed

fix(core): include tool_call_id in on_tool_start and on_tool_end events#35370
Balaji Seshadri (gitbalaji) wants to merge 1 commit intolangchain-ai:masterfrom
gitbalaji:fix/core-tool-call-id-in-events

Conversation

@gitbalaji
Copy link
Copy Markdown
Contributor

Summary

  • on_tool_error already included tool_call_id in its event data payload, but on_tool_start and on_tool_end were missing it.
  • Without this field, callback handlers and astream_events() consumers cannot correlate a tool execution with the originating LLM tool call — breaking observability and tracing.

Fixes #34849.

Root cause

In langchain_core/tracers/event_stream.py, tool_call_id is received in on_tool_start via **kwargs and stored in run_info. However, the _send() calls for on_tool_start and on_tool_end never included it in the event data dict. on_tool_error already did this correctly and serves as the reference pattern.

Changes

  • on_tool_start: add "tool_call_id": kwargs.get("tool_call_id") to the _send data
  • on_tool_end: read tool_call_id from run_info (with kwargs fallback, identical to on_tool_error) and include it in the _send data

Areas requiring careful review

  • The tool_call_id field is None when the tool is invoked with a plain dict rather than a ToolCall dict — this is intentional and consistent with on_tool_error.
  • No change to on_tool_error (already correct).

Test plan

  • test_tool_start_event_includes_tool_call_id — asserts correct id is present
  • test_tool_start_event_tool_call_id_is_none_when_not_provided — asserts None for plain dict input
  • test_tool_end_event_includes_tool_call_id — asserts correct id is present
  • test_tool_end_event_tool_call_id_is_none_when_not_provided — asserts None for plain dict input
  • All 6 tool_call_id tests pass (including the 2 pre-existing on_tool_error tests)
  • ruff check, ruff format, mypy clean

This PR was developed with the assistance of Claude Code (AI agent).

🤖 Generated with Claude Code

@github-actions github-actions Bot added core `langchain-core` package issues & PRs external fix For PRs that implement a fix labels Feb 20, 2026
Fixes langchain-ai#34849.

`on_tool_error` already included `tool_call_id` in its event data, but
`on_tool_start` and `on_tool_end` were missing it. Without this field,
callback handlers and event consumers cannot correlate a tool execution
with the originating LLM tool call.

- `on_tool_start`: add `tool_call_id` from `kwargs` to the `_send` payload
- `on_tool_end`: mirror the `on_tool_error` pattern — read from `run_info`
  (stored during `on_tool_start`) with a `kwargs` fallback, then emit it

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Feb 20, 2026

Merging this PR will not alter performance

⚠️ 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.

✅ 13 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing gitbalaji:fix/core-tool-call-id-in-events (210baa1) with master (be1c3fc)

Open in CodSpeed

Footnotes

  1. 23 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.

Copy link
Copy Markdown
Collaborator

@ccurme ccurme (ccurme) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Underlying issue is closed. Please read my comment there.

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

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pass tool_call_id to on_tool_start callback

3 participants