bug(langchain): failed streaming LLM runs retain IDs in callback memo
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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 therun_idto_updated_completion_start_time_memo.on_llm_end()removes therun_idin itsfinallyblock.on_llm_error()ends the observation but does not remove therun_idfrom the memo._detach_observation()and_reset()do not clear this set.
Steps to reproduce
Using a LangchainCallbackHandler with an in-memory Langfuse test client:
- Start an LLM run using
on_llm_start(). - Simulate the first streamed token using
on_llm_new_token(). - Confirm the run ID exists in
_updated_completion_start_time_memo. - Terminate the run using
on_llm_error(RuntimeError("synthetic failure"), run_id=run_id). - 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.pypassed. - Targeted Ruff lint and formatting checks passed.
git diff --checkpassed.
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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của langfuse/langfuse-python
-
tests/unit/test_prompt.py fails on a fresh clone without LANGFUSE_* env varsCó thể đã có người làm @Mathoholic đã nhận 4 ngày trước. Đang mởbug feat-prompt-management sdk-python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
langfuse/langfuse-python#1976 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug: ChatPromptClient.compile appends str(whole list) once per non-dict placeholder itemCó thể đã có người làm @hassiebp đã nhận 6 ngày trước. Đang mởbug feat-prompt-management sdk-python
langfuse/langfuse-python#1971 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug: GCS upload detection in MediaManager uses substring match on the full URLCó thể đã có người làm @hassiebp đã nhận 15 ngày trước. Đang mởbug feat-multimodal-media sdk-python
langfuse/langfuse-python#1913 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
mask is not applied to create_dataset_item or create_score(comment=)Có thể đã có người làm @hassiebp đã nhận 23 ngày trước. Đang mởbug compliance feat-data-masking sdk-python security
langfuse/langfuse-python#1896 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[HTTPXodus] Consider migrating from httpx to httpx2 (the actively maintained fork)Có thể đã có người làm @hassiebp đã nhận 42 ngày trước. Đang mởimprovement sdk-python
langfuse/langfuse-python#1856 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của langfuse/langfuse-python
Issue tương tự
-
documentation good first issue
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 72/100
sujithputta02/Esapay#13 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Flaky: credential-proxy admission metric test asserts a wall-clock bound it does not establishĐang mởneeds-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
gke-labs/kube-agents#3277 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
jbacalso24/mimry#291 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
good first issue
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 82/100
eli-alvarado/us-watchlist-alerts#14 ·
Maintainer thường phản hồi trong vòng 1 ngày