产品介绍

Python human interaction API

See Human interaction and recovery for the task flow and HITL declarations for manifest fields and defaults. This section defines handlers, context, middleware, and resume actions. Host request formats are specified in AG-UI and A2A.

HITLHandler, context, and decisions

Import HITLHandler, HITLContext, HITLState, TriggerDecision, InteractionRequest, and ResumeDecision from dataagent. A handler must support parameterless construction. Instances are shared across calls; keep interaction state in graph state and checkpoints, and make methods idempotent for replay.

Method Returns Default behavior
trigger_policy(context, *, params) TriggerDecision Trigger on every matching phase or tool call
interaction_builder(context, *, params) InteractionRequest Use params.question and params.options, with a generic question as fallback
resume_handler(context, *, params) ResumeDecision Give the answer to the model with replan, without changing business state

Override with def or async def while retaining keyword-only params. Generators are unsupported. Return the corresponding decision object, not an equivalent dictionary.

HITLContext is a frozen dataclass with these fields:

Field Meaning
state Detached graph-state preview; direct mutations do not commit
runtime Native runtime; borrow host services through runtime.context
policy_id Full namespaced ID, such as my-plugin/clarification
event Current phase, one of the six below
tool_call Original tool call (id, name, args) during tool phases; otherwise None
tool_result Native ToolMessage or Command at after_tool; the state preview includes the tool's business updates
decision Trigger decision available while building the question and resuming
interaction Saved question available while resuming
answer User text available while resuming
Type Fields and defaults
TriggerDecision triggered=True, reason="", payload={}; an untriggered decision does not build a question
InteractionRequest Required nonblank question; options=[]
ResumeDecision action="replan", message=None, state_update={}; actions are continue, replan, and reject

Answers must be nonblank text. Blank or structured values pause again with validation_error. Options do not enforce enum validation; resume_handler owns domain checks. state_update accepts serializable business updates and cannot write messages, hitl_history, hitl_replans, or jump_to.

A handler can declare a state_schema, typically extending HITLState with business fields. The middleware combines compatible fields and rejects conflicting annotations for the same name. To commit an update:

def resume_handler(self, context, *, params):
    return ResumeDecision(
        action="continue",
        state_update={"selected_key": context.answer},
    )

Declare selected_key in the state schema first. Before-tool updates are visible through the tool's ToolRuntime.state. When the tool executes, changes commit in before-tool, tool, and after-tool order, applying each field's reducer. Explicit updates also commit on replan and reject.

Middleware and its six trigger phases

The middleware lives in dataagent.extensions.hitl. It saves pending interactions, interrupts, collects answers, and commits updates. Use async ainvoke or astream.

Interface Behavior
HITLMiddleware(policies, *, model_feedback=False) Accepts a list of HITLPolicy. Full IDs must be unique. model_feedback=True adds the shared feedback tool
HITLMiddleware.from_config(config, *, root, namespace="main", loader=None) Accepts HITLConfig or a dictionary. root anchors handler paths and namespace prefixes IDs. When enabled, constructs handlers and adds feedback; disabled configuration returns empty middleware
HITLPolicy(spec, handler, namespace="main") Binds a validated HITLPolicySpec and HITLHandler; the full ID is <namespace>/<spec.id>

Standard plugin assembly combines enabled policies into one middleware on the default root graph and loads instructions. Disabling a plugin's HITL skips its handlers and prompt while other sources remain active. Explicit from_config does not load or pass a prompt to the graph.

Phase Timing
before_agent Once per invocation before the first model call
before_model Before each model call
after_model After the model responds, before planned tools
before_tool Before an individual tool, including task
after_tool After the tool returns, before the parent graph commits the result
after_agent On the ending path; replanning can enter the model loop and reach review again

matcher is supported only at tool phases. To target a subagent, use matcher: task and inspect tool_call.args.subagent_type in the trigger. There is no automatic answer or interaction timeout.

request_human_feedback(question, reason="", options=None) is the model-visible shared tool; the framework injects ToolRuntime. It returns the raw answer in a successful ToolMessage. Only the first feedback call in a model response executes; other tool calls in that response are ignored, then the model replans. That response bypasses deterministic after_model policies, and the feedback tool itself bypasses before_tool and after_tool policies.

Plugin authors declare policies in the manifest. See manual middleware assembly when building your own graph.

Resume actions

Phase continue replan reject
before_agent Carry feedback into the model flow Continue with feedback without looping Follow the ending path with the reason
before_model Carry feedback into the model flow Continue with feedback without looping Follow the ending path with the reason
after_model Keep the plan Skip planned tools and return to the model Skip planned tools and follow the ending path
before_tool Execute after all matching policies continue Skip the call and give feedback to the model Decline the call and give the reason to the model
after_tool Keep the actual result Keep the actual result and give feedback to the model Report rejection after the call has executed
after_agent Finish normally Return to the model loop with feedback Finish with the rejection reason

Rejecting one tool call leaves the conversation usable. Enforce continuing restrictions in triggers or execution checks. Ending paths from state phases still pass native after_agent. Skipped plans receive error ToolMessages for their call IDs. Before-tool arguments remain unchanged. A return_direct tool returns directly on continue and returns to the model on replan or reject. Default triggers can ask again after replanning; one-time questions require a custom condition.

Pending interactions retain policy IDs, versions, phases, questions, and parameters, which must remain compatible when resuming. Resuming after a completed tool reuses its result. Handlers and interrupted tools can still replay; make external effects idempotent.