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

HumanInTheLoopMiddleware Async Agent Ainvoke Fails With RuntimeError: Called Get_config Outside Of A Runnable Context

When using HumanInTheLoopMiddleware with an async agent (agent.ainvoke), the middleware's aafter_model method delegates to synchronous after_model, which calls interrupt(). In the async execution path, the LangGraph config context variable is not set, causing get_config() to raise RuntimeError, breaking the approval flow.

highConfidence 88%LangchainChainslangchain-coreAffected V1.2.4Affected V1.2.7

Origin Analysis

In async LangGraph execution, the config contextvar is not propagated into the async middleware hook (aafter_model). HumanInTheLoopMiddleware.aafter_model directly calls synchronous after_model, which invokes langgraph.types.interrupt; interrupt relies on get_config() to read the current config from the contextvar. Because the contextvar is unset in the aafter_model context, get_config() raises 'Called get_config outside of a runnable context'.
1. Create an agent with create_agent, add HumanInTheLoopMiddleware with interrupt_on for a tool, use InMemorySaver. 2. Invoke the agent asynchronously with agent.ainvoke({'messages':[{'role':'user','content':'Delete old records'}]}, config={'configurable':{'thread_id':'some_id'}}). 3. Observe RuntimeError in langgraph/config.py get_config.

Fixing Code Block

Edge Case Audit

This workaround relies on the internal langgraph.config.set_config API, which may change across LangGraph versions. It does not fix the underlying async context propagation in LangGraph; concurrent tasks or threads may still see context leakage if multiple configs are active. Ensure each agent run uses a separate thread_id and persistent checkpointer. To roll back, remove the subclass and use the original HumanInTheLoopMiddleware once LangGraph fixed the context propagation; otherwise, monitor for new RuntimeErrors in other middleware that call get_config.

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