MCPcopy
hub / github.com/openai/openai-agents-python / main

Function main

examples/memory/memory_session_hitl_example.py:56–117  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

54
55
56async def main():
57 # Create an agent with a tool that requires approval
58 agent = Agent(
59 name="HITL Assistant",
60 instructions="You help users with information. Always use available tools when appropriate. Keep responses concise.",
61 tools=[get_weather],
62 )
63
64 # Create an in-memory SQLite session instance that will persist across runs
65 session = SQLiteSession(":memory:")
66 session_id = session.session_id
67
68 print("=== Memory Session + HITL Example ===")
69 print(f"Session id: {session_id}")
70 print("Enter a message to chat with the agent. Submit an empty line to exit.")
71 print("The agent will ask for approval before using tools.\n")
72
73 auto_mode = is_auto_mode()
74
75 while True:
76 # Get user input
77 if auto_mode:
78 user_message = input_with_fallback("You: ", "What's the weather in Oakland?")
79 else:
80 print("You: ", end="", flush=True)
81 loop = asyncio.get_event_loop()
82 user_message = await loop.run_in_executor(None, input)
83
84 if not user_message.strip():
85 break
86
87 # Run the agent
88 result = await Runner.run(agent, user_message, session=session)
89
90 # Handle interruptions (tool approvals)
91 while result.interruptions:
92 # Get the run state
93 state = result.to_state()
94
95 for interruption in result.interruptions:
96 tool_name = interruption.name or "Unknown tool"
97 args = interruption.arguments or "(no arguments)"
98
99 approved = await prompt_yes_no(
100 f"Agent {interruption.agent.name} wants to call '{tool_name}' with {args}. Approve?"
101 )
102
103 if approved:
104 state.approve(interruption)
105 print("Approved tool call.")
106 else:
107 state.reject(interruption)
108 print("Rejected tool call.")
109
110 # Resume the run with the updated state
111 result = await Runner.run(agent, state, session=session)
112
113 # Display the response

Callers 1

Calls 10

AgentClass · 0.90
SQLiteSessionClass · 0.90
is_auto_modeFunction · 0.90
input_with_fallbackFunction · 0.90
get_event_loopMethod · 0.80
approveMethod · 0.80
rejectMethod · 0.80
prompt_yes_noFunction · 0.70
runMethod · 0.45
to_stateMethod · 0.45

Tested by

no test coverage detected