{"record":{"id":"1fb50f088c8079c2","repo":"microsoft/semantic-kernel","slug":"return-not-found-in-function-signature","errorCode":null,"errorMessage":"return not found in function signature","messagePattern":"return not found in function signature","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/runtime/core/routed_agent.py","lineNumber":135,"sourceCode":"        func: The function to be decorated.\n        strict: If `True`, the handler will raise an exception if the message type or return type is not in the target\n            types. If `False`, it will log a warning instead.\n        match: A function that takes the message and the context as arguments and returns a boolean. This is used for\n            secondary routing after the message type. For handlers addressing the same message type, the match function\n            is applied in alphabetical order of the handlers and the first matching handler will be called while the\n            rest are skipped. If `None`, the first handler in alphabetical order matching the same message type will\n            be called.\n    \"\"\"\n\n    def decorator(\n        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 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        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}\")","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/core/routed_agent.py#L117-L153","documentation":"Raised by the @message_handler decorator when the decorated method has no return type annotation. The decorator inspects type_hints['return'] to determine which message types the handler produces, so a missing return annotation (even -> None) prevents routing of responses.","triggerScenarios":"Decorating an async method without a -> annotation, or an annotation that get_type_hints cannot resolve.","commonSituations":"Omitting the return annotation on a fire-and-forget handler (must still write -> None); PEP 484 string annotations referencing unresolved names.","solutions":["Add an explicit return annotation, e.g. -> None or -> ResponseMsg.","For handlers that return nothing, annotate -> None explicitly.","Ensure any names in string annotations are imported and resolvable by get_type_hints.","If using from __future__ import annotations, confirm all referenced types are in scope."],"exampleFix":"// before\n@message_handler\nasync def handle(self, message: MyMsg, ctx: MessageContext): ...\n// after\n@message_handler\nasync def handle(self, message: MyMsg, ctx: MessageContext) -> None: ...","handlingStrategy":"validation","validationCode":"from typing import get_type_hints\n\ndef handler_has_return_annotation(func) -> bool:\n    return \"return\" in get_type_hints(func)","typeGuard":"from typing import get_type_hints, Callable\n\ndef handler_has_return(func: Callable) -> bool:\n    try:\n        return \"return\" in get_type_hints(func)\n    except Exception:\n        return False","tryCatchPattern":null,"preventionTips":["Always annotate handler returns (use -> None for no response).","Ensure string annotations are resolvable.","Lint handler signatures in CI with get_type_hints checks.","Import all referenced types at runtime."],"tags":["runtime","decorators","message-handler","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}