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

Astream_events V2 Does Not Emit Terminal Error Events For Failed Runs

The v2 implementation of astream_events fails to emit terminal error events (on_chain_error, on_llm_error, on_chat_model_error, on_retriever_error) when a tracked run raises an exception. Only the corresponding start event is emitted, leaving consumers unable to reliably determine that a run terminated unsuccessfully.

highConfidence 85%Langchain-CoreAffected V1.4.8

Origin Analysis

In the astream_events v2 callback handler, the error-handling methods (on_chain_error, on_llm_error, on_chat_model_error, on_retriever_error) are missing or not connected to the event emission and run-tracking cleanup logic. As a result, when an exception propagates, no terminal error event is sent and the failed run remains in the internal run map.
Run the provided Python snippet:\n```python\nimport asyncio\nfrom langchain_core.runnables import RunnableLambda\n\ndef fail(_: str) -> str:\n raise ValueError(\"boom\")\n\nasync def main() -> None:\n chain = RunnableLambda(fail)\n events = []\n try:\n async for event in chain.astream_events(\"hello\", version=\"v2\"):\n events.append(event[\"event\"])\n except ValueError:\n pass\n print(events)\nasyncio.run(main())\n```\nObserve that the output contains only `on_chain_start` and no `on_chain_error`.

Fixing Code Block

Edge Case Audit

This patch assumes the handler class already defines `_send_event` and `_remove_run`. If those methods have different signatures or side effects, adapt accordingly. In concurrent execution, race conditions may occur if the same run_id is processed by multiple callbacks; ensure the run map is thread/async-safe (e.g., use a lock or async-safe data structure). Additionally, downstream consumers must handle serialization of the `error` object in the event payload; the error may contain non-serializable fields. Before deploying, test with multi-threaded and fallback scenarios. Rollback: keep a backup of the original `event_stream.py` and revert if any unexpected event duplication or missing cleanup occurs.

Ecosystem Topology