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

Function main

examples/memory/openai_session_hitl_example.py:56–115  ·  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 a session instance that will persist across runs
65 session = OpenAIConversationsSession()
66
67 print("=== OpenAI Session + HITL Example ===")
68 print("Enter a message to chat with the agent. Submit an empty line to exit.")
69 print("The agent will ask for approval before using tools.\n")
70
71 auto_mode = is_auto_mode()
72
73 while True:
74 # Get user input
75 if auto_mode:
76 user_message = input_with_fallback("You: ", "What's the weather in Oakland?")
77 else:
78 print("You: ", end="", flush=True)
79 loop = asyncio.get_event_loop()
80 user_message = await loop.run_in_executor(None, input)
81
82 if not user_message.strip():
83 break
84
85 # Run the agent
86 result = await Runner.run(agent, user_message, session=session)
87
88 # Handle interruptions (tool approvals)
89 while result.interruptions:
90 # Get the run state
91 state = result.to_state()
92
93 for interruption in result.interruptions:
94 tool_name = interruption.name or "Unknown tool"
95 args = interruption.arguments or "(no arguments)"
96
97 approved = await prompt_yes_no(
98 f"Agent {interruption.agent.name} wants to call '{tool_name}' with {args}. Approve?"
99 )
100
101 if approved:
102 state.approve(interruption)
103 print("Approved tool call.")
104 else:
105 state.reject(interruption)
106 print("Rejected tool call.")
107
108 # Resume the run with the updated state
109 result = await Runner.run(agent, state, session=session)
110
111 # Display the response
112 reply = result.final_output or "[No final output produced]"
113 print(f"Assistant: {reply}\n")

Callers 1

Calls 10

AgentClass · 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