{"record":{"id":"3fb27ed08448c080","repo":"microsoft/autogen","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/packages/autogen-core/src/autogen_core/_subscription_context.py","lineNumber":10,"sourceCode":"from contextlib import contextmanager\nfrom contextvars import ContextVar\nfrom typing import Any, ClassVar, Generator\n\nfrom ._agent_type import AgentType\n\n\nclass SubscriptionInstantiationContext:\n    def __init__(self) -> None:\n        raise RuntimeError(\n            \"SubscriptionInstantiationContext cannot be instantiated. It is a static class that provides context 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        \"\"\":meta private:\"\"\"\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:\n        try:","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_subscription_context.py#L1-L28","documentation":"SubscriptionInstantiationContext is a static utility class whose __init__ unconditionally raises RuntimeError. It exists only to carry two class-level members: the populate_context() context manager and the agent_type() accessor, both backed by a module-level ContextVar. Instantiating it is always a programming error, never a runtime condition to recover from.","triggerScenarios":"Any expression `SubscriptionInstantiationContext()` — directly, via copy.deepcopy/pickle that reconstructs objects, or through a framework that reflectively instantiates every class it discovers.","commonSituations":"A developer assumes from the class name that it holds per-subscription state and tries to construct one; IDE auto-complete generates a constructor call; generic serialization/copy machinery instantiates the class reflectively.","solutions":["Delete the constructor call — the class is used only through its classmethods.","To set up a context manually (e.g. in tests), use `with SubscriptionInstantiationContext.populate_context(AgentType(\"my_agent\")): ...`.","To read the current agent type inside that context, call `SubscriptionInstantiationContext.agent_type()`.","If a copy/pickle framework triggered it, exclude this class from serialization."],"exampleFix":"# before\nctx = SubscriptionInstantiationContext()  # RuntimeError\n\n# after\nfrom autogen_core import AgentType\nwith SubscriptionInstantiationContext.populate_context(AgentType(\"my_agent\")):\n    current = SubscriptionInstantiationContext.agent_type()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"# Not a value type — the guard is simply: never call the constructor.\n# Use only: SubscriptionInstantiationContext.populate_context(...) and .agent_type()","tryCatchPattern":null,"preventionTips":["Treat SubscriptionInstantiationContext as a module-level namespace; call classmethods on the class object.","Exclude infrastructure classes from pickling/copying frameworks."],"tags":["python","autogen-core","subscriptions","api-misuse","static-class"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}