microsoft/autogen · error · RuntimeError

SubscriptionInstantiationContext cannot be instantiated. It

Error message

SubscriptionInstantiationContext cannot be instantiated. It is a static class that provides context management for subscription instantiation.

What it means

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.

Source

Thrown at python/packages/autogen-core/src/autogen_core/_subscription_context.py:10

from contextlib import contextmanager
from contextvars import ContextVar
from typing import Any, ClassVar, Generator

from ._agent_type import AgentType


class SubscriptionInstantiationContext:
    def __init__(self) -> None:
        raise RuntimeError(
            "SubscriptionInstantiationContext cannot be instantiated. It is a static class that provides context management for subscription instantiation."
        )

    _SUBSCRIPTION_CONTEXT_VAR: ClassVar[ContextVar[AgentType]] = ContextVar("_SUBSCRIPTION_CONTEXT_VAR")

    @classmethod
    @contextmanager
    def populate_context(cls, ctx: AgentType) -> Generator[None, Any, None]:
        """:meta private:"""
        token = SubscriptionInstantiationContext._SUBSCRIPTION_CONTEXT_VAR.set(ctx)
        try:
            yield
        finally:
            SubscriptionInstantiationContext._SUBSCRIPTION_CONTEXT_VAR.reset(token)

    @classmethod
    def agent_type(cls) -> AgentType:
        try:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Delete the constructor call — the class is used only through its classmethods.
  2. To set up a context manually (e.g. in tests), use `with SubscriptionInstantiationContext.populate_context(AgentType("my_agent")): ...`.
  3. To read the current agent type inside that context, call `SubscriptionInstantiationContext.agent_type()`.
  4. If a copy/pickle framework triggered it, exclude this class from serialization.

Example fix

# before
ctx = SubscriptionInstantiationContext()  # RuntimeError

# after
from autogen_core import AgentType
with SubscriptionInstantiationContext.populate_context(AgentType("my_agent")):
    current = SubscriptionInstantiationContext.agent_type()
Defensive patterns

Strategy: type-guard

Type guard

# Not a value type — the guard is simply: never call the constructor.
# Use only: SubscriptionInstantiationContext.populate_context(...) and .agent_type()

Prevention

When it happens

Trigger: Any expression `SubscriptionInstantiationContext()` — directly, via copy.deepcopy/pickle that reconstructs objects, or through a framework that reflectively instantiates every class it discovers.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/3fb27ed08448c080. Report an issue: GitHub.