{"record":{"id":"d7fdc051c03f6dd6","repo":"microsoft/semantic-kernel","slug":"agent-with-name-id-type-is-not-of-type-type-n","errorCode":null,"errorMessage":"Agent with name {id.type} is not of type {type.__name__}. It is of type {type_func_alias(agent_instance).__name__}","messagePattern":"Agent with name (.+?) is not of type (.+?)\\. It is of type (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/runtime/in_process/in_process_runtime.py","lineNumber":817,"sourceCode":"        if agent_id.type not in self._agent_factories:\n            raise LookupError(f\"Agent with name {agent_id.type} not found.\")\n\n        agent_factory = self._agent_factories[agent_id.type]\n        agent = await self._invoke_agent_factory(agent_factory, agent_id)\n        self._instantiated_agents[agent_id] = agent\n        return agent\n\n    # TODO(evmattso): uncomment out the following type ignore when this is fixed in mypy: https://github.com/python/mypy/issues/3737\n    async def try_get_underlying_agent_instance(self, id: AgentId, type: type[T] = Agent) -> T:  # type: ignore[assignment]\n        \"\"\"Try to get the underlying agent instance by name and namespace.\"\"\"\n        if id.type not in self._agent_factories:\n            raise LookupError(f\"Agent with name {id.type} not found.\")\n\n        # TODO(evmattso): check if remote\n        agent_instance = await self._get_agent(id)\n\n        if not isinstance(agent_instance, type):\n            raise TypeError(\n                f\"Agent with name {id.type} is not of type {type.__name__}. \"\n                f\"It is of type {type_func_alias(agent_instance).__name__}\"\n            )\n\n        return agent_instance\n\n    async def add_subscription(self, subscription: Subscription) -> None:\n        \"\"\"Add a subscription to the runtime.\"\"\"\n        await self._subscription_manager.add_subscription(subscription)\n\n    async def remove_subscription(self, id: str) -> None:\n        \"\"\"Remove a subscription from the runtime.\"\"\"\n        await self._subscription_manager.remove_subscription(id)\n\n    async def get(\n        self, id_or_type: AgentId | AgentType | str, /, key: str = \"default\", *, lazy: bool = True\n    ) -> AgentId:\n        \"\"\"Get an agent by id or type.\"\"\"","sourceCodeStart":799,"sourceCodeEnd":835,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/in_process/in_process_runtime.py#L799-L835","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Match the type parameter to the actual class the factory returns.","If you only need the base Agent interface, omit the type parameter (defaults to Agent).","If multiple agent types are possible, use isinstance checks after retrieval or catch TypeError."],"exampleFix":"# before\nagent = await runtime.try_get_underlying_agent_instance(\n    AgentId('my_agent', 'default'), OldAgent\n)  # factory returns NewAgent → TypeError\n\n# after\nagent = await runtime.try_get_underlying_agent_instance(\n    AgentId('my_agent', 'default'), NewAgent\n)\n# or if you just need the base interface:\nagent = await runtime.try_get_underlying_agent_instance(\n    AgentId('my_agent', 'default')\n)","handlingStrategy":"type-guard","validationCode":"# Retrieve as base Agent first, then check type\nagent = await runtime.try_get_underlying_agent_instance(id)  # defaults to Agent\nif not isinstance(agent, ExpectedAgent):\n    raise TypeError(f'Expected {ExpectedAgent.__name__}, got {type(agent).__name__}')","typeGuard":"def is_agent_of_type(agent, expected_type) -> bool:\n    return isinstance(agent, expected_type)","tryCatchPattern":"try:\n    agent = await runtime.try_get_underlying_agent_instance(id, SpecificAgent)\nexcept TypeError:\n    agent = await runtime.try_get_underlying_agent_instance(id)  # fall back to base","preventionTips":["Omit the type parameter if you only need the base Agent interface.","Verify the factory's return type matches the requested type parameter.","Use isinstance checks after base-type retrieval for polymorphic scenarios."],"tags":["agent-runtime","try-get-agent","type-mismatch","typeerror","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}