- The interaction flow is unpredictable: The agent might need different information based on context
- You want a conversational experience: Let the agent guide the user through a form-like interaction
- The agent should be intelligent about what it needs: Rather than blindly requesting predefined fields, the agent determines what’s missing
How It Works
TheUserControlFlowTools toolkit provides your agent with a special get_user_input tool. When the agent realizes it’s missing information:
- Agent calls
get_user_inputwith a list of fields it needs filled - Execution pauses and requirements are added to the returned
RunOutput user_input_schemapopulated in the requirement, with the input schema the agent created- You collect the user’s input and set field values in
user_input_schema - Call
continue_run()to resume with the filled values - Repeat if needed: Agent may request more information based on previous responses
get_user_input to collect that information. Pretty smart!
Understanding the get_user_input Tool
When your agent calls the get_user_input tool, it provides a list of fields using this format:
tool.user_input_schema as UserInputField objects that you can iterate through and fill. For a detailed breakdown of the UserInputField structure, see Understanding UserInputField.
The While Loop Pattern
Notice thewhile run_response.is_paused: loop? This is crucial for dynamic user input, because the agent might request input multiple times:
- First ask for email details
- Send the email
- Realize it needs meeting details
- Pause again to request those fields
- Complete the task
Important: Always check
field.value before prompting. If the agent has already filled a field based on context (like extracting it from the user’s message), field.value won’t be None and you shouldn’t overwrite it.Customizing Toolkit Behavior
TheUserControlFlowTools toolkit comes with default instructions that guide the agent, but you can customize them:
Handling Pre-Filled Values
The agent can pre-fill some fields based on the conversation context. This works the same way as in User Input—always checkfield.value before prompting:
Best Practices
- Always use a while loop: The agent may need multiple rounds of input
- Check field values: Don’t overwrite fields the agent has already filled
- Provide clear prompts: Use the
field.descriptionto help users understand what’s needed - Validate input: Add your own validation before setting
field.value - Handle interruptions gracefully: Store
run_idto resume later if needed
Async Support
Dynamic user input works seamlessly with async agents. Usearun() and acontinue_run() for asynchronous flows:
Streaming Support
Dynamic user input also works with streaming. The agent will emit events until it needs user input, then pause:When to Use This Pattern
Use Dynamic User Input when:- The agent needs to adapt its questions based on previous responses
- You want the agent to intelligently determine what information is missing
- The interaction flow changes based on context
- You know exactly which tool fields require user input upfront
- The input requirements are always the same
- You want more explicit control over what gets asked
Usage Examples
Agentic User Input
Let the agent dynamically request user input