{"record":{"id":"3c66e8e5bf39180f","repo":"microsoft/autogen","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/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py","lineNumber":997,"sourceCode":"\n        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: 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        if id.type not in self._agent_factories:\n            raise LookupError(f\"Agent with name {id.type} not found.\")\n\n        # TODO: 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__}. 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        await self._subscription_manager.add_subscription(subscription)\n\n    async def remove_subscription(self, id: str) -> None:\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        return await get_impl(\n            id_or_type=id_or_type,\n            key=key,\n            lazy=lazy,","sourceCodeStart":979,"sourceCodeEnd":1015,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py#L979-L1015","documentation":"Thrown by SingleThreadedAgentRuntime.try_get_underlying_agent_instance() when the runtime locates the requested agent (its type is registered and instantiated) but the concrete instance is not an instance of the type parameter you passed. The runtime deliberately enforces this check before returning, so you cannot accidentally receive an object of the wrong class and have it fail later at attribute access. The message names both the requested type and the actual runtime type of the instance.","triggerScenarios":"Calling `await runtime.try_get_underlying_agent_instance(AgentId(\"worker\", \"default\"), MySubAgent)` when the agent type \"worker\" was registered with a factory producing a different class (e.g. `Agent` or another subclass). Also happens when two agent types share a key/type string and you fetch with the wrong type parameter, or after refactoring the agent class without updating the call site.","commonSituations":"Test code that grabs the underlying agent to assert on its state; refactoring where the agent class was renamed or swapped in the register call while callers still use the old class; copy-pasting an AgentId literal from another agent's code path.","solutions":["Read the message: it tells you the actual class of the instance; change the `type` argument of try_get_underlying_agent_instance to that class.","Verify what factory you registered for `id.type` (runtime.agent_styles / the AgentRuntime.register call) and make the requested type match the class that factory returns.","If you do not know the concrete class, call with the base class `try_get_underlying_agent_instance(id, Agent)` and then narrow with isinstance yourself.","If the agent registration itself is wrong (wrong class in the factory), fix the registration at startup."],"exampleFix":"# before\nagent = await runtime.try_get_underlying_agent_instance(\n    AgentId(\"worker\", \"default\"), MyOtherAgent\n)  # TypeError: it is of type MyAgent\n\n# after\nfrom autogen_core import Agent\nbase = await runtime.try_get_underlying_agent_instance(AgentId(\"worker\", \"default\"), Agent)\nif isinstance(base, MyAgent):\n    agent: MyAgent = base\nelse:\n    raise TypeError(f\"unexpected agent class {type(base).__name__}\")","handlingStrategy":"try-catch","validationCode":"agent_id = AgentId(\"worker\", \"default\")\nbase = await runtime.try_get_underlying_agent_instance(agent_id, Agent)  # base type always passes\nif not isinstance(base, MyAgent):\n    raise TypeError(f\"worker agent is {type(base).__name__}, expected MyAgent\")","typeGuard":"def is_agent_of_type(runtime: SingleThreadedAgentRuntime, agent_id: AgentId, cls: type) -> bool:\n    base = asyncio.get_event_loop().run_until_complete(\n        runtime.try_get_underlying_agent_instance(agent_id, Agent)\n    )\n    return isinstance(base, cls)","tryCatchPattern":"try:\n    agent = await runtime.try_get_underlying_agent_instance(agent_id, MyAgent)\nexcept TypeError as e:\n    # message contains the actual class name; log it and re-register or adjust callers\n    logger.error(\"type mismatch: %s\", e)\n    raise","preventionTips":["Fetch with the base Agent type and narrow with isinstance when the concrete class is not guaranteed.","Keep a single source of truth mapping AgentId.type -> agent class next to the register() call.","In tests, assert the registered factory's return type matches the class used in try_get_underlying_agent_instance."],"tags":["python","autogen-core","runtime","type-check","agents"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}