{"record":{"id":"e08b5daa926bdafa","repo":"microsoft/autogen","slug":"invalid-chunk-type-type-chunk-e08b5d","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/_code_executor_agent.py","lineNumber":816,"sourceCode":"        cancellation_token: CancellationToken,\n    ) -> AsyncGenerator[Union[CreateResult, ModelClientStreamingChunkEvent], None]:\n        \"\"\"\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:","sourceCodeStart":798,"sourceCodeEnd":834,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py#L798-L834","documentation":"CodeExecutorAgent's streaming generation path requires model_client.create_stream() to yield only str deltas and one final CreateResult. This error means some other chunk type appeared in the stream, violating the ChatCompletionClient protocol.","triggerScenarios":"Running a CodeExecutorAgent with model_client_stream=True against a custom or proxy client that yields provider-native chunk objects (dicts, lists, custom delta classes) instead of normalized str/CreateResult.","commonSituations":"Home-grown clients wrapping raw HTTP/SSE responses without normalization; replay test clients; thin decorators around real clients that pass through provider types.","solutions":["Fix the custom client's create_stream() to yield str deltas and end with a CreateResult.","Log type(chunk) to identify the offending object type.","Set model_client_stream=False to use the non-streaming create() path.","Use the official autogen-ext clients, which honor the contract."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"chunks = [c async for c in model_client.create_stream([SystemMessage(content=\"ping\")])]\nassert all(isinstance(c, (str, CreateResult)) for c in chunks), \"client emits invalid chunk types\"","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 ev in agent.on_messages_stream(msgs, ct):\n        ...\nexcept RuntimeError as e:\n    if \"Invalid chunk type\" in str(e):\n        # rerun with streaming disabled\n        ...\n    raise","preventionTips":["Use official autogen-ext clients for streaming.","Normalize custom client streams to str deltas + final CreateResult.","Add a protocol conformance test for custom clients."],"tags":["streaming","model-client","code-executor","protocol-violation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}