{"record":{"id":"6606ca2c70354d32","repo":"microsoft/autogen","slug":"message-type-not-found","errorCode":null,"errorMessage":"Message type not found","messagePattern":"Message type not found","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_closure_agent.py","lineNumber":43,"sourceCode":"\ndef get_handled_types_from_closure(\n    closure: Callable[[ClosureAgent, T, MessageContext], Awaitable[Any]],\n) -> Sequence[type]:\n    args = inspect.getfullargspec(closure)[0]\n    if len(args) != 3:\n        raise AssertionError(\"Closure must have 4 arguments\")\n\n    message_arg_name = args[1]\n\n    type_hints = get_type_hints(closure)\n\n    if \"return\" not in type_hints:\n        raise AssertionError(\"return not found in function signature\")\n\n    # Get the type of the message parameter\n    target_types = get_types(type_hints[message_arg_name])\n    if target_types is None:\n        raise AssertionError(\"Message type not found\")\n\n    # print(type_hints)\n    return_types = get_types(type_hints[\"return\"])\n\n    if return_types is None:\n        raise AssertionError(\"Return type not found\")\n\n    return target_types\n\n\nclass ClosureContext(Protocol):\n    @property\n    def id(self) -> AgentId: ...\n\n    async def send_message(\n        self,\n        message: Any,\n        recipient: AgentId,","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_closure_agent.py#L25-L61","documentation":"The same introspection helper requires the closure's second parameter (the message) to carry a resolvable type annotation that get_types() can extract. If the message parameter is unannotated, annotated Any, or its annotation cannot be resolved to concrete types, AssertionError('Message type not found') fires.","triggerScenarios":"async def handler(agent, message, ctx) -> None with no annotation on message; annotating message as Any; forward-reference annotations that get_type_hints cannot resolve; Union/None annotations for which get_types returns None.","commonSituations":"Generic handlers meant to accept 'anything' (use explicit routing or multiple registrations instead); missing imports for annotation types; annotation stripping by decorators or exec-based code.","solutions":["Annotate the message parameter with the concrete message type: async def handler(agent, message: MyMessage, ctx) -> None.","Ensure the annotation type is imported where the closure is defined so get_type_hints can evaluate it.","Avoid Any or bare object annotations; register a separate closure per message type if you handle several."],"exampleFix":"# before\nasync def handler(agent, message, ctx) -> None:  # message unannotated\n    ...\n\n# after\nasync def handler(agent: ClosureContext, message: TextMessage, ctx: MessageContext) -> None:\n    ...","handlingStrategy":"validation","validationCode":"from typing import get_type_hints\n\nhints = get_type_hints(my_closure)\nmsg_type = hints.get(\"message\")  # or the actual parameter name\nassert msg_type is not None and msg_type is not object, \"annotate the message parameter with a concrete type\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Annotate the message parameter with the concrete message class.","Avoid Any/object annotations on the message parameter.","One closure per message type; register several agents for several types."],"tags":["autogen-core","closure-agent","type-hints","annotations"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}