{"record":{"id":"2f357c042f6fc78d","repo":"microsoft/autogen","slug":"task-must-be-a-string-a-basechatmessage-or-a-lis","errorCode":null,"errorMessage":"Task must be a string, a BaseChatMessage, or a list of BaseChatMessage.","messagePattern":"Task must be a string, a BaseChatMessage, or a list of BaseChatMessage\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py","lineNumber":472,"sourceCode":"        \"\"\"\n        # Create the messages list if the task is a string or a chat message.\n        messages: List[BaseChatMessage] | None = None\n        if task is None:\n            pass\n        elif isinstance(task, str):\n            messages = [TextMessage(content=task, source=\"user\")]\n        elif isinstance(task, BaseChatMessage):\n            messages = [task]\n        elif isinstance(task, list):\n            if not task:\n                raise ValueError(\"Task list cannot be empty.\")\n            messages = []\n            for msg in task:\n                if not isinstance(msg, BaseChatMessage):\n                    raise ValueError(\"All messages in task list must be valid BaseChatMessage types\")\n                messages.append(msg)\n        else:\n            raise ValueError(\"Task must be a string, a BaseChatMessage, or a list of BaseChatMessage.\")\n        # Check if the messages types are registered with the message factory.\n        if messages is not None:\n            for msg in messages:\n                if not self._message_factory.is_registered(msg.__class__):\n                    raise ValueError(\n                        f\"Message type {msg.__class__} is not registered with the message factory. \"\n                        \"Please register it with the message factory by adding it to the \"\n                        \"custom_message_types list when creating the team.\"\n                    )\n\n        if self._is_running:\n            raise ValueError(\"The team is already running, it cannot run again until it is stopped.\")\n        self._is_running = True\n\n        if self._embedded_runtime:\n            # Start the embedded runtime.\n            assert isinstance(self._runtime, SingleThreadedAgentRuntime)\n            self._runtime.start()","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py#L454-L490","documentation":"The task parameter of run()/run_stream() accepts exactly three shapes: a str, a single BaseChatMessage, or a list of BaseChatMessage. Any other type (int, dict, tuple, an agent event, None-like sentinels) hits the final else branch.","triggerScenarios":"Calling team.run(task=42), team.run(task={'prompt': 'hi'}), or passing a BaseAgentEvent instance. Note task=None is valid and means 'no new task'.","commonSituations":"Passing a dict expecting it to be interpreted as a message; passing a variable whose type changed upstream; passing a ChatCompletion result object directly.","solutions":["Convert the value: strings pass through, dicts go through MessageFactory.create(), otherwise build TextMessage(content=str(value), source=\"user\").","Check the variable you are forwarding — often an upstream function returns a dict or object instead of the expected str/message.","Pass None explicitly when there is no task."],"exampleFix":"# before\nawait team.run(task=result.json())  # dict -> ValueError\n\n# after\nawait team.run(task=str(result.json()))  # or TextMessage(content=..., source=\"user\")","handlingStrategy":"type-guard","validationCode":"from autogen_agentchat.messages import BaseChatMessage, TextMessage\n\ndef normalize_task(task):\n    if isinstance(task, str):\n        return [TextMessage(content=task, source=\"user\")]\n    if isinstance(task, BaseChatMessage):\n        return [task]\n    if isinstance(task, list):\n        return [m if isinstance(m, BaseChatMessage) else TextMessage(content=str(m), source=\"user\") for m in task]\n    return [TextMessage(content=str(task), source=\"user\")]","typeGuard":"def is_valid_task(task) -> bool:\n    return task is None or isinstance(task, (str, BaseChatMessage)) or (\n        isinstance(task, list) and task and all(isinstance(m, BaseChatMessage) for m in task)\n    )","tryCatchPattern":null,"preventionTips":["Always pass str, BaseChatMessage, list[BaseChatMessage], or None.","Normalize upstream data (dicts, objects) into messages before calling run().","Annotate helper APIs with the task union type so mypy catches misuse."],"tags":["python","autogen","task","type-validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}