Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/core/langchain_core/tracers/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ async def on_tool_start(
"event": "on_tool_start",
"data": {
"input": inputs or {},
"tool_call_id": kwargs.get("tool_call_id"),
},
"name": name_,
"tags": tags or [],
Expand Down
50 changes: 50 additions & 0 deletions libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,56 @@
assert error_event["data"]["tool_call_id"] == tool_call_id


async def test_tool_start_event_includes_tool_call_id() -> None:
"""Test that on_tool_start event includes tool_call_id when provided."""

@tool
def sample_tool(x: int) -> str:
"""A sample tool."""
return f"value: {x}"

tool_call_id = "test-tool-call-id-123"
tool_call = {
"name": "sample_tool",
"args": {"x": 42},
"id": tool_call_id,
"type": "tool_call",
}

events = [
event async for event in sample_tool.astream_events(tool_call, version="v2")
]

start_events = [e for e in events if e["event"] == "on_tool_start"]
assert len(start_events) == 1

start_event = start_events[0]
assert start_event["name"] == "sample_tool"
assert "tool_call_id" in start_event["data"]
assert start_event["data"]["tool_call_id"] == tool_call_id


async def test_tool_start_event_tool_call_id_is_none_when_not_provided() -> None:
"""Test that on_tool_start event has tool_call_id=None when not provided."""

@tool
def sample_tool_no_id(x: int) -> str:
"""A sample tool."""
return f"value: {x}"

events = [
event async for event in sample_tool_no_id.astream_events({"x": 42}, version="v2")

Check failure on line 2920 in libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py

View workflow job for this annotation

GitHub Actions / lint (libs/core, 3.13) / Python 3.13

ruff (E501)

tests/unit_tests/runnables/test_runnable_events_v2.py:2920:89: E501 Line too long (90 > 88)

Check failure on line 2920 in libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py

View workflow job for this annotation

GitHub Actions / lint (libs/core, 3.10) / Python 3.10

ruff (E501)

tests/unit_tests/runnables/test_runnable_events_v2.py:2920:89: E501 Line too long (90 > 88)

Check failure on line 2920 in libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py

View workflow job for this annotation

GitHub Actions / lint (libs/core, 3.11) / Python 3.11

ruff (E501)

tests/unit_tests/runnables/test_runnable_events_v2.py:2920:89: E501 Line too long (90 > 88)

Check failure on line 2920 in libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py

View workflow job for this annotation

GitHub Actions / lint (libs/core, 3.12) / Python 3.12

ruff (E501)

tests/unit_tests/runnables/test_runnable_events_v2.py:2920:89: E501 Line too long (90 > 88)

Check failure on line 2920 in libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py

View workflow job for this annotation

GitHub Actions / lint (libs/core, 3.14) / Python 3.14

ruff (E501)

tests/unit_tests/runnables/test_runnable_events_v2.py:2920:89: E501 Line too long (90 > 88)
]

start_events = [e for e in events if e["event"] == "on_tool_start"]
assert len(start_events) == 1

start_event = start_events[0]
assert start_event["name"] == "sample_tool_no_id"
assert "tool_call_id" in start_event["data"]
assert start_event["data"]["tool_call_id"] is None


async def test_tool_error_event_tool_call_id_is_none_when_not_provided() -> None:
"""Test that on_tool_error event has tool_call_id=None when not provided."""

Expand Down
Loading