@ -29,6 +29,7 @@ from langchain_core.runnables import (
from langchain_core . runnables . history import RunnableWithMessageHistory
from langchain_core . runnables . schema import StreamEvent
from langchain_core . tools import tool
from tests . unit_tests . stubs import AnyStr
def _with_nulled_run_id ( events : Sequence [ StreamEvent ] ) - > List [ StreamEvent ] :
@ -340,7 +341,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " hello " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " hello " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -348,7 +349,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -356,7 +357,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " world! " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " world! " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -364,7 +365,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " output " : AIMessageChunk ( content = " hello world! " )} ,
" data " : { " output " : AIMessageChunk ( content = " hello world! " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_end " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -399,7 +400,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " hello " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " hello " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -407,7 +408,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -415,7 +416,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " world! " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " world! " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -430,7 +431,9 @@ async def test_astream_events_from_model() -> None:
[
{
" generation_info " : None ,
" message " : AIMessage ( content = " hello world! " ) ,
" message " : AIMessage (
content = " hello world! " , id = AnyStr ( )
) ,
" text " : " hello world! " ,
" type " : " ChatGeneration " ,
}
@ -447,7 +450,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessage ( content = " hello world! " )} ,
" data " : { " chunk " : AIMessage ( content = " hello world! " , id = AnyStr ( ) )} ,
" event " : " on_chain_stream " ,
" metadata " : { } ,
" name " : " i_dont_stream " ,
@ -455,7 +458,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ ] ,
} ,
{
" data " : { " output " : AIMessage ( content = " hello world! " )} ,
" data " : { " output " : AIMessage ( content = " hello world! " , id = AnyStr ( ) )} ,
" event " : " on_chain_end " ,
" metadata " : { } ,
" name " : " i_dont_stream " ,
@ -490,7 +493,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " hello " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " hello " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -498,7 +501,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -506,7 +509,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " world! " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " world! " , id = AnyStr ( ) )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " } ,
" name " : " my_model " ,
@ -521,7 +524,9 @@ async def test_astream_events_from_model() -> None:
[
{
" generation_info " : None ,
" message " : AIMessage ( content = " hello world! " ) ,
" message " : AIMessage (
content = " hello world! " , id = AnyStr ( )
) ,
" text " : " hello world! " ,
" type " : " ChatGeneration " ,
}
@ -538,7 +543,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ " my_model " ] ,
} ,
{
" data " : { " chunk " : AIMessage ( content = " hello world! " )} ,
" data " : { " chunk " : AIMessage ( content = " hello world! " , id = AnyStr ( ) )} ,
" event " : " on_chain_stream " ,
" metadata " : { } ,
" name " : " ai_dont_stream " ,
@ -546,7 +551,7 @@ async def test_astream_events_from_model() -> None:
" tags " : [ ] ,
} ,
{
" data " : { " output " : AIMessage ( content = " hello world! " )} ,
" data " : { " output " : AIMessage ( content = " hello world! " , id = AnyStr ( ) )} ,
" event " : " on_chain_end " ,
" metadata " : { } ,
" name " : " ai_dont_stream " ,
@ -563,7 +568,10 @@ async def test_event_stream_with_simple_chain() -> None:
) . with_config ( { " run_name " : " my_template " , " tags " : [ " my_template " ] } )
infinite_cycle = cycle (
[ AIMessage ( content = " hello world! " ) , AIMessage ( content = " goodbye world! " ) ]
[
AIMessage ( content = " hello world! " , id = " ai1 " ) ,
AIMessage ( content = " goodbye world! " , id = " ai2 " ) ,
]
)
# When streaming GenericFakeChatModel breaks AIMessage into chunks based on spaces
model = (
@ -640,7 +648,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " , " my_model " , " seq:step:2 " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " hello " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " hello " , id = " ai1 " )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " , " foo " : " bar " } ,
" name " : " my_model " ,
@ -648,7 +656,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " , " my_model " , " seq:step:2 " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " hello " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " hello " , id = " ai1 " )} ,
" event " : " on_chain_stream " ,
" metadata " : { " foo " : " bar " } ,
" name " : " my_chain " ,
@ -656,7 +664,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " " , id = " ai1 " )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " , " foo " : " bar " } ,
" name " : " my_model " ,
@ -664,7 +672,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " , " my_model " , " seq:step:2 " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " " , id = " ai1 " )} ,
" event " : " on_chain_stream " ,
" metadata " : { " foo " : " bar " } ,
" name " : " my_chain " ,
@ -672,7 +680,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " world! " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " world! " , id = " ai1 " )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { " a " : " b " , " foo " : " bar " } ,
" name " : " my_model " ,
@ -680,7 +688,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " , " my_model " , " seq:step:2 " ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " world! " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " world! " , id = " ai1 " )} ,
" event " : " on_chain_stream " ,
" metadata " : { " foo " : " bar " } ,
" name " : " my_chain " ,
@ -702,7 +710,9 @@ async def test_event_stream_with_simple_chain() -> None:
[
{
" generation_info " : None ,
" message " : AIMessageChunk ( content = " hello world! " ) ,
" message " : AIMessageChunk (
content = " hello world! " , id = " ai1 "
) ,
" text " : " hello world! " ,
" type " : " ChatGenerationChunk " ,
}
@ -719,7 +729,7 @@ async def test_event_stream_with_simple_chain() -> None:
" tags " : [ " my_chain " , " my_model " , " seq:step:2 " ] ,
} ,
{
" data " : { " output " : AIMessageChunk ( content = " hello world! " )} ,
" data " : { " output " : AIMessageChunk ( content = " hello world! " , id = " ai1 " )} ,
" event " : " on_chain_end " ,
" metadata " : { " foo " : " bar " } ,
" name " : " my_chain " ,
@ -1332,8 +1342,8 @@ async def test_runnable_each() -> None:
async def test_events_astream_config ( ) - > None :
""" Test that astream events support accepting config """
infinite_cycle = cycle ( [ AIMessage ( content = " hello world! " )] )
good_world_on_repeat = cycle ( [ AIMessage ( content = " Goodbye world " )] )
infinite_cycle = cycle ( [ AIMessage ( content = " hello world! " , id = " ai1 " )] )
good_world_on_repeat = cycle ( [ AIMessage ( content = " Goodbye world " , id = " ai2 " )] )
model = GenericFakeChatModel ( messages = infinite_cycle ) . configurable_fields (
messages = ConfigurableField (
id = " messages " ,
@ -1343,7 +1353,7 @@ async def test_events_astream_config() -> None:
)
model_02 = model . with_config ( { " configurable " : { " messages " : good_world_on_repeat } } )
assert model_02 . invoke ( " hello " ) == AIMessage ( content = " Goodbye world " )
assert model_02 . invoke ( " hello " ) == AIMessage ( content = " Goodbye world " , id = " ai2 " )
events = await _collect_events ( model_02 . astream_events ( " hello " , version = " v1 " ) )
assert events == [
@ -1356,7 +1366,7 @@ async def test_events_astream_config() -> None:
" tags " : [ ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " Goodbye " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " Goodbye " , id = " ai2 " )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { } ,
" name " : " RunnableConfigurableFields " ,
@ -1364,7 +1374,7 @@ async def test_events_astream_config() -> None:
" tags " : [ ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " " , id = " ai2 " )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { } ,
" name " : " RunnableConfigurableFields " ,
@ -1372,7 +1382,7 @@ async def test_events_astream_config() -> None:
" tags " : [ ] ,
} ,
{
" data " : { " chunk " : AIMessageChunk ( content = " world " )} ,
" data " : { " chunk " : AIMessageChunk ( content = " world " , id = " ai2 " )} ,
" event " : " on_chat_model_stream " ,
" metadata " : { } ,
" name " : " RunnableConfigurableFields " ,
@ -1380,7 +1390,7 @@ async def test_events_astream_config() -> None:
" tags " : [ ] ,
} ,
{
" data " : { " output " : AIMessageChunk ( content = " Goodbye world " )} ,
" data " : { " output " : AIMessageChunk ( content = " Goodbye world " , id = " ai2 " )} ,
" event " : " on_chat_model_end " ,
" metadata " : { } ,
" name " : " RunnableConfigurableFields " ,
@ -1418,7 +1428,9 @@ async def test_runnable_with_message_history() -> None:
store [ session_id ] = [ ]
return InMemoryHistory ( messages = store [ session_id ] )
infinite_cycle = cycle ( [ AIMessage ( content = " hello " ) , AIMessage ( content = " world " ) ] )
infinite_cycle = cycle (
[ AIMessage ( content = " hello " , id = " ai3 " ) , AIMessage ( content = " world " , id = " ai4 " ) ]
)
prompt = ChatPromptTemplate . from_messages (
[
@ -1441,7 +1453,10 @@ async def test_runnable_with_message_history() -> None:
) . ainvoke ( { " question " : " hello " } )
assert store == {
" session-123 " : [ HumanMessage ( content = " hello " ) , AIMessage ( content = " hello " ) ]
" session-123 " : [
HumanMessage ( content = " hello " ) ,
AIMessage ( content = " hello " , id = " ai3 " ) ,
]
}
with_message_history . with_config (
@ -1450,8 +1465,8 @@ async def test_runnable_with_message_history() -> None:
assert store == {
" session-123 " : [
HumanMessage ( content = " hello " ) ,
AIMessage ( content = " hello " ),
AIMessage ( content = " hello " , id = " ai3 " ),
HumanMessage ( content = " meow " ) ,
AIMessage ( content = " world " ),
AIMessage ( content = " world " , id = " ai4 " ),
]
}