{"record":{"id":"1225065eca33bf66","repo":"microsoft/autogen","slug":"return-type-type-return-value-is-not-none","errorCode":null,"errorMessage":"Return type {type(return_value)} is not None.","messagePattern":"Return type (.+?) is not None\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_routed_agent.py","lineNumber":272,"sourceCode":"\n        if return_types is None:\n            raise AssertionError(\"Return type not found. Please use `None` as the type hint of the return type.\")\n\n        # Convert target_types to list and stash\n\n        @wraps(func)\n        async def wrapper(self: AgentT, message: ReceivesT, ctx: MessageContext) -> None:\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)  # type: ignore\n\n            if return_value is not None:\n                if strict:\n                    raise ValueError(f\"Return type {type(return_value)} is not None.\")\n                else:\n                    logger.warning(f\"Return type {type(return_value)} is not None. It will be ignored.\")\n\n            return None\n\n        wrapper_handler = cast(MessageHandler[AgentT, ReceivesT, None], wrapper)\n        wrapper_handler.target_types = list(target_types)\n        wrapper_handler.produces_types = list(return_types)\n        wrapper_handler.is_message_handler = True\n        # Wrap the match function with a check on the is_rpc flag.\n        wrapper_handler.router = lambda _message, _ctx: (not _ctx.is_rpc) and (match(_message, _ctx) if match else True)\n\n        return wrapper_handler\n\n    if func is None and not callable(func):\n        return decorator\n    elif callable(func):\n        return decorator(func)","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_routed_agent.py#L254-L290","documentation":"The @event wrapper enforces the 'events return nothing' contract: if the decorated coroutine returns a non-None value and strict=True, ValueError is raised; with strict=False the value is logged and discarded (the wrapper always returns None). This prevents silently losing data that the caller might expect back.","triggerScenarios":"An @event handler contains 'return something' or its last expression evaluates to a value; refactoring an @rpc handler into @event but leaving return statements; generator/expression-based bodies that accidentally return non-None.","commonSituations":"Reusing handler bodies between rpc and event variants; early 'return result' used as a short-circuit inside event handlers; tests asserting a return value from event handlers.","solutions":["Remove return values from @event handlers — use bare 'return' for early exits","If the caller needs a response, switch to @rpc with the response type annotated","Set strict=False to downgrade to a warning while the value is still ignored"],"exampleFix":"# before\n@event\nasync def on_event(self, message: Ev, ctx: MessageContext) -> None:\n    return compute(message)  # ValueError when strict\n\n# after\n@event\nasync def on_event(self, message: Ev, ctx: MessageContext) -> None:\n    compute(message)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await agent.on_message(event, ctx)\nexcept ValueError as e:\n    if \"is not None\" in str(e):\n        logger.error(\"@event handler returned a value: %s\", e)\n    raise","preventionTips":["Never return values from @event handlers; use bare 'return' for early exits","If a computed result is needed, publish it as a new event or move the handler to @rpc"],"tags":["python","message-routing","pubsub","runtime","autogen-core"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}