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%LangchainAffected 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