@@ -356,6 +356,51 @@ class DummyControlFlowError(RuntimeError):
356356 )
357357
358358
359+ def test_streamed_llm_error_cleans_only_its_completion_start_time_memo (
360+ langfuse_memory_client ,
361+ ):
362+ handler = CallbackHandler ()
363+ failed_run_id = uuid4 ()
364+ active_run_id = uuid4 ()
365+ failed_context = copy_context ()
366+ active_context = copy_context ()
367+
368+ for context , run_id in (
369+ (failed_context , failed_run_id ),
370+ (active_context , active_run_id ),
371+ ):
372+ context .run (
373+ handler .on_llm_start ,
374+ {"name" : "TestLLM" },
375+ ["prompt" ],
376+ run_id = run_id ,
377+ metadata = {"ls_model_name" : "test-model" },
378+ invocation_params = {},
379+ )
380+ context .run (handler .on_llm_new_token , "first token" , run_id = run_id )
381+
382+ response = LLMResult (generations = [[Generation (text = "done" )]], llm_output = {})
383+
384+ try :
385+ assert failed_run_id in handler ._updated_completion_start_time_memo
386+ assert active_run_id in handler ._updated_completion_start_time_memo
387+
388+ failed_context .run (
389+ handler .on_llm_error ,
390+ RuntimeError ("synthetic failure" ),
391+ run_id = failed_run_id ,
392+ )
393+
394+ assert active_run_id in handler ._updated_completion_start_time_memo
395+ assert failed_run_id not in handler ._updated_completion_start_time_memo
396+ finally :
397+ active_context .run (
398+ handler .on_llm_end ,
399+ response ,
400+ run_id = active_run_id ,
401+ )
402+
403+
359404def test_control_flow_resume_uses_thread_keyed_explicit_resume_context (
360405 memory_exporter , langfuse_memory_client , monkeypatch
361406):
0 commit comments