{"record":{"id":"5a617c5b61fc7b35","repo":"microsoft/autogen","slug":"invalid-chunk-type-type-chunk","errorCode":null,"errorMessage":"Invalid chunk type: {type(chunk)}","messagePattern":"Invalid chunk type: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py","lineNumber":1104,"sourceCode":"        llm_messages = cls._get_compatible_context(model_client=model_client, messages=system_messages + all_messages)\n\n        tools = [tool for wb in workbench for tool in await wb.list_tools()] + handoff_tools\n\n        if model_client_stream:\n            model_result: Optional[CreateResult] = None\n\n            async for chunk in model_client.create_stream(\n                llm_messages,\n                tools=tools,\n                json_output=output_content_type,\n                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, full_message_id=message_id)\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(\n                llm_messages,\n                tools=tools,\n                cancellation_token=cancellation_token,\n                json_output=output_content_type,\n            )\n            yield model_result\n\n    @classmethod\n    async def _process_model_result(\n        cls,\n        model_result: CreateResult,\n        inner_messages: List[BaseAgentEvent | BaseChatMessage],\n        cancellation_token: CancellationToken,","sourceCodeStart":1086,"sourceCodeEnd":1122,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py#L1086-L1122","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Align package versions: pip install -U autogen-agentchat autogen-core autogen-ext so all packages share the same CreateResult type.","If using a custom model client, make create_stream yield only str chunks and exactly one CreateResult at the end.","Check `pip list | grep autogen` for duplicate/conflicting installs and reinstall into a clean venv."],"exampleFix":"# before (custom client)\nasync def create_stream(self, messages, **kwargs):\n    yield {\"text\": \"hello\"}          # dict chunk -> RuntimeError\n    yield self._make_result(...)\n\n# after\nasync def create_stream(self, messages, **kwargs):\n    yield \"hello\"\n    yield CreateResult(finish_reason=\"stop\", content=\"hello\", usage=..., cached=False)","handlingStrategy":"type-guard","validationCode":"from autogen_core.models import CreateResult\nasync def check_client_stream_protocol(client, sample_messages):\n    types_seen = set()\n    async for chunk in client.create_stream(sample_messages):\n        types_seen.add(type(chunk))\n    assert types_seen <= {str, CreateResult}, f\"non-conforming chunks: {types_seen - {str, CreateResult}}\"","typeGuard":"from autogen_core.models import CreateResult\n\ndef is_valid_stream_chunk(chunk) -> bool:\n    return isinstance(chunk, (str, CreateResult))","tryCatchPattern":"try:\n    async for msg in agent.run_stream(task=\"hi\"):\n        ...\nexcept RuntimeError as e:\n    if \"Invalid chunk type\" in str(e):\n        raise RuntimeError(\"model client does not conform to create_stream protocol; align autogen package versions or fix the client\") from e\n    raise","preventionTips":["Pin compatible versions of autogen-agentchat, autogen-core, and autogen-ext in one requirements set.","For custom model clients, unit-test create_stream yields only str chunks plus one final CreateResult.","Use a clean virtualenv per project to avoid duplicate autogen-core copies."],"tags":["python","streaming","model-client","version-mismatch","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}