Skip to main content
Version: 2.23-unstable

Human-in-the-Loop

Module dataclasses

ConfirmationUIResult

Result of the confirmation UI interaction.

Arguments:

  • action: The action taken by the user such as "confirm", "reject", or "modify". This action type is not enforced to allow for custom actions to be implemented.
  • feedback: Optional feedback message from the user. For example, if the user rejects the tool execution, they might provide a reason for the rejection.
  • new_tool_params: Optional set of new parameters for the tool. For example, if the user chooses to modify the tool parameters, they can provide a new set of parameters here.

action

"confirm", "reject", "modify"

ToolExecutionDecision

Decision made regarding tool execution.

Arguments:

  • tool_name: The name of the tool to be executed.
  • execute: A boolean indicating whether to execute the tool with the provided parameters.
  • tool_call_id: Optional unique identifier for the tool call. This can be used to track and correlate the decision with a specific tool invocation.
  • feedback: Optional feedback message. For example, if the tool execution is rejected, this can contain the reason. Or if the tool parameters were modified, this can contain the modification details.
  • final_tool_params: Optional final parameters for the tool if execution is confirmed or modified.

ToolExecutionDecision.to_dict

python
def to_dict() -> dict[str, Any]

Convert the ToolExecutionDecision to a dictionary representation.

Returns:

A dictionary containing the tool execution decision details.

ToolExecutionDecision.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ToolExecutionDecision"

Populate the ToolExecutionDecision from a dictionary representation.

Arguments:

  • data: A dictionary containing the tool execution decision details.

Returns:

An instance of ToolExecutionDecision.

Module policies

AlwaysAskPolicy

Always ask for confirmation.

AlwaysAskPolicy.should_ask

python
def should_ask(tool_name: str, tool_description: str,
tool_params: dict[str, Any]) -> bool

Always ask for confirmation before executing the tool.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.

Returns:

Always returns True, indicating confirmation is needed.

AlwaysAskPolicy.update_after_confirmation

python
def update_after_confirmation(
tool_name: str, tool_description: str, tool_params: dict[str, Any],
confirmation_result: ConfirmationUIResult) -> None

Update the policy based on the confirmation UI result.

AlwaysAskPolicy.to_dict

python
def to_dict() -> dict[str, Any]

Serialize the policy to a dictionary.

AlwaysAskPolicy.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ConfirmationPolicy"

Deserialize the policy from a dictionary.

NeverAskPolicy

Never ask for confirmation.

NeverAskPolicy.should_ask

python
def should_ask(tool_name: str, tool_description: str,
tool_params: dict[str, Any]) -> bool

Never ask for confirmation, always proceed with tool execution.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.

Returns:

Always returns False, indicating no confirmation is needed.

NeverAskPolicy.update_after_confirmation

python
def update_after_confirmation(
tool_name: str, tool_description: str, tool_params: dict[str, Any],
confirmation_result: ConfirmationUIResult) -> None

Update the policy based on the confirmation UI result.

NeverAskPolicy.to_dict

python
def to_dict() -> dict[str, Any]

Serialize the policy to a dictionary.

NeverAskPolicy.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ConfirmationPolicy"

Deserialize the policy from a dictionary.

AskOncePolicy

Ask only once per tool with specific parameters.

AskOncePolicy.should_ask

python
def should_ask(tool_name: str, tool_description: str,
tool_params: dict[str, Any]) -> bool

Ask for confirmation only once per tool with specific parameters.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.

Returns:

True if confirmation is needed, False if already asked with the same parameters.

AskOncePolicy.update_after_confirmation

python
def update_after_confirmation(
tool_name: str, tool_description: str, tool_params: dict[str, Any],
confirmation_result: ConfirmationUIResult) -> None

Store the tool and parameters if the action was "confirm" to avoid asking again.

This method updates the internal state to remember that the user has already confirmed the execution of the tool with the given parameters.

Arguments:

  • tool_name: The name of the tool that was executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters that were passed to the tool.
  • confirmation_result: The result from the confirmation UI.

AskOncePolicy.to_dict

python
def to_dict() -> dict[str, Any]

Serialize the policy to a dictionary.

AskOncePolicy.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ConfirmationPolicy"

Deserialize the policy from a dictionary.

