{"record":{"id":"d5075aea5cdb0125","repo":"microsoft/semantic-kernel","slug":"receive-task-must-be-an-instance-of-asyncio-task-o","errorCode":null,"errorMessage":"receive_task must be an instance of asyncio.Task or None","messagePattern":"receive_task must be an instance of asyncio\\.Task or None","errorType":"validation","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/group_chat/broadcast_queue.py","lineNumber":37,"sourceCode":"    \"\"\"Utility class to associate a queue with its specific lock.\"\"\"\n\n    queue: deque = Field(default_factory=deque)\n    queue_lock: SkipValidation[asyncio.Lock] = Field(default_factory=asyncio.Lock, exclude=True)\n    receive_task: SkipValidation[asyncio.Task | None] = None\n    receive_failure: Exception | None = None\n\n    @property\n    def is_empty(self):\n        \"\"\"Check if the queue is empty.\"\"\"\n        return len(self.queue) == 0\n\n    @model_validator(mode=\"before\")\n    def validate_receive_task(cls, values: Any):\n        \"\"\"Validate the receive task.\"\"\"\n        if isinstance(values, dict):\n            receive_task = values.get(\"receive_task\")\n            if receive_task is not None and not isinstance(receive_task, asyncio.Task):\n                raise ValidationError(\"receive_task must be an instance of asyncio.Task or None\")\n        return values\n\n\n@experimental\n@dataclass\nclass ChannelReference:\n    \"\"\"Tracks a channel along with its hashed key.\"\"\"\n\n    hash: str\n    channel: AgentChannel = field(default_factory=AgentChannel)\n\n\n@experimental\nclass BroadcastQueue(KernelBaseModel):\n    \"\"\"A queue for broadcasting messages to listeners.\"\"\"\n\n    queues: dict[str, QueueReference] = Field(default_factory=dict)\n    block_duration: float = 0.1","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/group_chat/broadcast_queue.py#L19-L55","documentation":"QueueReference is a pydantic model that owns a receive_task field used to track the background receive coroutine for a broadcast channel. A before-mode model_validator asserts that receive_task, if not None, is an instance of asyncio.Task. This prevents storing bare coroutines or Futures that would never be awaited/cancelled correctly by the broadcast loop.","triggerScenarios":"Constructing QueueReference (directly or via an ORM/serialization layer) with a dict where receive_task holds something other than None or an asyncio.Task — e.g. a raw coroutine object (async def result not wrapped), an asyncio.Future, a concurrent.futures.Future, or a thread handle.","commonSituations":"Manually instantiating QueueReference in tests or a custom broadcast implementation and assigning `receive_task=some_coro()` instead of `asyncio.create_task(some_coro())`; migrating code that previously used Futures; deserializing queue state from a format that does not preserve the Task type.","solutions":["Leave receive_task unset/None when constructing QueueReference; the broadcast loop assigns the Task itself via asyncio.create_task.","If you must supply it, wrap the coroutine first: receive_task=asyncio.create_task(self.receive(channel_ref, queue_ref)).","Never pass a coroutine object or a Future; only asyncio.Task (or None) is accepted."],"exampleFix":"// before\nQueueReference(queue=..., receive_task=self.receive(ref, q))  # coroutine, not a Task\n\n// after\nQueueReference(queue=..., receive_task=None)\n# the broadcast loop creates the task itself via asyncio.create_task","handlingStrategy":"type-guard","validationCode":"import asyncio\n\ndef coerce_receive_task(value):\n    if value is None or isinstance(value, asyncio.Task):\n        return value\n    if asyncio.iscoroutine(value):\n        return asyncio.create_task(value)\n    raise TypeError(\"receive_task must be asyncio.Task or None\")","typeGuard":"import asyncio\n\ndef is_valid_receive_task(v) -> bool:\n    return v is None or isinstance(v, asyncio.Task)","tryCatchPattern":null,"preventionTips":["Let the broadcast loop create the receive task itself; leave receive_task unset.","Never assign a raw coroutine or Future to receive_task.","In tests, construct QueueReference with receive_task=None."],"tags":["broadcast","pydantic","asyncio","validation","queue"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}