{"record":{"id":"6c1281c032aa2940","repo":"microsoft/autogen","slug":"agent-with-name-id-type-is-not-of-type-type-n-6c1281","errorCode":null,"errorMessage":"Agent with name {id.type} is not of type {type.__name__}","messagePattern":"Agent with name (.+?) is not of type (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py","lineNumber":818,"sourceCode":"\n        if agent_id.type not in self._agent_factories:\n            raise ValueError(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(f\"Agent with name {id.type} is not of type {type.__name__}\")\n\n        return agent_instance\n\n    async def add_subscription(self, subscription: Subscription) -> None:\n        if self._host_connection is None:\n            raise RuntimeError(\"Host connection is not set.\")\n\n        message = agent_worker_pb2.AddSubscriptionRequest(subscription=subscription_to_proto(subscription))\n        _response: agent_worker_pb2.AddSubscriptionResponse = await self._host_connection.stub.AddSubscription(\n            message, metadata=self._host_connection.metadata\n        )\n\n        # Add to local subscription manager.\n        await self._subscription_manager.add_subscription(subscription)\n\n    async def remove_subscription(self, id: str) -> None:\n        if self._host_connection is None:\n            raise RuntimeError(\"Host connection is not set.\")","sourceCodeStart":800,"sourceCodeEnd":836,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py#L800-L836","documentation":"After resolving the agent via _get_agent, try_get_underlying_agent_instance(type=T) performs isinstance(agent_instance, type) and raises TypeError(f\"Agent with name {id.type} is not of type {type.__name__}\") when the instance belongs to a different class. Unlike error 884 (which uses exact type equality on factories), this check is isinstance-based, so subclasses pass; only genuinely unrelated classes fail.","triggerScenarios":"Requesting type=AssistantAgent for an id whose factory constructs CriticAgent; factories whose returned class varies by configuration/key so the same type name yields different classes for different keys; passing the wrong type parameter while iterating heterogeneous agents.","commonSituations":"Generic management code that assumes all agents under an id are one class; config-driven factories that switch implementation by agent key; refactors renaming or splitting agent classes while callers keep the old type argument.","solutions":["Pass the type argument matching what the factory for that id actually constructs","Make config-driven factories deterministic per key, or register distinct type names per implementation","Catch TypeError and handle the mixed-type case explicitly in generic tooling","Add unit tests asserting try_get_underlying_agent_instance(id, ExpectedClass) succeeds for every registered type you rely on"],"exampleFix":"# before\nagent = await runtime.try_get_underlying_agent_instance(AgentId('critic', 'default'), AssistantAgent)  # TypeError\n\n# after\nagent = await runtime.try_get_underlying_agent_instance(AgentId('critic', 'default'), CriticAgent)","handlingStrategy":"type-guard","validationCode":"from autogen_core import AgentId\nasync def is_agent_of_type(runtime, id: AgentId, cls) -> bool:\n    try:\n        await runtime.try_get_underlying_agent_instance(id, cls)\n        return True\n    except (LookupError, TypeError):\n        return False","typeGuard":"async def is_agent_of_type(runtime, id, cls) -> bool:\n    try:\n        await runtime.try_get_underlying_agent_instance(id, cls)\n        return True\n    except (LookupError, TypeError):\n        return False","tryCatchPattern":"try:\n    agent = await runtime.try_get_underlying_agent_instance(id, AssistantAgent)\nexcept TypeError:\n    agent = None  # present but different class; handle or skip","preventionTips":["Pass the concrete class the factory constructs for that id","Keep one implementation class per agent type name","Test the (id, class) pairs you depend on in CI","Subclasses are fine (isinstance) — unrelated classes are not"],"tags":["lookup","type-mismatch","isinstance"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}