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-CoreMemoryEmbeddingsChainsAffected 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

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state