microsoft/semantic-kernel · error · AgentInvokeException

Unexpected reply type from `a_generate_reply`: {type(reply)}

Error message

Unexpected reply type from `a_generate_reply`: {type(reply)}

What it means

Thrown by AutoGenConversableAgent._create_reply_content when the reply returned by the AutoGen conversable_agent's a_generate_reply is neither a str nor a dict. The translator only knows how to map those two shapes into a ChatMessageContent, so any other object (e.g. a custom autogen message class, None, a list) is rejected.

Source

Thrown at python/semantic_kernel/agents/autogen/autogen_conversable_agent.py:314

                    items.append(
                        FunctionResultContent(
                            id=tool_response.get("tool_call_id"),
                            result=tool_response.get("content"),
                        )
                    )

        return ChatMessageContent(role=role, items=items, name=name)  # type: ignore

    async def _create_reply_content(
        self, reply: str | dict[str, Any], thread: AgentThread
    ) -> AgentResponseItem[ChatMessageContent]:
        response: ChatMessageContent
        if isinstance(reply, str):
            response = ChatMessageContent(content=reply, role=AuthorRole.ASSISTANT)
        elif isinstance(reply, dict):
            response = ChatMessageContent(**reply)
        else:
            raise AgentInvokeException(f"Unexpected reply type from `a_generate_reply`: {type(reply)}")

        await thread.on_new_message(response)

        return AgentResponseItem(
            message=response,
            thread=thread,
        )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure the underlying ConversableAgent's reply functions return a plain str or a dict compatible with ChatMessageContent kwargs.
  2. Normalize the reply before invoke by wrapping/replacing custom reply logic.
  3. Pin compatible autogen and semantic-kernel versions.

Example fix

# before
# custom reply function returns an object
conv.register_reply(lambda *a, **k: MyMsgObject(...))

# after
conv.register_reply(lambda *a, **k: {'content': 'hello', 'role': 'assistant'})
Defensive patterns

Strategy: try-catch

Type guard

def reply_is_supported(reply: object) -> bool:
    return isinstance(reply, (str, dict))

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentInvokeException
try:
    async for r in agent.invoke(messages=msg, thread=thread):
        ...
except AgentInvokeException as e:
    if 'Unexpected reply type' in str(e):
        logger.error('Underlying autogen agent returned an unsupported reply shape; check custom reply functions')
    raise

Prevention

When it happens

Trigger: A customized autogen ConversableAgent whose reply producer returns a non-str/non-dict object; an autogen version/extension that returns a richer message type; a None reply from a misconfigured reply function.

Common situations: Registering custom reply functions on the underlying conversable_agent that return objects; version mismatch between autogen and the SK wrapper's expectations.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/499933c0b0bf341c. Report an issue: GitHub.