PrintMessageMiddleware is a built-in IMiddleware that pretty print IMessage to console.
Note
PrintMessageMiddleware support the following IMessage types:
- TextMessage
- MultiModalMessage
- ToolCallMessage
- ToolCallResultMessage
- Message
- (streaming) TextMessageUpdate
- (streaming) ToolCallMessageUpdate
Use PrintMessageMiddleware in an agent
You can use RegisterPrintMessage to register the PrintMessageMiddleware to an agent.
var agentWithPrintMessageMiddleware = agent
.RegisterPrintMessage();
await agentWithPrintMessageMiddleware.SendAsync("write a long poem");
RegisterPrintMessage will format the message and print it to console

Streaming message support
PrintMessageMiddleware also supports streaming message types like TextMessageUpdate and ToolCallMessageUpdate. If you register PrintMessageMiddleware to a IStreamingAgent, it will format the streaming message and print it to console if the message is of supported type.
var streamingAgent = new OpenAIChatAgent(gpt4o, "assistant")
.RegisterMessageConnector()
.RegisterPrintMessage();
await streamingAgent.SendAsync("write a long poem");
