Skip to content
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

Core[major]: Base Tracer to propagate raw output from tool for on_tool_end #18932

Merged
merged 12 commits into from
Apr 2, 2024
1 change: 0 additions & 1 deletion libs/core/langchain_core/callbacks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,6 @@ def on_tool_end(
Args:
output (Any): The output of the tool.
"""
output = str(output)
handle_event(
self.handlers,
"on_tool_end",
Expand Down
1 change: 0 additions & 1 deletion libs/core/langchain_core/tracers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ def on_tool_start(

def on_tool_end(self, output: Any, *, run_id: UUID, **kwargs: Any) -> Run:
"""End a trace for a tool run."""
output = str(output)
tool_run = self._get_run(run_id, run_type="tool")
tool_run.outputs = {"output": output}
tool_run.end_time = datetime.now(timezone.utc)
Expand Down
82 changes: 82 additions & 0 deletions libs/core/tests/unit_tests/runnables/test_runnable_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,88 @@ async def _collect_events(events: AsyncIterator[StreamEvent]) -> List[StreamEven
return events_


async def test_event_stream_with_simple_function_tool() -> None:
"""Test the event stream with a function and tool"""

def foo(x:int):
eyurtsev marked this conversation as resolved.
Show resolved Hide resolved
"""Foo"""
return {"x": 5}

@tool
def get_docs(x: int):
eyurtsev marked this conversation as resolved.
Show resolved Hide resolved
"""Hello Doc"""
return [Document(page_content="hello")]

chain = foo | get_docs
events = await _collect_events(chain.astream_events({}, version="v1"))
assert events == [
{
"event": "on_chain_start",
"run_id": "",
"name": "RunnableSequence",
"tags": [],
"metadata": {},
"data": {"input": {}},
},
{
"event": "on_chain_start",
"name": "foo",
"run_id": "",
"tags": ["seq:step:1"],
"metadata": {},
"data": {},
},
{
"event": "on_chain_stream",
"name": "foo",
"run_id": "",
"tags": ["seq:step:1"],
"metadata": {},
"data": {"chunk": {"x": 5}},
},
{
"event": "on_chain_end",
"name": "foo",
"run_id": "",
"tags": ["seq:step:1"],
"metadata": {},
"data": {"input": {}, "output": {"x": 5}},
},
{
"event": "on_tool_start",
"name": "get_docs",
"run_id": "",
"tags": ["seq:step:2"],
"metadata": {},
"data": {"input": {"x": 5}},
},
{
"event": "on_tool_end",
"name": "get_docs",
"run_id": "",
"tags": ["seq:step:2"],
"metadata": {},
"data": {"input": {"x": 5}, "output": [Document(page_content="hello")]},
},
{
"event": "on_chain_stream",
"run_id": "",
"tags": [],
"metadata": {},
"name": "RunnableSequence",
"data": {"chunk": [Document(page_content="hello")]},
},
{
"event": "on_chain_end",
"name": "RunnableSequence",
"run_id": "",
"tags": [],
"metadata": {},
"data": {"output": [Document(page_content="hello")]},
},
]


async def test_event_stream_with_single_lambda() -> None:
"""Test the event stream with a tool."""

Expand Down