Skip to content

Commit 9ed90ab

Browse files
dhionson2hassiebp
andauthored
fix(langchain): clean up streaming run memo on error (#1978)
Co-authored-by: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com>
1 parent 38408cd commit 9ed90ab

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎langfuse/langchain/CallbackHandler.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,8 @@ def on_llm_error(
13691369
except Exception as e:
13701370
langfuse_logger.exception(e)
13711371
finally:
1372+
self._updated_completion_start_time_memo.discard(run_id)
1373+
13721374
if parent_run_id is None:
13731375
self._reset(run_id)
13741376

‎tests/unit/test_langchain.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
359404
def test_control_flow_resume_uses_thread_keyed_explicit_resume_context(
360405
memory_exporter, langfuse_memory_client, monkeypatch
361406
):

0 commit comments

Comments
 (0)