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

This PR completes work for PR #18798 to expose raw tool output in
on_tool_end.

Affected APIs:
* astream_log
* astream_events
* callbacks sent to langsmith via langsmith-sdk
* Any other code that relies on BaseTracer!

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
pull/19895/head
Mohammad Mohtashim 6 months ago committed by GitHub
parent 2ae6dcdf01
commit 9ae2df36fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -984,7 +984,6 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
Args:
output (Any): The output of the tool.
"""
output = str(output)
handle_event(
self.handlers,
"on_tool_end",

@ -506,7 +506,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
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)

@ -52,6 +52,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) -> dict:
"""Foo"""
return {"x": 5}
@tool
def get_docs(x: int) -> List[Document]:
"""Hello Doc"""
return [Document(page_content="hello")]
chain = RunnableLambda(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."""

Loading…
Cancel
Save