Module strategies

BlockingConfirmationStrategy

Confirmation strategy that blocks execution to gather user feedback.

BlockingConfirmationStrategy.__init__

python
def __init__(*,
confirmation_policy: ConfirmationPolicy,
confirmation_ui: ConfirmationUI,
reject_template: str = REJECTION_FEEDBACK_TEMPLATE,
modify_template: str = MODIFICATION_FEEDBACK_TEMPLATE,
user_feedback_template: str = USER_FEEDBACK_TEMPLATE) -> None

Initialize the BlockingConfirmationStrategy with a confirmation policy and UI.

Arguments:

  • confirmation_policy: The confirmation policy to determine when to ask for user confirmation.
  • confirmation_ui: The user interface to interact with the user for confirmation.
  • reject_template: Template for rejection feedback messages. It should include a {tool_name} placeholder.
  • modify_template: Template for modification feedback messages. It should include {tool_name} and {final_tool_params} placeholders.
  • user_feedback_template: Template for user feedback messages. It should include a {feedback} placeholder.

BlockingConfirmationStrategy.run

python
def run(
*,
tool_name: str,
tool_description: str,
tool_params: dict[str, Any],
tool_call_id: str | None = None,
confirmation_strategy_context: dict[str, Any] | None = None
) -> ToolExecutionDecision

Run the human-in-the-loop strategy for a given tool and its parameters.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.
  • tool_call_id: Optional unique identifier for the tool call. This can be used to track and correlate the decision with a specific tool invocation.
  • confirmation_strategy_context: Optional dictionary for passing request-scoped resources. Useful in web/server environments to provide per-request objects (e.g., WebSocket connections, async queues, Redis pub/sub clients) that strategies can use for non-blocking user interaction.

Returns:

A ToolExecutionDecision indicating whether to execute the tool with the given parameters, or a feedback message if rejected.

BlockingConfirmationStrategy.run_async

python
async def run_async(
*,
tool_name: str,
tool_description: str,
tool_params: dict[str, Any],
tool_call_id: str | None = None,
confirmation_strategy_context: dict[str, Any] | None = None
) -> ToolExecutionDecision

Async version of run. Calls the sync run() method by default.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.
  • tool_call_id: Optional unique identifier for the tool call.
  • confirmation_strategy_context: Optional dictionary for passing request-scoped resources.

Returns:

A ToolExecutionDecision indicating whether to execute the tool with the given parameters.

BlockingConfirmationStrategy.to_dict

python
def to_dict() -> dict[str, Any]

Serializes the BlockingConfirmationStrategy to a dictionary.

Returns:

Dictionary with serialized data.

BlockingConfirmationStrategy.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "BlockingConfirmationStrategy"

Deserializes the BlockingConfirmationStrategy from a dictionary.

Arguments:

  • data: Dictionary to deserialize from.

Returns:

Deserialized BlockingConfirmationStrategy.

Module user_interfaces

RichConsoleUI

Rich console interface for user interaction.

RichConsoleUI.get_user_confirmation

python
def get_user_confirmation(tool_name: str, tool_description: str,
tool_params: dict[str, Any]) -> ConfirmationUIResult

Get user confirmation for tool execution via rich console prompts.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.

Returns:

ConfirmationUIResult based on user input.

RichConsoleUI.to_dict

python
def to_dict() -> dict[str, Any]

Serializes the RichConsoleConfirmationUI to a dictionary.

Returns:

Dictionary with serialized data.

RichConsoleUI.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ConfirmationUI"

Deserialize the ConfirmationUI from a dictionary.

SimpleConsoleUI

Simple console interface using standard input/output.

SimpleConsoleUI.get_user_confirmation

python
def get_user_confirmation(tool_name: str, tool_description: str,
tool_params: dict[str, Any]) -> ConfirmationUIResult

Get user confirmation for tool execution via simple console prompts.

Arguments:

  • tool_name: The name of the tool to be executed.
  • tool_description: The description of the tool.
  • tool_params: The parameters to be passed to the tool.

SimpleConsoleUI.to_dict

python
def to_dict() -> dict[str, Any]

Serialize the UI to a dictionary.

SimpleConsoleUI.from_dict

python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ConfirmationUI"

Deserialize the ConfirmationUI from a dictionary.