AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Inconsistent `Generations` Shape In Chat Model Streaming `On_llm_error` Callbacks

BaseChatModel.stream() and astream() emit malformed LLMResult.generations to on_llm_error: when no chunks are produced, generations is [[]]; when partial chunks exist, it is [[chunk], []] instead of the correct [] or [[chunk]] respectively.

highConfidence 95%Langchain-CoreAffected V1.4.8

Origin Analysis

PR #30626 introduced _generate_response_from_error(e), which returns a flat list of ChatGeneration. The stream/astream exception handlers wrap this list unconditionally in an outer list, then prepend accumulated chunks if present. This yields an extra empty inner list when the error has no HTTP response (common case), producing [[]] with no chunks or [[chunk], []] with partial chunks.
Provided in issue: run the FakeListChatModel loop with error_on_chunk_number values 0-3; observe generations shape from callback. See inconsistency.

Fixing Code Block

Edge Case Audit

This changes the shape of the callback payload in edge cases. Downstream consumers that have adapted to the malformed shape (e.g., expecting an extra empty row) may break. Ensure all async paths are updated identically; otherwise sync and async behavior diverges. Thoroughly test with integrations that raise HTTP errors (which may provide error metadata). Rollback: revert to previous unconditional wrapping if regressions appear, but this perpetuates the bug.

Ecosystem Topology