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
Closed
fix(core): include tool_call_id in on_tool_start and on_tool_end events#35370Balaji Seshadri (gitbalaji) wants to merge 1 commit intolangchain-ai:masterfrom
Balaji Seshadri (gitbalaji) wants to merge 1 commit intolangchain-ai:masterfrom
Conversation
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>
23b1530 to
210baa1
Compare
Merging this PR will not alter performance
|
Collaborator
ccurme (ccurme)
left a comment
There was a problem hiding this comment.
Underlying issue is closed. Please read my comment there.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
on_tool_erroralready includedtool_call_idin its event data payload, buton_tool_startandon_tool_endwere missing it.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_idis received inon_tool_startvia**kwargsand stored inrun_info. However, the_send()calls foron_tool_startandon_tool_endnever included it in the eventdatadict.on_tool_erroralready did this correctly and serves as the reference pattern.Changes
on_tool_start: add"tool_call_id": kwargs.get("tool_call_id")to the_senddataon_tool_end: readtool_call_idfromrun_info(withkwargsfallback, identical toon_tool_error) and include it in the_senddataAreas requiring careful review
tool_call_idfield isNonewhen the tool is invoked with a plain dict rather than aToolCalldict — this is intentional and consistent withon_tool_error.on_tool_error(already correct).Test plan
test_tool_start_event_includes_tool_call_id— asserts correct id is presenttest_tool_start_event_tool_call_id_is_none_when_not_provided— assertsNonefor plain dict inputtest_tool_end_event_includes_tool_call_id— asserts correct id is presenttest_tool_end_event_tool_call_id_is_none_when_not_provided— assertsNonefor plain dict inputtool_call_idtests pass (including the 2 pre-existingon_tool_errortests)ruff check,ruff format,mypyclean🤖 Generated with Claude Code