{"record":{"id":"3eaf8aabee2975e9","repo":"microsoft/autogen","slug":"message-type-type-message-not-in-target-types","errorCode":null,"errorMessage":"Message type {type(message)} not in target types {self._expected_types} of {self.id}. Set unknown_type_policy to 'warn' to suppress this exception, or 'ignore' to suppress this warning.","messagePattern":"Message type (.+?) not in target types (.+?) of (.+?)\\. Set unknown_type_policy to 'warn' to suppress this exception, or 'ignore' to suppress this warning\\.","errorType":"exception","errorClass":"CantHandleException","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_closure_agent.py","lineNumber":127,"sourceCode":"\n    @property\n    def id(self) -> AgentId:\n        return self._id\n\n    @property\n    def runtime(self) -> AgentRuntime:\n        return self._runtime\n\n    async def on_message_impl(self, message: Any, ctx: MessageContext) -> Any:\n        if type(message) not in self._expected_types:\n            if self._unknown_type_policy == \"warn\":\n                warnings.warn(\n                    f\"Message type {type(message)} not in target types {self._expected_types} of {self.id}. Set unknown_type_policy to 'error' to raise an exception, or 'ignore' to suppress this warning.\",\n                    stacklevel=1,\n                )\n                return None\n            elif self._unknown_type_policy == \"error\":\n                raise CantHandleException(\n                    f\"Message type {type(message)} not in target types {self._expected_types} of {self.id}. Set unknown_type_policy to 'warn' to suppress this exception, or 'ignore' to suppress this warning.\"\n                )\n\n        return await self._closure(self, message, ctx)\n\n    async def save_state(self) -> Mapping[str, Any]:\n        \"\"\"Closure agents do not have state. So this method always returns an empty dictionary.\"\"\"\n        return {}\n\n    async def load_state(self, state: Mapping[str, Any]) -> None:\n        \"\"\"Closure agents do not have state. So this method does nothing.\"\"\"\n        pass\n\n    @classmethod\n    async def register_closure(\n        cls,\n        runtime: AgentRuntime,\n        type: str,","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_closure_agent.py#L109-L145","documentation":"In on_message_impl, if the delivered message's concrete type is not among the types derived from the closure's message annotation, behavior depends on unknown_type_policy: 'error' raises CantHandleException, 'warn' emits a warning and drops the message (returns None), 'ignore' silently proceeds. Note the exception text is self-inconsistent: it says set policy to 'warn' to suppress the exception — the actual escaping fix is 'ignore'.","triggerScenarios":"A closure annotated for message: MsgA receives MsgB (e.g. broadcast/publish to a topic multiple closure agents subscribe to); deserialization producing a different concrete class than annotated; unknown_type_policy='error' (the default is 'warn').","commonSituations":"Topic subscriptions shared by heterogeneous agents; version skew where a peer sends an upgraded message type; policy set to 'error' globally to catch routing bugs, then hit by an intentional broadcast.","solutions":["Align the closure's message annotation with what is actually delivered, or register multiple closures/agents per message type.","Narrow the agent's subscriptions so it only receives the annotated type.","Set unknown_type_policy='ignore' to silently skip foreign types, or keep 'warn' to log-and-drop.","If 'error' is intentional, fix the publisher/routing so the right type reaches this agent."],"exampleFix":"# before\nawait ClosureAgent.register(\n    runtime, \"worker\", lambda: ClosureAgent(\"d\", handler, unknown_type_policy=\"error\")\n)\n# handler annotated for MsgA but topic also carries MsgB -> CantHandleException\n\n# after\nasync def handler(agent: ClosureContext, message: MsgA, ctx: MessageContext) -> None: ...\nawait ClosureAgent.register(\n    runtime, \"worker_a\", lambda: ClosureAgent(\"d\", handler, unknown_type_policy=\"ignore\")\n)","handlingStrategy":"fallback","validationCode":"if unknown_type_policy == \"error\" and type(msg) not in expected_types:\n    # route to the correct handler or drop before it raises\n    logging.warning(\"unexpected %s for closure\", type(msg))\n    return","typeGuard":"def is_handled_message(msg: object, expected: tuple[type, ...]) -> TypeGuard[Any]:\n    return type(msg) in expected","tryCatchPattern":"from autogen_core.exceptions import CantHandleException\n\ntry:\n    await closure_agent.handle_message(msg, ctx)\nexcept CantHandleException as e:\n    if \"not in target types\" in str(e):\n        logging.warning(\"dropped foreign message %s\", type(msg))\n    else:\n        raise","preventionTips":["Scope topic subscriptions so closure agents only receive annotated types.","Choose unknown_type_policy deliberately: 'warn' during development, 'ignore' for mixed topics, 'error' only for strict routing.","Test with every message type that can reach the agent before enabling 'error'."],"tags":["autogen-core","closure-agent","message-routing","policy"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}