Result Overrides
Result override middleware intercepts the AgentResponse returned by an agent and modifies its content before the response reaches the caller. Unlike termination middleware (which skips the agent entirely), result override middleware always calls the agent and then transforms what comes back.
Why Result Overrides?
- Content transformation — Reformat, translate, or clean up the text in every reply.
- Response enrichment — Append disclaimers, timestamps, citation links, or confidence scores.
- Conditional replacement — Replace the entire response when a business rule is triggered (e.g., the agent returned an empty answer).
The Override Pattern
The shape of a result override middleware function is identical to any other AgentRunMiddleware. The difference is in what happens inside:
The key constructor is AgentResponse(IList<ChatMessage>): supply the transformed message list and the framework wraps it in a proper response object.
Demo: Disclaimer Middleware
The full demo is in Lesson7/Demo1_ResultOverrides.cs.
Step 1 – Build the base agent
Step 2 – Register the override middleware
Step 3 – Call the agent normally
The caller sees the modified response. The middleware is completely transparent — no changes are needed at the call site.
Comparison: Termination vs. Result Override
| Pattern | Calls innerAgent? | What is returned? | Typical use |
|---|---|---|---|
| Termination (Lesson 6) | No | Synthetic response constructed by middleware | Block forbidden input before the LLM is called |
| Result Override (Lesson 7) | Yes | Agent response transformed by middleware | Enrich, format, or conditionally replace output |
Common Override Scenarios
| Scenario | What the middleware modifies |
|---|---|
| Append disclaimer | Concatenate fixed text to msg.Text |
| Add timestamp header | Prepend $"[{DateTime.UtcNow:u}]\n" to each assistant message |
| Translate to another language | Call a translation API on msg.Text and replace the content |
| Fallback on empty reply | Check if msg.Text is empty; return a default message if so |
| Redact PII | Run a regex substitution on msg.Text before returning |
Key Types
| Type / Member | Package | Role |
|---|---|---|
AgentResponse(IList<ChatMessage>) | Microsoft.Agents.AI | Construct the modified response to return |
AgentResponse.Messages | Microsoft.Agents.AI | The list of messages produced by the agent run |
AIAgent.AsBuilder().Use().Build() | Microsoft.Agents.AI | Register middleware on an existing agent |
ChatMessage(ChatRole, string) | Microsoft.Extensions.AI | Create a replacement or enriched message |
Required Packages
Reference
Microsoft Agent Framework – Middleware: Result Overrides