microsoft/autogen · error · ValueError

Expected text content in the last message: {last_message.con

Error message

Expected text content in the last message: {last_message.content}

What it means

The last assistant message HAS content parts, but none of type 'text' — e.g. only image_file parts (image generation) or an unexpected part type. on_messages_stream hard-codes extraction of text content to build its TextMessage, so non-text-only replies cannot be returned and raise ValueError showing the actual content.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py:515

        # Get messages after run completion
        assistant_messages: AsyncCursorPage[Message] = await cancellation_token.link_future(
            asyncio.ensure_future(
                self._client.beta.threads.messages.list(thread_id=self._thread_id, order="desc", limit=1)  # type: ignore[reportDeprecated]
            )
        )

        if not assistant_messages.data:
            raise ValueError("No messages received from assistant")

        # Get the last message's content
        last_message = assistant_messages.data[0]
        if not last_message.content:
            raise ValueError(f"No content in the last message: {last_message}")

        # Extract text content
        text_content = [content for content in last_message.content if content.type == "text"]
        if not text_content:
            raise ValueError(f"Expected text content in the last message: {last_message.content}")

        # Return the assistant's response as a Response with inner messages
        chat_message = TextMessage(source=self.name, content=text_content[0].text.value)
        yield Response(chat_message=chat_message, inner_messages=inner_messages)

    async def handle_incoming_message(self, message: BaseChatMessage, cancellation_token: CancellationToken) -> None:
        """Handle regular text messages by adding them to the thread."""
        content: str | List[MessageContentPartParam] | None = None
        llm_message = message.to_model_message()
        if isinstance(llm_message.content, str):
            content = llm_message.content
        else:
            content = []
            for c in llm_message.content:
                if isinstance(c, str):
                    content.append(TextContentBlockParam(text=c, type="text"))
                elif isinstance(c, Image):
                    content.append(ImageURLContentBlockParam(image_url=ImageURLParam(url=c.data_uri), type="image_url"))

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Instruct the assistant to always include a textual answer in its final message.
  2. If you need image/file outputs, consume the thread messages yourself via agent.messages.list(...) instead of relying on on_messages' TextMessage extraction.
  3. Inspect the printed last_message.content in the error to see which part types were returned, then adapt consumption accordingly.

Example fix

# before
instructions = "Generate an image of a cat."

# after
instructions = (
    "Generate an image of a cat, then always describe the image "
    "in a text reply to the user."
)
Defensive patterns

Strategy: validation

Validate before calling

page = await agent.messages.list(thread_id=agent._thread_id, order="desc", limit=1)
has_text = page.data and any(
    getattr(part, "type", None) == "text" for part in (page.data[0].content or [])
)
if not has_text:
    # request a textual follow-up instead of calling on_messages

Try / catch

try:
    resp = await agent.on_messages(msgs, ct)
except ValueError as e:
    if "Expected text content" in str(e):
        page = await agent.messages.list(thread_id=agent._thread_id, order="desc", limit=1)
        # consume image/file parts from page yourself
    else:
        raise

Prevention

When it happens

Trigger: An assistant configured for image generation returning only an image_file content part; assistants emitting annotations-only or other non-text parts as the final message.

Common situations: Using the Assistants API for image output while expecting text; instructions that let the model answer with a file/image and no prose.

Related errors


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