microsoft/autogen · error · RuntimeError

No final model result in streaming mode.

Error message

No final model result in streaming mode.

What it means

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.

Source

Thrown at python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py:818

        """
        Perform a model inference and yield either streaming chunk events or the final CreateResult.
        """
        all_messages = await model_context.get_messages()
        llm_messages = cls._get_compatible_context(model_client=model_client, messages=system_messages + all_messages)

        if model_client_stream:
            model_result: Optional[CreateResult] = None
            async for chunk in model_client.create_stream(
                llm_messages, tools=[], cancellation_token=cancellation_token
            ):
                if isinstance(chunk, CreateResult):
                    model_result = chunk
                elif isinstance(chunk, str):
                    yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name)
                else:
                    raise RuntimeError(f"Invalid chunk type: {type(chunk)}")
            if model_result is None:
                raise RuntimeError("No final model result in streaming mode.")
            yield model_result
        else:
            model_result = await model_client.create(llm_messages, tools=[], cancellation_token=cancellation_token)
            yield model_result

    @staticmethod
    async def _add_messages_to_context(
        model_context: ChatCompletionContext,
        messages: Sequence[BaseChatMessage],
    ) -> None:
        """
        Add incoming messages to the model context.
        """
        for msg in messages:
            if isinstance(msg, HandoffMessage):
                for llm_msg in msg.context:
                    await model_context.add_message(llm_msg)
            await model_context.add_message(msg.to_model_message())

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Patch the custom client to yield a final CreateResult after all deltas.
  2. Upgrade autogen-ext / autogen-core / autogen-agentchat to matching versions.
  3. Inspect client-side logs for exceptions that end the stream prematurely.
  4. Workaround: model_client_stream=False.
Defensive patterns

Strategy: fallback

Validate before calling

chunks = [c async for c in model_client.create_stream([SystemMessage(content="ping")])]
if not any(isinstance(c, CreateResult) for c in chunks):
    model_client_stream = False  # client cannot complete the streaming contract

Try / catch

try:
    async for ev in agent.run_stream(task=task):
        ...
except RuntimeError as e:
    if "No final model result" in str(e):
        result = await agent.run(task=task)  # non-streaming fallback
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/fea0099e51564e00. Report an issue: GitHub.