Skip to content

fix: UserInputTool prompts now display in Gradio interface#1954

Draft
wingding12 wants to merge 1 commit intohuggingface:mainfrom
wingding12:fix/user-input-tool-gradio-805
Draft

fix: UserInputTool prompts now display in Gradio interface#1954
wingding12 wants to merge 1 commit intohuggingface:mainfrom
wingding12:fix/user-input-tool-gradio-805

Conversation

@wingding12
Copy link

Summary

Fixes #805

Previously, UserInputTool used Python's input() function directly, which only works in terminal contexts. When used with GradioUI, the prompts would appear in the terminal instead of the Gradio chat interface.

Changes

  • UserInputTool now accepts an optional get_user_input callback parameter that can be used to customize how user input is collected
  • Default behavior is unchanged (uses terminal input())
  • GradioUI automatically configures UserInputTool instances to use a handler that displays questions in the chat interface
  • When the agent needs user input, it pauses execution and shows the question in the chat
  • The user's next message is treated as the answer to the pending question

New Classes

  • GradioUserInputHandler: Manages the input request/response flow between the agent and the Gradio interface
  • UserInputRequiredException: Exception raised to pause agent execution when user input is needed

Example Usage

from smolagents import CodeAgent, GradioUI, UserInputTool

agent = CodeAgent(
    tools=[UserInputTool()],  # Works automatically with GradioUI
    model=model
)

# Terminal usage - uses input() as before
tool = UserInputTool()
answer = tool.forward("What is your name?")

# Custom handler
def my_handler(question: str) -> str:
    return get_input_from_my_ui(question)

tool = UserInputTool(get_user_input=my_handler)

Test Plan

  • Added tests for UserInputTool with custom input handlers
  • All existing GradioUI tests pass
  • Verified backward compatibility - default behavior unchanged

Fixes huggingface#805

Previously, UserInputTool used Python's input() function directly, which
only works in terminal contexts. When used with GradioUI, the prompts
would appear in the terminal instead of the Gradio chat interface.

Changes:
- UserInputTool now accepts an optional `get_user_input` callback that
  can be used to customize how user input is collected
- Default behavior unchanged (uses terminal input())
- GradioUI automatically configures UserInputTool instances to use a
  handler that displays questions in the chat interface
- When the agent needs user input, it pauses and shows the question
- The user's next message is treated as the answer

Added GradioUserInputHandler class to manage the input request/response
flow between the agent and the Gradio interface.

Added tests for UserInputTool with custom input handlers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

userinputtool() Prompts Not Displaying in Gradio Interface

1 participant