@ -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. """