fix(langchain): handle empty llm generations - #1839
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: arpan sahu <28574248+arpansahu@users.noreply.github.com>
|
|
|
Disclosure: this contribution was prepared with AI assistance (GitHub Copilot CLI), and The defect, the fix and the regression test were verified locally: the new test fails on If your project would prefer not to take AI-assisted contributions, or you would rather this |
Problem
CallbackHandler.on_llm_end()assumes every LangChainLLMResultcontains at least one generation in the last generation batch. If a provider returns an empty generation batch but still includes model and usage metadata, the callback raisesIndexError, logs the error, and never ends the generation span.Reproducer
Fix
Extract the last available generation by skipping empty generation batches, and use
Noneas the output when no generation is present. This keeps the span lifecycle correct while preserving existing output extraction for normal chat and text generations.Testing
python -m pytest tests\\unit\\test_langchain.py::test_llm_callback_ends_generation_with_empty_generations -qfailed before the fix and passed after.python -m pytest tests\\unit\\test_langchain.py -q-> 24 passed.ruff format langfuse\\langchain\\CallbackHandler.py tests\\unit\\test_langchain.py --check-> passed after formatting.ruff check langfuse\\langchain\\CallbackHandler.py tests\\unit\\test_langchain.py-> All checks passed.Note: full offline
tests\\unitbaseline still has pre-existing Windows/environment failures unrelated to this change (test_path, prompt subprocess missingopentelemetry, and prompt mock errors).Greptile Summary
The PR makes LangChain LLM completion handling tolerate empty generation batches so generation spans can still finish with model and usage metadata.
Nonewhen none exists.Confidence Score: 5/5
The PR appears safe to merge with no actionable correctness, security, or quality issues identified.
The empty-result path now avoids indexing an empty batch, safely passes
Nonethrough the observation update, preserves model and usage metadata, and ends the generation span.Reviews (1): Last reviewed commit: "fix(langchain): handle empty llm generat..." | Re-trigger Greptile