441. A single sensitive tool requires human approval.
552. The first turn always issues that tool call.
663. Rejection uses a custom message via ``tool_error_formatter``.
7+ 4. The example prints both the formatter output and the assistant's final reply.
78"""
89
910import asyncio
@@ -23,7 +24,7 @@ async def tool_error_formatter(args: ToolErrorFormatterArgs[None]) -> str | None
2324 """Build a simple output message for rejected tool calls."""
2425 if args .kind != "approval_rejected" :
2526 return None
26- # The defualt one is "Tool execution was not approved."
27+ # The default message is "Tool execution was not approved."
2728 return "Publish action was canceled because approval was rejected."
2829
2930
@@ -33,12 +34,27 @@ async def publish_announcement(title: str, body: str) -> str:
3334 return f"Published announcement '{ title } ' with body: { body } "
3435
3536
37+ def _find_formatter_output (result : object ) -> str | None :
38+ items = getattr (result , "new_items" , None )
39+ if not isinstance (items , list ):
40+ return None
41+
42+ for item in items :
43+ if getattr (item , "type" , None ) != "tool_call_output_item" :
44+ continue
45+ output = getattr (item , "output" , None )
46+ if isinstance (output , str ):
47+ return output
48+ return None
49+
50+
3651async def main () -> None :
3752 agent = Agent (
3853 name = "Operations Assistant" ,
3954 instructions = (
4055 "When a user asks to publish an announcement, call the publish_announcement tool directly. "
41- "Do not ask the user for approval in plain text; runtime approvals handle that."
56+ "Do not ask the user for approval in plain text; runtime approvals handle that. "
57+ "If the tool call is rejected, respond with the exact rejection message and nothing else."
4258 ),
4359 model_settings = ModelSettings (tool_choice = "publish_announcement" ),
4460 tools = [publish_announcement ],
@@ -69,6 +85,11 @@ async def main() -> None:
6985
7086 result = await Runner .run (agent , state , run_config = run_config )
7187
88+ formatter_output = _find_formatter_output (result )
89+ if formatter_output :
90+ print ("\n Formatter output:" )
91+ print (formatter_output )
92+
7293 print ("\n Final output:" )
7394 print (result .final_output )
7495
0 commit comments