microsoft/autogen · error · RuntimeError
InputRequestContext.runtime() must be called within the inpu
Error message
InputRequestContext.runtime() must be called within the input callback of a UserProxyAgent.
What it means
InputRequestContext.request_id() reads a ContextVar that is only set while UserProxyAgent is inside its input callback (via populate_context). Calling it outside that dynamic scope raises a LookupError wrapped in this RuntimeError, because there is no active input request to identify.
Source
Thrown at python/packages/autogen-agentchat/src/autogen_agentchat/agents/_user_proxy_agent.py:156
_INPUT_REQUEST_CONTEXT_VAR: ClassVar[ContextVar[str]] = ContextVar("_INPUT_REQUEST_CONTEXT_VAR")
@classmethod
@contextmanager
def populate_context(cls, ctx: str) -> Generator[None, Any, None]:
""":meta private:"""
token = UserProxyAgent.InputRequestContext._INPUT_REQUEST_CONTEXT_VAR.set(ctx)
try:
yield
finally:
UserProxyAgent.InputRequestContext._INPUT_REQUEST_CONTEXT_VAR.reset(token)
@classmethod
def request_id(cls) -> str:
try:
return cls._INPUT_REQUEST_CONTEXT_VAR.get()
except LookupError as e:
raise RuntimeError(
"InputRequestContext.runtime() must be called within the input callback of a UserProxyAgent."
) from e
def __init__(
self,
name: str,
*,
description: str = "A human user",
input_func: Optional[InputFuncType] = None,
) -> None:
"""Initialize the UserProxyAgent."""
super().__init__(name=name, description=description)
self.input_func = input_func or cancellable_input
self._is_async = iscoroutinefunction(self.input_func)
@property
def produced_message_types(self) -> Sequence[type[BaseChatMessage]]:
"""Message types this agent can produce."""View on GitHub (pinned to 027ecf0a37)
Solutions
- Move the request_id() call inside the input_func callback passed to UserProxyAgent, or inside a populate_context block.
- Pass the request id explicitly to components that run outside the callback instead of reading the context var.
- Guard usage with try/except RuntimeError and treat it as 'no active input request'.
Example fix
// before
rid = UserProxyAgent.InputRequestContext.request_id() # outside callback -> raises
// after
async def my_input(prompt: str, ct: CancellationToken | None) -> str:
rid = UserProxyAgent.InputRequestContext.request_id() # inside callback: ok
return await ui.get_input(rid, prompt) Defensive patterns
Strategy: try-catch
Validate before calling
# Read the id only inside the callback
async def input_func(prompt: str, ct) -> str:
try:
rid = UserProxyAgent.InputRequestContext.request_id()
except RuntimeError:
rid = "unavailable"
return await ui.get_input(rid, prompt) Try / catch
try:
rid = UserProxyAgent.InputRequestContext.request_id()
except RuntimeError as e:
if "must be called within the input callback" in str(e):
rid = None # no active input request; use a fallback identifier
else:
raise Prevention
- Call request_id() only within input_func or a populate_context block.
- Pass the request id explicitly to out-of-band UI components instead of reading the ContextVar.
When it happens
Trigger: Calling InputRequestContext.request_id() (or a helper that uses it, e.g. runtime UI bindings) from arbitrary code outside the input_func callback, or before the agent has requested input.
Common situations: UI integrations (e.g. web frontends) that fetch the runtime request id eagerly at startup or in background tasks instead of inside the input callback thread/task that UserProxyAgent invoked.
Related errors
- InputRequestContext cannot be instantiated. It is a static c
- Failed to get user input: {str(e)}
- Installing directory is not set
- useAuth must be used within an AuthProvider
- Handoff message target does not match agent name: {messages[
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/642ece3b05ac8c8d.
Report an issue: GitHub.