{"record":{"id":"ffe35ff4643dd828","repo":"microsoft/semantic-kernel","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/semantic_kernel/agents/runtime/in_process/agent_instantiation_context.py","lineNumber":24,"sourceCode":"from typing import Any, ClassVar\n\nfrom semantic_kernel.agents.runtime.core.agent_id import AgentId\nfrom semantic_kernel.agents.runtime.core.core_runtime import CoreRuntime\nfrom semantic_kernel.utils.feature_stage_decorator import experimental\n\n\n@experimental\nclass AgentInstantiationContext:\n    \"\"\"A static class that provides context for agent instantiation.\n\n    This static class can be used to access the current runtime and agent ID\n    during agent instantiation -- inside the factory function or the agent's\n    class constructor.\n    \"\"\"\n\n    def __init__(self) -> None:\n        \"\"\"Instantiate the AgentInstantiationContext class.\"\"\"\n        raise RuntimeError(\n            \"AgentInstantiationContext cannot be instantiated. It is a static class that provides context management \"\n            \"for agent instantiation.\"\n        )\n\n    _AGENT_INSTANTIATION_CONTEXT_VAR: ClassVar[ContextVar[tuple[CoreRuntime, AgentId]]] = ContextVar(\n        \"_AGENT_INSTANTIATION_CONTEXT_VAR\"\n    )\n\n    @classmethod\n    @contextmanager\n    def populate_context(cls, ctx: tuple[CoreRuntime, AgentId]) -> Generator[None, Any, None]:\n        \"\"\"Populate the context with the current runtime and agent ID.\"\"\"\n        token = AgentInstantiationContext._AGENT_INSTANTIATION_CONTEXT_VAR.set(ctx)\n        try:\n            yield\n        finally:\n            AgentInstantiationContext._AGENT_INSTANTIATION_CONTEXT_VAR.reset(token)\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/in_process/agent_instantiation_context.py#L6-L42","documentation":"AgentInstantiationContext is a static utility class that provides access to the current runtime and agent ID during agent instantiation via a ContextVar. It is not meant to be instantiated directly — its __init__ unconditionally raises RuntimeError. The class exposes classmethods (current_runtime, current_agent_id) and a context manager (populate_context) for internal use by the runtime.","triggerScenarios":"Calling AgentInstantiationContext() directly, e.g. `ctx = AgentInstantiationContext()`. The __init__ method raises immediately before any instance state is set.","commonSituations":"A developer discovers the class name and assumes it is a normal dataclass or context object, attempting to construct it to pass around agent context. Common when migrating from a pattern where context objects are injected manually.","solutions":["Do not instantiate the class. Access context through the classmethods: AgentInstantiationContext.current_runtime() and AgentInstantiationContext.current_agent_id().","If you need agent context inside an agent's __init__ or factory function, call the classmethods directly — the runtime sets the ContextVar for you during instantiation."],"exampleFix":"// before\nctx = AgentInstantiationContext()\n\n// after\nruntime = AgentInstantiationContext.current_runtime()\nagent_id = AgentInstantiationContext.current_agent_id()","handlingStrategy":"validation","validationCode":"# This is a static class — never instantiate it.\n# Validate by checking the class, not an instance:\nfrom semantic_kernel.agents.runtime.in_process.agent_instantiation_context import AgentInstantiationContext\n# Correct: use classmethods directly\nruntime = AgentInstantiationContext.current_runtime()\nagent_id = AgentInstantiationContext.current_agent_id()","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Treat AgentInstantiationContext as a namespace/static accessor, never as a constructor.","Look for @classmethod and @contextmanager decorators — their presence signals static-only usage."],"tags":["agent-runtime","static-class","instantiation","contextvar","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}