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

@Task Awaited Inside StateGraph Node Returns None Under Astream_events

When a @task is awaited inside a StateGraph node, invoking the graph with astream_events (or stream_mode=events) returns None for the task's result instead of the actual value. The same graph works correctly under ainvoke and astream. The root cause is that arun_with_retry drains the task stream but discards the RETURN channel write, so the freshly scheduled task future resolves to None.

highConfidence 92%LanggraphConditional EdgesState ManagementHuman-in-the-loopAffected V1.2.12Affected V1.2.14Affected Vmain

Origin Analysis

In langgraph/pregel/_retry.py, arun_with_retry(..., stream=True) consumes task.proc.astream() with an async for loop and returns nothing, causing the coroutine to resolve to None. Meanwhile, in langgraph/pregel/_runner.py, _acall_impl schedules fresh tasks via chain_future(fut, destination), so the destination receives arun_with_retry's return value (None) instead of reading the task's RETURN channel from task.writes. The already-run task branch correctly reads task.writes[RETURN], but the fresh-schedule branch does not.
1. Define an async @task function that returns a value.\n2. Define a StateGraph node that awaits that task and returns a dict with the result.\n3. Compile the graph with a checkpointer.\n4. Run graph.ainvoke -> returns {'out': 42}.\n5. Run graph.astream_events(..., version='v2') and observe the final on_chain_end event output -> returns {'out': None} instead of {'out': 42}.

Fixing Code Block

Edge Case Audit

This change only addresses fresh tasks scheduled with stream=True. If a task returns None explicitly, the RETURN channel still contains None, so the next() expression will return None, preserving current behavior. However, if no RETURN write is present (e.g., a task that raises or is interrupted), MISSING will be returned, which may require downstream code to handle MISSING consistently. Ensure that all callers of arun_with_retry treat MISSING as a sentinel and not as a valid value. Rollback: revert this patch to restore previous behavior. Also verify concurrent execution and retry scenarios, as task.writes may be mutated by multiple writers.

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