{"record":{"id":"4705d77128e828a2","repo":"microsoft/autogen","slug":"the-team-did-not-produce-a-final-taskresult-check","errorCode":null,"errorMessage":"The team did not produce a final TaskResult. Check the team's run_stream method.","messagePattern":"The team did not produce a final TaskResult\\. Check the team's run_stream method\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_chat_agent_container.py","lineNumber":103,"sourceCode":"    @event\n    async def handle_request(self, message: GroupChatRequestPublish, ctx: MessageContext) -> None:\n        \"\"\"Handle a content request event by passing the messages in the buffer\n        to the delegate agent and publish the response.\"\"\"\n        if isinstance(self._agent, Team):\n            try:\n                stream = self._agent.run_stream(\n                    task=self._message_buffer,\n                    cancellation_token=ctx.cancellation_token,\n                    output_task_messages=False,\n                )\n                result: TaskResult | None = None\n                async for team_event in stream:\n                    if isinstance(team_event, TaskResult):\n                        result = team_event\n                    else:\n                        await self._log_message(team_event)\n                if result is None:\n                    raise RuntimeError(\n                        \"The team did not produce a final TaskResult. Check the team's run_stream method.\"\n                    )\n                self._message_buffer.clear()\n                # Publish the team response to the group chat.\n                await self.publish_message(\n                    GroupChatTeamResponse(result=result, name=self._agent.name),\n                    topic_id=DefaultTopicId(type=self._parent_topic_type),\n                    cancellation_token=ctx.cancellation_token,\n                )\n            except Exception as e:\n                # Publish the error to the group chat.\n                error_message = SerializableException.from_exception(e)\n                await self.publish_message(\n                    GroupChatError(error=error_message),\n                    topic_id=DefaultTopicId(type=self._parent_topic_type),\n                    cancellation_token=ctx.cancellation_token,\n                )\n                # Raise the error to the runtime.","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_chat_agent_container.py#L85-L121","documentation":"Raised inside ChatAgentContainer when a nested Team used as a group-chat participant finished its run_stream without ever yielding a final TaskResult event. Every compliant Team.run_stream must terminate with a TaskResult; if the inner stream ends empty (e.g., an exception was swallowed or a custom Team was implemented incorrectly), the container cannot publish a GroupChatTeamResponse and fails the outer chat.","triggerScenarios":"Wrapping a custom Team subclass (implementing run_stream yourself) that returns without yielding TaskResult; an inner team whose stream is cut off by cancellation or by an exception raised before the final event; incompatible/older Team implementations after a version upgrade.","commonSituations":"Using a hand-rolled Team as a participant in SelectorGroupChat/Swarm/GraphFlow; upgrading autogen-agentchat where the Team streaming contract changed; inner team cancelled mid-run and the cancellation path skips the final event.","solutions":["Ensure your custom Team.run_stream always yields a TaskResult as its final event on every code path (including early exits).","If cancellation can interrupt the inner run, catch and still emit TaskResult with the messages collected so far.","Prefer composing built-in teams (RoundRobin, Selector, GraphFlow) as the nested participant instead of implementing run_stream manually."],"exampleFix":"// before\nclass MyTeam(Team):\n    async def run_stream(self, task, cancellation_token):\n        for msg in self._produce(task):\n            yield msg\n        # returns without TaskResult -> error\n\n// after\nclass MyTeam(Team):\n    async def run_stream(self, task, cancellation_token):\n        messages = []\n        for msg in self._produce(task):\n            messages.append(msg)\n            yield msg\n        yield TaskResult(messages=messages, stop_reason=\"done\")","handlingStrategy":"validation","validationCode":"async def run_stream_yields_result(team: Team, task) -> bool:\n    found = False\n    async for ev in team.run_stream(task):\n        if isinstance(ev, TaskResult):\n            found = True\n    return found\n# unit-test this for every custom Team before nesting it in a group chat","typeGuard":null,"tryCatchPattern":"try:\n    async for ev in outer_team.run_stream(task):\n        ...\nexcept RuntimeError as e:\n    if \"did not produce a final TaskResult\" in str(e):\n        # inspect the inner Team.run_stream implementation; add a final TaskResult yield\n        ...","preventionTips":["Unit-test custom Team.run_stream against the contract: final event must be TaskResult.","Use try/finally in run_stream to guarantee TaskResult is emitted even on early return.","Prefer built-in teams as nested participants."],"tags":["group-chat","nested-team","streaming","contract"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}