nat.middleware.hitl.hitl_middleware#

Human-in-the-Loop (HITL) middleware for the NeMo Agent Toolkit.

Classes#

HITLMiddleware

Human-in-the-Loop middleware.

Module Contents#

class HITLMiddleware(
config: nat.middleware.hitl.hitl_middleware_config.HITLMiddlewareConfig,
builder: nat.builder.builder.Builder,
)#

Bases: nat.middleware.dynamic.dynamic_function_middleware.DynamicFunctionMiddleware

Human-in-the-Loop middleware.

Intercepts function calls to present human prompts before and/or after execution. Subclasses must implement _on_pre_invoke_response and _on_post_invoke_response to control what happens with the prompt response at each phase.

Initialize the HITL middleware.

Args:

config: HITL middleware configuration. builder: Workflow builder used for function discovery.

_hitl_config: nat.middleware.hitl.hitl_middleware_config.HITLMiddlewareConfig#
abstractmethod _on_pre_invoke_response(
response: nat.data_models.interactive.InteractionResponse,
context: nat.middleware.middleware.InvocationContext,
) → nat.middleware.middleware.InvocationContext | None#
Async:

Handle the pre-invoke prompt response and return an invocation decision.

Args:

response: The human prompt response collected before the function is called. context: Invocation context containing the function arguments.

Returns:

None to proceed unchanged, an InvocationContext with updated modified_args or modified_kwargs to alter inputs, or an InvocationContext with action set to InvocationAction.SKIP to bypass the function call entirely.

abstractmethod _on_post_invoke_response(
response: nat.data_models.interactive.InteractionResponse,
context: nat.middleware.middleware.InvocationContext,
) → nat.middleware.middleware.InvocationContext | None#
Async:

Handle the post-invoke prompt response and return an invocation decision.

Args:

response: The human prompt response collected after the function returns. context: Invocation context with output set to the function result for a single call or chunk.

Returns:

None to return the output as-is, or an InvocationContext with output updated to replace the function’s return value. For streaming calls, set output to None to suppress the chunk entirely.

async pre_invoke(
context: nat.middleware.middleware.InvocationContext,
) → nat.middleware.middleware.InvocationContext | None#

Present a human prompt before the function is called.

A no-op when pre_invoke_prompt is not configured.

Args:

context: Invocation context (output is None at this phase).

Returns:

The result of _on_pre_invoke_response.

async post_invoke(
context: nat.middleware.middleware.InvocationContext,
) → nat.middleware.middleware.InvocationContext | None#

Present a human prompt after the function returns.

A no-op when post_invoke_prompt is not configured.

Args:

context: Invocation context with the function output populated.

Returns:

The result of _on_post_invoke_response.