How can I rewrite the assistant’s response without affecting tool call results? #5262
-
|
Description: I have a tool that returns structured data, which is consumed by the frontend to render the main UI. Currently, the assistant’s output directly reflects this structured data. However, I would like to modify the assistant’s visible response to something like: “I have completed data retrieval.” At the same time, I must preserve the original tool call result (TOOL_CALL_RESULT) unchanged, since the frontend depends on that structured data for rendering. I’ve explored using middleware to achieve this, but so far I haven’t found a way to: Override or rewrite only the assistant’s textual response Question: What is the recommended way to customize or override the assistant’s response content without modifying or interfering with the tool call result payload? thanks🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
You can do this with an Non-streaming — 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
# Preserve function_call / function_result / function_approval_* contents.
# Only replace plain text contents.
has_text = any(c.type == "text" for c in msg.contents)
if has_text:
msg.contents = [
Content.from_text("I have completed data retrieval.")
if c.type == "text"
else c
for c in msg.contents
]
agent = Agent(chat_client=client, middleware=[rewrite_assistant_text])The frontend still receives the original Streaming: Use @agent_middleware
async def rewrite_assistant_text_stream(context: AgentContext, call_next):
def transform(update):
update.contents = [
Content.from_text("I have completed data retrieval.")
if c.type == "text"
else c
for c in update.contents
]
return update
context.stream_transform_hooks.append(transform)
await call_next()Chat-level alternative: If you want to intercept before the agent's tool loop finalizes the Tool calls and tool results are separate |
Beta Was this translation helpful? Give feedback.
You can do this with an
AgentMiddleware(orChatMiddleware) — the middleware pipeline is designed so you can rewrite the assistant's visible text after execution completes while leaving tool-call contents untouched.Message.contentsis a list of typedContentitems, so you filter bycontent.typeand only replace"text".Non-streaming —
AgentMiddleware: