{"record":{"id":"68b2f205a9399610","repo":"microsoft/autogen","slug":"return-type-not-found-68b2f2","errorCode":null,"errorMessage":"Return type not found","messagePattern":"Return type not found","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_routed_agent.py","lineNumber":137,"sourceCode":"        func: Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]],\n    ) -> MessageHandler[AgentT, ReceivesT, ProducesT]:\n        type_hints = get_type_hints(func)\n        if \"message\" not in type_hints:\n            raise AssertionError(\"message parameter not found in function signature\")\n\n        if \"return\" not in type_hints:\n            raise AssertionError(\"return parameter not found in function signature\")\n\n        # Get the type of the message parameter\n        target_types = get_types(type_hints[\"message\"])\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        # Convert target_types to list and stash\n\n        @wraps(func)\n        async def wrapper(self: AgentT, message: ReceivesT, ctx: MessageContext) -> ProducesT:\n            if type(message) not in target_types:\n                if strict:\n                    raise CantHandleException(f\"Message type {type(message)} not in target types {target_types}\")\n                else:\n                    logger.warning(f\"Message type {type(message)} not in target types {target_types}\")\n\n            return_value = await func(self, message, ctx)\n\n            if AnyType not in return_types and type(return_value) not in return_types:\n                if strict:\n                    raise ValueError(f\"Return type {type(return_value)} not in return types {return_types}\")\n                else:\n                    logger.warning(f\"Return type {type(return_value)} not in return types {return_types}\")","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_routed_agent.py#L119-L155","documentation":"The @message_handler decorator requires a resolvable return type annotation because it records produces_types (and serializes RPC responses). get_type_hints found no 'return' entry or get_types() could not extract a concrete type from it, so the decorator aborts at import/decoration time.","triggerScenarios":"A handler declared as async def handle(self, message: Msg, ctx: MessageContext) with no return annotation, or annotated with an unresolvable forward reference / unsupported generic so get_types returns None.","commonSituations":"Omitting '-> ...' because Python does not require it; converting a sync callback that returned nothing into an event-style handler; string return annotations referencing types not importable at runtime.","solutions":["Add an explicit return annotation: '-> MyResponseType' for @rpc/@message_handler handlers","Annotate fire-and-forget handlers with '-> None' (required for @event handlers)","Ensure any string/forward-referenced return type is importable at runtime so get_type_hints resolves it"],"exampleFix":"# before\n@message_handler\nasync def handle(self, message: Msg, ctx: MessageContext): ...\n\n# after\n@message_handler\nasync def handle(self, message: Msg, ctx: MessageContext) -> Response: ...","handlingStrategy":"validation","validationCode":"from typing import get_type_hints\n\ndef handler_has_return_hint(func) -> bool:\n    return \"return\" in get_type_hints(func)","typeGuard":"def has_return_annotation(func) -> bool:\n    return get_type_hints(func).get(\"return\") is not None","tryCatchPattern":null,"preventionTips":["Treat '-> ReturnType' as mandatory in handler code style and lint for it (ruff ANN rules)","Add a unit test that imports every agent module — decorator AssertionErrors surface at import time"],"tags":["python","type-hints","decorator","autogen-core"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}