{"record":{"id":"d29f780324ec9068","repo":"microsoft/autogen","slug":"agentinstantiationcontext-cannot-be-instantiated","errorCode":null,"errorMessage":"AgentInstantiationContext cannot be instantiated. It is a static class that provides context management for agent instantiation.","messagePattern":"AgentInstantiationContext cannot be instantiated\\. It is a static class that provides context management for agent instantiation\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_agent_instantiation.py","lineNumber":86,"sourceCode":"                # Start the runtime.\n                runtime.start()\n\n                # Register the agent type with a factory function.\n                await runtime.register_factory(\"test_agent\", test_agent_factory)\n\n                # Send a message to the agent. The runtime will instantiate the agent and call the message handler.\n                await runtime.send_message(TestMessage(content=\"Hello, world!\"), AgentId(\"test_agent\", \"default\"))\n\n                # Stop the runtime.\n                await runtime.stop()\n\n\n            asyncio.run(main())\n\n    \"\"\"\n\n    def __init__(self) -> None:\n        raise RuntimeError(\n            \"AgentInstantiationContext cannot be instantiated. It is a static class that provides context management for agent instantiation.\"\n        )\n\n    _AGENT_INSTANTIATION_CONTEXT_VAR: ClassVar[ContextVar[tuple[AgentRuntime, AgentId]]] = ContextVar(\n        \"_AGENT_INSTANTIATION_CONTEXT_VAR\"\n    )\n\n    @classmethod\n    @contextmanager\n    def populate_context(cls, ctx: tuple[AgentRuntime, AgentId]) -> Generator[None, Any, None]:\n        \"\"\":meta private:\"\"\"\n        token = AgentInstantiationContext._AGENT_INSTANTIATION_CONTEXT_VAR.set(ctx)\n        try:\n            yield\n        finally:\n            AgentInstantiationContext._AGENT_INSTANTIATION_CONTEXT_VAR.reset(token)\n\n    @classmethod","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_agent_instantiation.py#L68-L104","documentation":"AgentInstantiationContext is a static utility class whose __init__ deliberately raises RuntimeError. It exists only to expose classmethods (current_runtime, current_agent_id, populate_context, is_in_factory_call) that read a ContextVar holding the (runtime, agent_id) pair active during agent instantiation. It can never be meaningfully instantiated.","triggerScenarios":"Writing AgentInstantiationContext() anywhere — e.g. trying to inject or mock it, or copy-pasting a class reference into a constructor call. The guard fires unconditionally on any instantiation attempt.","commonSituations":"Developers new to the framework treating the class like a service object to pass around; test code attempting to build a context object manually instead of using the runtime's register/instantiate path.","solutions":["Remove the instantiation; call the classmethods directly: AgentInstantiationContext.current_runtime(), .current_agent_id(), .is_in_factory_call().","If you need to set up a context (e.g. in tests or a custom runtime), use AgentInstantiationContext.populate_context((runtime, agent_id)) as a context manager.","In tests, prefer registering an agent factory with a real runtime (SingleThreadedAgentRuntime) so the context is populated for you."],"exampleFix":"# before\nctx = AgentInstantiationContext()  # RuntimeError\n\n# after\nwith AgentInstantiationContext.populate_context((runtime, agent_id)):\n    rt = AgentInstantiationContext.current_runtime()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat AgentInstantiationContext as a namespace only: never instantiate, only call its classmethods.","Use linters/type checkers to flag obvious constructor calls on static utility classes.","In tests, set up context via populate_context((runtime, agent_id))."],"tags":["autogen-core","static-class","api-misuse","instantiation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}