-
Notifications
You must be signed in to change notification settings - Fork 19.6k
feat(core): automatically count and store meta for tool call count #33756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mdrxy
wants to merge
1
commit into
master
Choose a base branch
from
mdrxy/count-tool-calls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+465
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,13 +37,28 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC): | |
| def _persist_run(self, run: Run) -> None: | ||
| """Persist a run.""" | ||
|
|
||
| def _store_tool_call_metadata(self, run: Run) -> None: | ||
| """Store tool call count in run metadata automatically.""" | ||
| try: | ||
| # Avoid circular imports | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Utils doesn't import from base, so this shouldn't be needed right? |
||
| from langchain_core.tracers.utils import ( # noqa: PLC0415 | ||
| store_tool_call_count_in_run, | ||
| ) | ||
|
|
||
| store_tool_call_count_in_run(run) | ||
| except Exception: # noqa: S110 | ||
| # Avoid breaking existing functionality | ||
| pass | ||
|
|
||
| def _start_trace(self, run: Run) -> None: | ||
| """Start a trace for a run.""" | ||
| super()._start_trace(run) | ||
| self._on_run_create(run) | ||
|
|
||
| def _end_trace(self, run: Run) -> None: | ||
| """End a trace for a run.""" | ||
| self._store_tool_call_metadata(run) | ||
|
|
||
| if not run.parent_run_id: | ||
| self._persist_run(run) | ||
| self.run_map.pop(str(run.id)) | ||
|
|
@@ -534,6 +549,19 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC): | |
| async def _persist_run(self, run: Run) -> None: | ||
| """Persist a run.""" | ||
|
|
||
| async def _store_tool_call_metadata(self, run: Run) -> None: | ||
| """Store tool call count in run metadata.""" | ||
| try: | ||
| # Avoid circular imports | ||
| from langchain_core.tracers.utils import ( # noqa: PLC0415 | ||
| store_tool_call_count_in_run, | ||
| ) | ||
|
|
||
| store_tool_call_count_in_run(run) | ||
| except Exception: # noqa: S110 | ||
| # Avoid breaking existing functionality | ||
| pass | ||
|
|
||
| @override | ||
| async def _start_trace(self, run: Run) -> None: | ||
| """Start a trace for a run. | ||
|
|
@@ -551,6 +579,8 @@ async def _end_trace(self, run: Run) -> None: | |
| Ending a trace will run concurrently with each _on_[run_type]_end method. | ||
| No _on_[run_type]_end callback should depend on operations in _end_trace. | ||
| """ | ||
| await self._store_tool_call_metadata(run) | ||
|
|
||
| if not run.parent_run_id: | ||
| await self._persist_run(run) | ||
| self.run_map.pop(str(run.id)) | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| """Utility functions for working with Run objects and tracers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from langchain_core.tracers.schemas import Run | ||
|
|
||
|
|
||
| def count_tool_calls_in_run(run: Run) -> int: | ||
| """Count tool calls in a `Run` object by examining messages. | ||
| Args: | ||
| run: The `Run` object to examine. | ||
| Returns: | ||
| The total number of tool calls found in the run's messages. | ||
| """ | ||
| tool_call_count = 0 | ||
|
|
||
| # Check inputs for messages containing tool calls | ||
| inputs = getattr(run, "inputs", {}) or {} | ||
| if isinstance(inputs, dict) and "messages" in inputs: | ||
| messages = inputs["messages"] | ||
| if messages: | ||
| for msg in messages: | ||
| # Handle both dict and object representations | ||
| if hasattr(msg, "tool_calls"): | ||
| tool_calls = getattr(msg, "tool_calls", []) | ||
| if tool_calls: | ||
| tool_call_count += len(tool_calls) | ||
| elif isinstance(msg, dict) and "tool_calls" in msg: | ||
| tool_calls = msg.get("tool_calls", []) | ||
| if tool_calls: | ||
| tool_call_count += len(tool_calls) | ||
|
|
||
| # Also check outputs for completeness | ||
| outputs = getattr(run, "outputs", {}) or {} | ||
| if isinstance(outputs, dict) and "messages" in outputs: | ||
| messages = outputs["messages"] | ||
| if messages: | ||
| for msg in messages: | ||
| if hasattr(msg, "tool_calls"): | ||
| tool_calls = getattr(msg, "tool_calls", []) | ||
| if tool_calls: | ||
| tool_call_count += len(tool_calls) | ||
| elif isinstance(msg, dict) and "tool_calls" in msg: | ||
| tool_calls = msg.get("tool_calls", []) | ||
| if tool_calls: | ||
| tool_call_count += len(tool_calls) | ||
|
|
||
| return tool_call_count | ||
|
|
||
|
|
||
| def store_tool_call_count_in_run(run: Run, *, always_store: bool = False) -> int: | ||
| """Count tool calls in a `Run` and store the count in run metadata. | ||
| Args: | ||
| run: The `Run` object to analyze and modify. | ||
| always_store: If `True`, always store the count even if `0`. | ||
| If `False`, only store when there are tool calls. | ||
| Returns: | ||
| The number of tool calls found and stored. | ||
| """ | ||
| tool_call_count = count_tool_calls_in_run(run) | ||
|
|
||
| # Only store if there are tool calls or if explicitly requested | ||
| if tool_call_count > 0 or always_store: | ||
| # Store in run.extra for easy access | ||
| if not hasattr(run, "extra") or run.extra is None: | ||
| run.extra = {} | ||
| run.extra["tool_call_count"] = tool_call_count | ||
|
|
||
| return tool_call_count | ||
|
|
||
|
|
||
| def get_tool_call_count_from_run(run: Run) -> int | None: | ||
| """Get the tool call count from run metadata if available. | ||
| Args: | ||
| run: The `Run` object to check. | ||
| Returns: | ||
| The tool call count if stored in metadata, otherwise `None`. | ||
| """ | ||
| extra = getattr(run, "extra", {}) or {} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessarily defensive right? |
||
| if isinstance(extra, dict): | ||
| return extra.get("tool_call_count") | ||
| return None | ||
127 changes: 127 additions & 0 deletions
127
libs/core/tests/unit_tests/tracers/test_automatic_metadata.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| """Test automatic tool call count storage in tracers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import MagicMock, PropertyMock | ||
|
|
||
| from langchain_core.messages import AIMessage | ||
| from langchain_core.messages.tool import ToolCall | ||
| from langchain_core.tracers.base import BaseTracer | ||
| from langchain_core.tracers.schemas import Run | ||
|
|
||
|
|
||
| class MockTracer(BaseTracer): | ||
| """Mock tracer for testing automatic metadata storage.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.persisted_runs: list[Run] = [] | ||
|
|
||
| def _persist_run(self, run: Run) -> None: | ||
| """Store the run for inspection.""" | ||
| self.persisted_runs.append(run) | ||
|
|
||
|
|
||
| def test_base_tracer_automatically_stores_tool_call_count() -> None: | ||
| """Test that `BaseTracer` automatically stores tool call count.""" | ||
| tracer = MockTracer() | ||
|
|
||
| # Create a mock run with tool calls | ||
| run = MagicMock(spec=Run) | ||
| run.id = "test-run-id" | ||
| run.parent_run_id = None # Root run, will be persisted | ||
| run.extra = {} | ||
|
|
||
| # Set up messages with tool calls | ||
| tool_calls = [ | ||
| ToolCall(name="search", args={"query": "test"}, id="call_1"), | ||
| ToolCall(name="calculator", args={"expression": "2+2"}, id="call_2"), | ||
| ] | ||
| messages = [AIMessage(content="Test", tool_calls=tool_calls)] | ||
| run.inputs = {"messages": messages} | ||
| run.outputs = {} | ||
|
|
||
| # Add run to tracer's run_map to simulate it being tracked | ||
| tracer.run_map[str(run.id)] = run | ||
|
|
||
| # End the trace (this should trigger automatic metadata storage) | ||
| tracer._end_trace(run) | ||
|
|
||
| # Verify tool call count was automatically stored | ||
| assert "tool_call_count" in run.extra | ||
| assert run.extra["tool_call_count"] == 2 | ||
|
|
||
| # Verify the run was persisted | ||
| assert len(tracer.persisted_runs) == 1 | ||
| assert tracer.persisted_runs[0] == run | ||
|
|
||
|
|
||
| def test_base_tracer_handles_no_tool_calls() -> None: | ||
| """Test that `BaseTracer` handles runs with no tool calls gracefully.""" | ||
| tracer = MockTracer() | ||
|
|
||
| # Create a mock run without tool calls | ||
| run = MagicMock(spec=Run) | ||
| run.id = "test-run-id-no-tools" | ||
| run.parent_run_id = None | ||
| run.extra = {} | ||
|
|
||
| # Set up messages without tool calls | ||
| messages = [AIMessage(content="No tools here")] | ||
| run.inputs = {"messages": messages} | ||
| run.outputs = {} | ||
|
|
||
| # Add run to tracer's run_map | ||
| tracer.run_map[str(run.id)] = run | ||
|
|
||
| # End the trace | ||
| tracer._end_trace(run) | ||
|
|
||
| # Verify tool call count is not stored when there are no tool calls | ||
| assert "tool_call_count" not in run.extra | ||
|
|
||
|
|
||
| def test_base_tracer_handles_runs_without_messages() -> None: | ||
| """Test that `BaseTracer` handles runs without messages gracefully.""" | ||
| tracer = MockTracer() | ||
|
|
||
| # Create a mock run without messages | ||
| run = MagicMock(spec=Run) | ||
| run.id = "test-run-id-no-messages" | ||
| run.parent_run_id = None | ||
| run.extra = {} | ||
| run.inputs = {} | ||
| run.outputs = {} | ||
|
|
||
| # Add run to tracer's run_map | ||
| tracer.run_map[str(run.id)] = run | ||
|
|
||
| # End the trace | ||
| tracer._end_trace(run) | ||
|
|
||
| # Verify tool call count is not stored when there are no messages | ||
| assert "tool_call_count" not in run.extra | ||
|
|
||
|
|
||
| def test_base_tracer_doesnt_break_on_metadata_error() -> None: | ||
| """Test that `BaseTracer` continues working if metadata storage fails.""" | ||
| tracer = MockTracer() | ||
|
|
||
| # Create a mock run that will cause an error in tool call counting | ||
| run = MagicMock(spec=Run) | ||
| run.id = "test-run-id-error" | ||
| run.parent_run_id = None | ||
| run.extra = {} | ||
|
|
||
| # Make the run.inputs property raise an error when accessed | ||
| type(run).inputs = PropertyMock(side_effect=RuntimeError("Simulated error")) | ||
|
|
||
| # Add run to tracer's run_map | ||
| tracer.run_map[str(run.id)] = run | ||
|
|
||
| # End the trace - this should not raise an exception | ||
| tracer._end_trace(run) | ||
|
|
||
| # The run should still be persisted despite the metadata error | ||
| assert len(tracer.persisted_runs) == 1 | ||
| assert tracer.persisted_runs[0] == run |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put this all in
_complete_llm_runin tracers/core. We don't need to do this on all runs IMO