microsoft/semantic-kernel · 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-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.

Source

Thrown at python/semantic_kernel/agents/runtime/in_process/subscription_context.py:18

# Copyright (c) Microsoft. All rights reserved.

from collections.abc import Generator
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Any, ClassVar

from semantic_kernel.agents.runtime.core.agent_type import AgentType
from semantic_kernel.utils.feature_stage_decorator import experimental


@experimental
class SubscriptionInstantiationContext:
    """Context manager for subscription instantiation."""

    def __init__(self) -> None:
        """Prevent instantiation of SubscriptionInstantiationContext."""
        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]:
        """Populate the context with the agent type."""
        token = SubscriptionInstantiationContext._SUBSCRIPTION_CONTEXT_VAR.set(ctx)
        try:
            yield
        finally:
            SubscriptionInstantiationContext._SUBSCRIPTION_CONTEXT_VAR.reset(token)

    @classmethod
    def agent_type(cls) -> AgentType:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Never instantiate it; call the classmethods directly, e.g. `with SubscriptionInstantiationContext.populate_context(agent_type): ...`
  2. If you need the current agent type, read it via `SubscriptionInstantiationContext.agent_type()` (only valid inside an instantiation context).
  3. Let the AgentRuntime own all interaction with this class during agent registration/instantiation.

Example fix

// before
ctx = SubscriptionInstantiationContext()  # raises RuntimeError

// after
# static class - use classmethods only
with SubscriptionInstantiationContext.populate_context(agent_type):
    current = SubscriptionInstantiationContext.agent_type()
Defensive patterns

Strategy: validation

Validate before calling

# This class is never instantiated. Validate at review time that no code calls SubscriptionInstantiationContext().
import ast
src = open(path).read()
for node in ast.walk(ast.parse(src)):
    if isinstance(node, ast.Call) and getattr(node.func, 'id', '') == 'SubscriptionInstantiationContext':
        raise AssertionError('SubscriptionInstantiationContext must not be instantiated')

Type guard

from typing import Type
# no instance guard is meaningful; the class is used only by classmethod.
# Treat any value claimed to be an instance as a bug.
def _assert_not_instance(value: object) -> None:
    assert not isinstance(value, SubscriptionInstantiationContext)

Prevention

When it happens

Trigger: Writing `SubscriptionInstantiationContext()` anywhere in application or test code. The constructor unconditionally raises before any state is set.

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

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/f264d4904d6bdc1b. Report an issue: GitHub.