microsoft/autogen · error · RuntimeError

Invalid chunk type: {type(chunk)}

Error message

Invalid chunk type: {type(chunk)}

What it means

During streaming inference, AssistantAgent consumes chunks from model_client.create_stream() and accepts only CreateResult (final result) and str (text deltas); any other object type raises RuntimeError. This almost always indicates a model client that does not conform to the ChatCompletionClient streaming protocol — e.g. a custom/older client yielding different event objects.

Source

Thrown at python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py:1104

        llm_messages = cls._get_compatible_context(model_client=model_client, messages=system_messages + all_messages)

        tools = [tool for wb in workbench for tool in await wb.list_tools()] + handoff_tools

        if model_client_stream:
            model_result: Optional[CreateResult] = None

            async for chunk in model_client.create_stream(
                llm_messages,
                tools=tools,
                json_output=output_content_type,
                cancellation_token=cancellation_token,
            ):
                if isinstance(chunk, CreateResult):
                    model_result = chunk
                elif isinstance(chunk, str):
                    yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name, full_message_id=message_id)
                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=tools,
                cancellation_token=cancellation_token,
                json_output=output_content_type,
            )
            yield model_result

    @classmethod
    async def _process_model_result(
        cls,
        model_result: CreateResult,
        inner_messages: List[BaseAgentEvent | BaseChatMessage],
        cancellation_token: CancellationToken,

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Align package versions: pip install -U autogen-agentchat autogen-core autogen-ext so all packages share the same CreateResult type.
  2. If using a custom model client, make create_stream yield only str chunks and exactly one CreateResult at the end.
  3. Check `pip list | grep autogen` for duplicate/conflicting installs and reinstall into a clean venv.

Example fix

# before (custom client)
async def create_stream(self, messages, **kwargs):
    yield {"text": "hello"}          # dict chunk -> RuntimeError
    yield self._make_result(...)

# after
async def create_stream(self, messages, **kwargs):
    yield "hello"
    yield CreateResult(finish_reason="stop", content="hello", usage=..., cached=False)
Defensive patterns

Strategy: type-guard

Validate before calling

from autogen_core.models import CreateResult
async def check_client_stream_protocol(client, sample_messages):
    types_seen = set()
    async for chunk in client.create_stream(sample_messages):
        types_seen.add(type(chunk))
    assert types_seen <= {str, CreateResult}, f"non-conforming chunks: {types_seen - {str, CreateResult}}"

Type guard

from autogen_core.models import CreateResult

def is_valid_stream_chunk(chunk) -> bool:
    return isinstance(chunk, (str, CreateResult))

Try / catch

try:
    async for msg in agent.run_stream(task="hi"):
        ...
except RuntimeError as e:
    if "Invalid chunk type" in str(e):
        raise RuntimeError("model client does not conform to create_stream protocol; align autogen package versions or fix the client") from e
    raise

Prevention

When it happens

Trigger: Calling agent.run_stream()/on_messages_stream with a model client whose create_stream yields non-conforming chunk types (custom clients emitting dicts or library-specific event objects), or mixed autogen package versions where CreateResult classes differ between autogen_core copies.

Common situations: Third-party model clients implementing only part of the protocol, duplicate autogen-core installations making isinstance checks fail across copies, or upgrading autogen-agentchat without upgrading autogen-core (or the client package).

Related errors


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