{"record":{"id":"642ece3b05ac8c8d","repo":"microsoft/autogen","slug":"inputrequestcontext-runtime-must-be-called-withi","errorCode":null,"errorMessage":"InputRequestContext.runtime() must be called within the input callback of a UserProxyAgent.","messagePattern":"InputRequestContext\\.runtime\\(\\) must be called within the input callback of a UserProxyAgent\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/agents/_user_proxy_agent.py","lineNumber":156,"sourceCode":"\n        _INPUT_REQUEST_CONTEXT_VAR: ClassVar[ContextVar[str]] = ContextVar(\"_INPUT_REQUEST_CONTEXT_VAR\")\n\n        @classmethod\n        @contextmanager\n        def populate_context(cls, ctx: str) -> Generator[None, Any, None]:\n            \"\"\":meta private:\"\"\"\n            token = UserProxyAgent.InputRequestContext._INPUT_REQUEST_CONTEXT_VAR.set(ctx)\n            try:\n                yield\n            finally:\n                UserProxyAgent.InputRequestContext._INPUT_REQUEST_CONTEXT_VAR.reset(token)\n\n        @classmethod\n        def request_id(cls) -> str:\n            try:\n                return cls._INPUT_REQUEST_CONTEXT_VAR.get()\n            except LookupError as e:\n                raise RuntimeError(\n                    \"InputRequestContext.runtime() must be called within the input callback of a UserProxyAgent.\"\n                ) from e\n\n    def __init__(\n        self,\n        name: str,\n        *,\n        description: str = \"A human user\",\n        input_func: Optional[InputFuncType] = None,\n    ) -> None:\n        \"\"\"Initialize the UserProxyAgent.\"\"\"\n        super().__init__(name=name, description=description)\n        self.input_func = input_func or cancellable_input\n        self._is_async = iscoroutinefunction(self.input_func)\n\n    @property\n    def produced_message_types(self) -> Sequence[type[BaseChatMessage]]:\n        \"\"\"Message types this agent can produce.\"\"\"","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_user_proxy_agent.py#L138-L174","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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'."],"exampleFix":"// before\nrid = UserProxyAgent.InputRequestContext.request_id()  # outside callback -> raises\n\n// after\nasync def my_input(prompt: str, ct: CancellationToken | None) -> str:\n    rid = UserProxyAgent.InputRequestContext.request_id()  # inside callback: ok\n    return await ui.get_input(rid, prompt)","handlingStrategy":"try-catch","validationCode":"# Read the id only inside the callback\nasync def input_func(prompt: str, ct) -> str:\n    try:\n        rid = UserProxyAgent.InputRequestContext.request_id()\n    except RuntimeError:\n        rid = \"unavailable\"\n    return await ui.get_input(rid, prompt)","typeGuard":null,"tryCatchPattern":"try:\n    rid = UserProxyAgent.InputRequestContext.request_id()\nexcept RuntimeError as e:\n    if \"must be called within the input callback\" in str(e):\n        rid = None  # no active input request; use a fallback identifier\n    else:\n        raise","preventionTips":["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."],"tags":["user-proxy","context","input-callback","misuse"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}