Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

bug(langchain): failed streaming LLM runs retain IDs in callback memo

Đang mở
#1,977 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@hassiebp đang làm issue này rồi.

Từ ngày 11/10/2026.

  • #1978 của @dhionson2 — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

bug integration-langchain sdk-python

Bug description

LangchainCallbackHandler retains completed LLM run IDs in _updated_completion_start_time_memo when a streaming execution terminates with an error.

The successful completion path (on_llm_end) removes the run ID from the set, but the error path (on_llm_error) does not perform the same cleanup.

As a result, long-lived callback handlers reused across failed streaming executions can accumulate stale UUID entries.

Root cause

In langfuse/langchain/CallbackHandler.py:

  • on_llm_new_token() records the first-token timestamp and adds the run_id to _updated_completion_start_time_memo.
  • on_llm_end() removes the run_id in its finally block.
  • on_llm_error() ends the observation but does not remove the run_id from the memo.
  • _detach_observation() and _reset() do not clear this set.

Steps to reproduce

Using a LangchainCallbackHandler with an in-memory Langfuse test client:

  1. Start an LLM run using on_llm_start().
  2. Simulate the first streamed token using on_llm_new_token().
  3. Confirm the run ID exists in _updated_completion_start_time_memo.
  4. Terminate the run using on_llm_error(RuntimeError("synthetic failure"), run_id=run_id).
  5. Inspect _updated_completion_start_time_memo.

Actual behavior: The failed run ID remains in the set after the observation has ended.

Expected behavior: The failed run ID should be removed, while unrelated active run IDs remain untouched.

Impact

This causes incremental retention of UUID identifiers when streaming LLM executions fail and the same callback handler is reused.

The observed retention is limited to UUID entries. There is no evidence of retained generations, spans, or a significant process-level memory leak.

Proposed solution

Ensure on_llm_error() discards its own run_id from _updated_completion_start_time_memo in a finally block, mirroring the existing cleanup in on_llm_end().

This should preserve:

  • Active nested or concurrent run state.
  • Existing OpenTelemetry context handling.
  • LangGraph control-flow and resume behavior.
  • Current public SDK behavior.

Validation

I reproduced this issue locally using Langfuse Python SDK 4.17.0 and created a deterministic regression test.

Before the fix (RED):

  • The failed run ID remained in the memo.
  • The regression assertion failed as expected.

After the minimal fix (GREEN):

  • The failed run ID was removed.
  • Another active run ID remained unaffected.
  • All 24 tests in tests/unit/test_langchain.py passed.
  • Targeted Ruff lint and formatting checks passed.
  • git diff --check passed.

Full unit suite on Windows:

  • 883 passed
  • 2 skipped
  • 3 failed in unrelated tests (two subprocess import failures and one platform-dependent path serialization assertion).

The full suite has not yet been confirmed green on Linux.

Contribution

I have a minimal fix and regression test ready locally.

I'm happy to open a focused PR if the maintainers agree with the proposed approach.

Ngôn ngữ chính
Python
Star
500
Fork
367
Merge trung bình
15 giờ 9 phút
Pull request đã merge (30 ngày)
53

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của langfuse/langfuse-python

Tất cả issue của langfuse/langfuse-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.