microsoft/semantic-kernel · error · TypeError

Agent with name {id.type} is not of type {type.__name__}. It

Error message

Agent with name {id.type} is not of type {type.__name__}. It is of type {type_func_alias(agent_instance).__name__}

What it means

try_get_underlying_agent_instance(type=T) retrieves the agent via _get_agent and then checks isinstance(agent_instance, type). If the agent's actual type does not match the requested type parameter, TypeError is raised with both the expected and actual class names.

Source

Thrown at python/semantic_kernel/agents/runtime/in_process/in_process_runtime.py:817

        if agent_id.type not in self._agent_factories:
            raise LookupError(f"Agent with name {agent_id.type} not found.")

        agent_factory = self._agent_factories[agent_id.type]
        agent = await self._invoke_agent_factory(agent_factory, agent_id)
        self._instantiated_agents[agent_id] = agent
        return agent

    # TODO(evmattso): uncomment out the following type ignore when this is fixed in mypy: https://github.com/python/mypy/issues/3737
    async def try_get_underlying_agent_instance(self, id: AgentId, type: type[T] = Agent) -> T:  # type: ignore[assignment]
        """Try to get the underlying agent instance by name and namespace."""
        if id.type not in self._agent_factories:
            raise LookupError(f"Agent with name {id.type} not found.")

        # TODO(evmattso): check if remote
        agent_instance = await self._get_agent(id)

        if not isinstance(agent_instance, type):
            raise TypeError(
                f"Agent with name {id.type} is not of type {type.__name__}. "
                f"It is of type {type_func_alias(agent_instance).__name__}"
            )

        return agent_instance

    async def add_subscription(self, subscription: Subscription) -> None:
        """Add a subscription to the runtime."""
        await self._subscription_manager.add_subscription(subscription)

    async def remove_subscription(self, id: str) -> None:
        """Remove a subscription from the runtime."""
        await self._subscription_manager.remove_subscription(id)

    async def get(
        self, id_or_type: AgentId | AgentType | str, /, key: str = "default", *, lazy: bool = True
    ) -> AgentId:
        """Get an agent by id or type."""

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Match the type parameter to the actual class the factory returns.
  2. If you only need the base Agent interface, omit the type parameter (defaults to Agent).
  3. If multiple agent types are possible, use isinstance checks after retrieval or catch TypeError.

Example fix

# before
agent = await runtime.try_get_underlying_agent_instance(
    AgentId('my_agent', 'default'), OldAgent
)  # factory returns NewAgent → TypeError

# after
agent = await runtime.try_get_underlying_agent_instance(
    AgentId('my_agent', 'default'), NewAgent
)
# or if you just need the base interface:
agent = await runtime.try_get_underlying_agent_instance(
    AgentId('my_agent', 'default')
)
Defensive patterns

Strategy: type-guard

Validate before calling

# Retrieve as base Agent first, then check type
agent = await runtime.try_get_underlying_agent_instance(id)  # defaults to Agent
if not isinstance(agent, ExpectedAgent):
    raise TypeError(f'Expected {ExpectedAgent.__name__}, got {type(agent).__name__}')

Type guard

def is_agent_of_type(agent, expected_type) -> bool:
    return isinstance(agent, expected_type)

Try / catch

try:
    agent = await runtime.try_get_underlying_agent_instance(id, SpecificAgent)
except TypeError:
    agent = await runtime.try_get_underlying_agent_instance(id)  # fall back to base

Prevention

When it happens

Trigger: Calling try_get_underlying_agent_instance(AgentId('my_agent', 'k'), ExpectedAgent) when the registered factory produces a DifferentAgent instance. The type parameter defaults to Agent (base class), so this only triggers when a specific subclass is requested.

Common situations: An agent factory was updated to return a different agent class, but the caller still requests the old type. A runtime shared across multiple agent types where the wrong type parameter is passed. Inheritance hierarchies where a factory returns a sibling subclass.

Related errors


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