{"record":{"id":"380174f2b4d9690f","repo":"microsoft/autogen","slug":"agent-with-name-id-type-not-found","errorCode":null,"errorMessage":"Agent with name {id.type} not found.","messagePattern":"Agent with name (.+?) not found\\.","errorType":"exception","errorClass":"LookupError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py","lineNumber":991,"sourceCode":"                logger.error(f\"Error constructing agent {agent_id}\", exc_info=True)\n                raise\n\n    async def _get_agent(self, agent_id: AgentId) -> Agent:\n        if agent_id in self._instantiated_agents:\n            return self._instantiated_agents[agent_id]\n\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(","sourceCodeStart":973,"sourceCodeEnd":1009,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py#L973-L1009","documentation":"try_get_underlying_agent_instance raises LookupError when the AgentId's type is absent from _agent_factories — i.e. the runtime does not know the type at all. A separate TypeError is raised later if the type exists but the instance is not of the requested class, so this error specifically means 'no such agent type registered'.","triggerScenarios":"Calling await runtime.try_get_underlying_agent_instance(AgentId('worker', '1'), WorkerAgent) where 'worker' was never registered on this runtime; querying an agent instance in tests before registration; using an id built from a stale string constant after the registration name changed.","commonSituations":"Test helpers that grab the underlying instance to assert on internal state; refactors renaming agent types while call sites keep old strings; multiple runtimes (per-test) where lookup happens on the wrong runtime object.","solutions":["Ensure register_factory/register_agent_instance ran (and was awaited) for that type before the lookup","Centralize type names as shared AgentType constants/enum so producers, registrations, and lookups cannot drift","Wrap the lookup in try/except LookupError when probing optional agents, treating absence as None"],"exampleFix":"# before\nagent = await runtime.try_get_underlying_agent_instance(\n    AgentId(\"worker\", \"1\"), WorkerAgent\n)  # LookupError if unregistered\n\n# after\ntry:\n    agent = await runtime.try_get_underlying_agent_instance(\n        AgentId(WORKER.type, \"1\"), WorkerAgent\n    )\nexcept LookupError:\n    agent = None","handlingStrategy":"try-catch","validationCode":"def lookup_will_succeed(runtime, agent_id) -> bool:\n    return agent_id.type in runtime._agent_factories","typeGuard":"def type_is_registered(runtime, t: str) -> bool:\n    return t in runtime._agent_factories","tryCatchPattern":"try:\n    agent = await runtime.try_get_underlying_agent_instance(\n        AgentId(WORKER.type, key), WorkerAgent\n    )\nexcept LookupError:\n    agent = None  # optional agent; handle absence\nexcept TypeError:\n    raise  # registered but wrong class: real bug","preventionTips":["Register all queried agents before lookups in tests","Import shared type constants instead of string literals","Separate LookupError (unknown type) handling from TypeError (wrong class) handling"],"tags":["autogen-core","runtime","lookuperror","agent-registration","testing"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}