Skip to content
Discussion options

You must be logged in to vote

You can do this with an AgentMiddleware (or ChatMiddleware) — the middleware pipeline is designed so you can rewrite the assistant's visible text after execution completes while leaving tool-call contents untouched. Message.contents is a list of typed Content items, so you filter by content.type and only replace "text".

Non-streaming — AgentMiddleware:

from agent_framework import AgentContext, agent_middleware, Content


@agent_middleware
async def rewrite_assistant_text(context: AgentContext, call_next):
    await call_next()
    response = context.result
    if not response:
        return

    for msg in response.messages:
        if msg.role != "assistant":
            continue
        

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Aimee-min-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
agents Issues related to single agents agent middleware
2 participants