{"record":{"id":"f264d4904d6bdc1b","repo":"microsoft/semantic-kernel","slug":"subscriptioninstantiationcontext-cannot-be-instant","errorCode":null,"errorMessage":"SubscriptionInstantiationContext cannot be instantiated. It is a static class that provides context management for subscription instantiation.","messagePattern":"SubscriptionInstantiationContext cannot be instantiated\\. It is a static class that provides context management for subscription instantiation\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/runtime/in_process/subscription_context.py","lineNumber":18,"sourceCode":"# Copyright (c) Microsoft. All rights reserved.\n\nfrom collections.abc import Generator\nfrom contextlib import contextmanager\nfrom contextvars import ContextVar\nfrom typing import Any, ClassVar\n\nfrom semantic_kernel.agents.runtime.core.agent_type import AgentType\nfrom semantic_kernel.utils.feature_stage_decorator import experimental\n\n\n@experimental\nclass SubscriptionInstantiationContext:\n    \"\"\"Context manager for subscription instantiation.\"\"\"\n\n    def __init__(self) -> None:\n        \"\"\"Prevent instantiation of SubscriptionInstantiationContext.\"\"\"\n        raise RuntimeError(\n            \"SubscriptionInstantiationContext cannot be instantiated. It is a static class that provides context \"\n            \"management for subscription instantiation.\"\n        )\n\n    _SUBSCRIPTION_CONTEXT_VAR: ClassVar[ContextVar[AgentType]] = ContextVar(\"_SUBSCRIPTION_CONTEXT_VAR\")\n\n    @classmethod\n    @contextmanager\n    def populate_context(cls, ctx: AgentType) -> Generator[None, Any, None]:\n        \"\"\"Populate the context with the agent type.\"\"\"\n        token = SubscriptionInstantiationContext._SUBSCRIPTION_CONTEXT_VAR.set(ctx)\n        try:\n            yield\n        finally:\n            SubscriptionInstantiationContext._SUBSCRIPTION_CONTEXT_VAR.reset(token)\n\n    @classmethod\n    def agent_type(cls) -> AgentType:","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/in_process/subscription_context.py#L1-L36","documentation":"SubscriptionInstantiationContext is a static-only helper that holds a ContextVar carrying the agent type during runtime-driven agent instantiation. Its __init__ deliberately raises RuntimeError to forbid instances; all useful behavior is exposed through the classmethods populate_context and agent_type.","triggerScenarios":"Writing `SubscriptionInstantiationContext()` anywhere in application or test code. The constructor unconditionally raises before any state is set.","commonSituations":"Confusing the class with a normal context-manager instance you hold a reference to; copy-pasting a pattern that expects `ctx = SomeContext()`; IDE auto-completing the class as a constructor.","solutions":["Never instantiate it; call the classmethods directly, e.g. `with SubscriptionInstantiationContext.populate_context(agent_type): ...`","If you need the current agent type, read it via `SubscriptionInstantiationContext.agent_type()` (only valid inside an instantiation context).","Let the AgentRuntime own all interaction with this class during agent registration/instantiation."],"exampleFix":"// before\nctx = SubscriptionInstantiationContext()  # raises RuntimeError\n\n// after\n# static class - use classmethods only\nwith SubscriptionInstantiationContext.populate_context(agent_type):\n    current = SubscriptionInstantiationContext.agent_type()","handlingStrategy":"validation","validationCode":"# This class is never instantiated. Validate at review time that no code calls SubscriptionInstantiationContext().\nimport ast\nsrc = open(path).read()\nfor node in ast.walk(ast.parse(src)):\n    if isinstance(node, ast.Call) and getattr(node.func, 'id', '') == 'SubscriptionInstantiationContext':\n        raise AssertionError('SubscriptionInstantiationContext must not be instantiated')","typeGuard":"from typing import Type\n# no instance guard is meaningful; the class is used only by classmethod.\n# Treat any value claimed to be an instance as a bug.\ndef _assert_not_instance(value: object) -> None:\n    assert not isinstance(value, SubscriptionInstantiationContext)","tryCatchPattern":null,"preventionTips":["Treat SubscriptionInstantiationContext as a static utility","Call only populate_context / agent_type classmethods","Let the runtime manage this class during instantiation"],"tags":["runtime","agent-runtime","api-misuse","context"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}