bug(langchain): failed streaming LLM runs retain IDs in callback memo
Mantenedores costumam responder em até 1 dia
Avaliação
Esta issue ainda não foi avaliada.
Descrição
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.
- Linguagem predominante
- Python
- Estrelas
- 500
- Forks
- 367
- Merge médio
- 15h 9min
- PRs com merge (30d)
- 53
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de langfuse/langfuse-python
-
tests/unit/test_prompt.py fails on a fresh clone without LANGFUSE_* env varsTalvez já em andamento @Mathoholic assumiu há 3 dias. Abertabug feat-prompt-management sdk-python
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 80/100
langfuse/langfuse-python#1976 · 3 comentários ·
Mantenedores costumam responder em até 1 dia
-
bug: ChatPromptClient.compile appends str(whole list) once per non-dict placeholder itemTalvez já em andamento @hassiebp assumiu há 5 dias. Abertabug feat-prompt-management sdk-python
langfuse/langfuse-python#1971 · 1 responsável ·
Mantenedores costumam responder em até 1 dia
-
bug: GCS upload detection in MediaManager uses substring match on the full URLTalvez já em andamento @hassiebp assumiu há 14 dias. Abertabug feat-multimodal-media sdk-python
langfuse/langfuse-python#1913 · 1 comentário · 1 responsável ·
Mantenedores costumam responder em até 1 dia
-
mask is not applied to create_dataset_item or create_score(comment=)Talvez já em andamento @hassiebp assumiu há 21 dias. Abertabug compliance feat-data-masking sdk-python security
langfuse/langfuse-python#1896 · 1 comentário · 1 responsável ·
Mantenedores costumam responder em até 1 dia
-
[HTTPXodus] Consider migrating from httpx to httpx2 (the actively maintained fork)Talvez já em andamento @hassiebp assumiu há 40 dias. Abertaimprovement sdk-python
langfuse/langfuse-python#1856 · 1 responsável ·
Mantenedores costumam responder em até 1 dia
Todas as issues de langfuse/langfuse-python
Issues semelhantes
-
customer-reported question
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
Azure/azure-sdk-for-python#49548 · 1 comentário · 1 reação ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 88/100
btclib-org/btclib-wallet#409 ·
Mantenedores costumam responder em até 1 dia
-
Phase 2 PSA Capability & Career Profile
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
bcgov/digital-journeys#2310 ·
-
bug good first issue
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
fastmachinelearning/hls4ml#1582 ·
Mantenedores costumam responder em até 3 dias
-
Sort modelsAbertaenhancement good first issue
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
Mantenedores costumam responder em até 1 dia