Table of Contents

PrintMessageMiddleware is a built-in IMiddleware that pretty print IMessage to console.

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 image

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");

image