airflow.providers.common.ai.mixins.hitl_review¶
Attributes¶
Classes¶
The host operator's attributes and hooks, and the mixin methods that call each other. |
|
Mixin that drives an iterative HITL review after the operator generates its first output. |
Module Contents¶
- class airflow.providers.common.ai.mixins.hitl_review.HITLReviewProtocol[source]¶
Bases:
ProtocolThe host operator’s attributes and hooks, and the mixin methods that call each other.
- hitl_timeout: datetime.timedelta | None[source]¶
- property prompt: str | collections.abc.Sequence[Any][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_reviewplugin:changes_requested: callsregenerate_with_feedback()and starts the next round.approved: returns the output.rejected: raisesHITLRejectException.
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_inputstate, holding no worker or pool slot. The plugin answers the request and resumes the task inresume_hitl_review(), which runs the next round in a new process; the conversation needed to regenerate is kept in the task state store throughdump_review_state()andload_review_state(). On older Airflow the task stays running and polls the human action XCom (airflow_hitl_review_human_action) everyhitl_poll_intervalseconds.The review stops after
hitl_timeout, one wall-clock budget for all rounds combined, ormax_hitl_iterations. When polling, it also stops, by re-raising the underlying exception, when pulling the human action XCom fails_MAX_CONSECUTIVE_XCOM_PULL_FAILUREStimes in a row, even ifhitl_timeoutisNone.Max iterations:
iterationcounts outputs shown to the reviewer (1 = initial, 2 = first regeneration, etc.). When the reviewer requests changes atiteration >= max_hitl_iterations, the mixin raisesHITLMaxIterationsErrorwithout calling the LLM. Withmax_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(strorSequence[UserContent])
And must implement
regenerate_with_feedback(). To park between rounds on Airflow 3.3+, it also implementsdump_review_state()andload_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 toregenerate_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_FAILUREStimes in a row.
- Return type:
- 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_timeoutelapsed 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.
- 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_historyto pass toregenerate_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;Nonewhen the review ran in this process.