airflow.providers.common.ai.mixins.hitl_review

Attributes

log

Classes

HITLReviewProtocol

The host operator's attributes and hooks, and the mixin methods that call each other.

HITLReviewMixin

Mixin that drives an iterative HITL review after the operator generates its first output.

Module Contents

airflow.providers.common.ai.mixins.hitl_review.log[source]
class airflow.providers.common.ai.mixins.hitl_review.HITLReviewProtocol[source]

Bases: Protocol

The host operator’s attributes and hooks, and the mixin methods that call each other.

enable_hitl_review: bool[source]
max_hitl_iterations: int[source]
hitl_timeout: datetime.timedelta | None[source]
hitl_poll_interval: float[source]
task_id: str[source]
log: Any[source]
property prompt: str | collections.abc.Sequence[Any][source]
regenerate_with_feedback(*, feedback, message_history)[source]
dump_review_state(message_history)[source]
load_review_state(context, state)[source]
complete_hitl_review(context, approved_output, *, state)[source]
class airflow.providers.common.ai.mixins.hitl_review.HITLReviewMixin[source]

Mixin that drives an iterative HITL review after the operator generates its first output.

Each round, the mixin pushes session metadata and the agent output to XCom and waits for the reviewer to act in the hitl_review plugin:

  • changes_requested: calls regenerate_with_feedback() and starts the next round.

  • approved: returns the output.

  • rejected: raises HITLRejectException.

How it waits: on Airflow 3.3+ each round is also a Human-in-the-loop request on the task instance, and the task parks in the awaiting_input state, holding no worker or pool slot. The plugin answers the request and resumes the task in resume_hitl_review(), which runs the next round in a new process; the conversation needed to regenerate is kept in the task state store through dump_review_state() and load_review_state(). On older Airflow the task stays running and polls the human action XCom (airflow_hitl_review_human_action) every hitl_poll_interval seconds.

The review stops after hitl_timeout, one wall-clock budget for all rounds combined, or max_hitl_iterations. When polling, it also stops, by re-raising the underlying exception, when pulling the human action XCom fails _MAX_CONSECUTIVE_XCOM_PULL_FAILURES times in a row, even if hitl_timeout is None.

Max iterations: iteration counts outputs shown to the reviewer (1 = initial, 2 = first regeneration, etc.). When the reviewer requests changes at iteration >= max_hitl_iterations, the mixin raises HITLMaxIterationsError without calling the LLM. With max_hitl_iterations=5, the reviewer can request changes at most 4 times (iterations 1–4); the fifth output is the last chance to approve or reject.

All agent outputs and human feedback are persisted as iteration-keyed XCom entries (airflow_hitl_review_agent_output_1, airflow_hitl_review_human_feedback_1, etc.) for full auditability.

Operators using this mixin must set:

  • enable_hitl_review (bool)

  • hitl_timeout (timedelta | None)

  • hitl_poll_interval (float, seconds)

  • prompt (str or Sequence[UserContent])

And must implement regenerate_with_feedback(). To park between rounds on Airflow 3.3+, it also implements dump_review_state() and load_review_state(); without them the review polls, as on older Airflow.

run_hitl_review(context, output, *, message_history=None)[source]

Start the HITL review of output.

On Airflow 3.3+ this parks the task and never returns; the review continues in resume_hitl_review(). Otherwise it blocks until the review ends.

Parameters:
  • context (airflow.sdk.Context) – Airflow task context.

  • output (Any) – Initial LLM output (any pydantic-serializable value).

  • message_history (Any) – Provider-specific conversation state (e.g. pydantic-ai list[ModelMessage]). Passed to regenerate_with_feedback() on each iteration.

Returns:

The approved output as a string (JSON for non-str values pydantic can serialize).

Raises:
  • TaskAwaitingInput – When the review parks the task until the reviewer acts.

  • HITLMaxIterationsError – When max iterations reached without approval.

  • HITLRejectException – When the reviewer rejects the output.

  • HITLTimeoutError – When hitl_timeout elapses with no response.

  • Exception – The last XCom pull exception, when pulling the human action fails _MAX_CONSECUTIVE_XCOM_PULL_FAILURES times in a row.

Return type:

str

resume_hitl_review(context, iteration, deadline, state_sha256, event)[source]

Continue a review parked by run_hitl_review() with the reviewer’s answer.

Approving returns complete_hitl_review(); requesting changes regenerates and parks the task again for the next round.

Raises:
  • HITLRejectException – When the reviewer rejects the output.

  • HITLMaxIterationsError – When changes are requested on the last allowed iteration.

  • HITLTimeoutError – When hitl_timeout elapsed before the reviewer answered.

  • HITLTriggerEventError – When the resume event reports another error.

abstractmethod regenerate_with_feedback(*, feedback, message_history)[source]

Re-run the LLM with reviewer feedback.

Parameters:
  • feedback (str) – The reviewer’s feedback text.

  • message_history (Any) – Provider-specific conversation state.

Returns:

(new_output_string, updated_message_history).

Return type:

tuple[str, Any]

abstractmethod dump_review_state(message_history)[source]

Serialize what load_review_state() needs to regenerate after the task resumes.

Called on Airflow 3.3+ before every park; the string is kept in the task state store.

Parameters:

message_history (Any) – Provider-specific conversation state.

abstractmethod load_review_state(context, state)[source]

Restore what dump_review_state() saved, in the resumed task.

Called before each regeneration.

Returns:

The message_history to pass to regenerate_with_feedback().

Return type:

Any

complete_hitl_review(context, approved_output, *, state)[source]

Return the task’s result once the review is approved.

Parameters:
  • approved_output (str) – The approved output as the reviewer read it.

  • state (str | None) – What dump_review_state() saved for the approved round, when the task resumed after parking; None when the review ran in this process.

Was this entry helpful?