{"record":{"id":"fea0099e51564e00","repo":"microsoft/autogen","slug":"no-final-model-result-in-streaming-mode-fea009","errorCode":null,"errorMessage":"No final model result in streaming mode.","messagePattern":"No final model result in streaming mode\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py","lineNumber":818,"sourceCode":"        \"\"\"\n        Perform a model inference and yield either streaming chunk events or the final CreateResult.\n        \"\"\"\n        all_messages = await model_context.get_messages()\n        llm_messages = cls._get_compatible_context(model_client=model_client, messages=system_messages + all_messages)\n\n        if model_client_stream:\n            model_result: Optional[CreateResult] = None\n            async for chunk in model_client.create_stream(\n                llm_messages, tools=[], cancellation_token=cancellation_token\n            ):\n                if isinstance(chunk, CreateResult):\n                    model_result = chunk\n                elif isinstance(chunk, str):\n                    yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name)\n                else:\n                    raise RuntimeError(f\"Invalid chunk type: {type(chunk)}\")\n            if model_result is None:\n                raise RuntimeError(\"No final model result in streaming mode.\")\n            yield model_result\n        else:\n            model_result = await model_client.create(llm_messages, tools=[], cancellation_token=cancellation_token)\n            yield model_result\n\n    @staticmethod\n    async def _add_messages_to_context(\n        model_context: ChatCompletionContext,\n        messages: Sequence[BaseChatMessage],\n    ) -> None:\n        \"\"\"\n        Add incoming messages to the model context.\n        \"\"\"\n        for msg in messages:\n            if isinstance(msg, HandoffMessage):\n                for llm_msg in msg.context:\n                    await model_context.add_message(llm_msg)\n            await model_context.add_message(msg.to_model_message())","sourceCodeStart":800,"sourceCodeEnd":836,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py#L800-L836","documentation":"CodeExecutorAgent consumed model_client.create_stream() to completion but never received the final CreateResult chunk, so it has no complete model result to work with. Same streaming contract as AssistantAgent: str chunks are deltas, the last chunk must be the full CreateResult.","triggerScenarios":"CodeExecutorAgent with model_client_stream=True and a client whose create_stream() yields only str deltas then returns; a client that raises internally and ends the generator early; cancellation mid-stream in older client versions.","commonSituations":"Mock/stub clients in tests that emit a few text chunks and stop; partial upgrades of autogen-ext where the final result emission regressed; gateway proxies that cut the stream before the terminating frame.","solutions":["Patch the custom client to yield a final CreateResult after all deltas.","Upgrade autogen-ext / autogen-core / autogen-agentchat to matching versions.","Inspect client-side logs for exceptions that end the stream prematurely.","Workaround: model_client_stream=False."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"chunks = [c async for c in model_client.create_stream([SystemMessage(content=\"ping\")])]\nif not any(isinstance(c, CreateResult) for c in chunks):\n    model_client_stream = False  # client cannot complete the streaming contract","typeGuard":null,"tryCatchPattern":"try:\n    async for ev in agent.run_stream(task=task):\n        ...\nexcept RuntimeError as e:\n    if \"No final model result\" in str(e):\n        result = await agent.run(task=task)  # non-streaming fallback\n    else:\n        raise","preventionTips":["Smoke-test create_stream's final CreateResult before enabling model_client_stream.","Keep autogen packages version-aligned.","Watch client logs for exceptions that truncate the stream."],"tags":["streaming","model-client","code-executor","protocol-violation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}