dabla opened a new pull request, #77438:
URL: https://github.com/apache/airflow/pull/77438

   Adds two optional callables to `@task.agent`, for what happens after the 
agent run. The decorated function only builds the prompt, so today the task can 
neither shape its result nor react to a failed run.
   
   ```python
   def contexts_of_batch(output: dict[str, str], batch: dict) -> dict:
       return {"document": batch["document"], "contexts": output}
   
   
   def no_contexts_when_refused(error: Exception, batch: dict) -> dict[str, 
str]:
       if isinstance(error, ModelHTTPError) and error.status_code == 400:
           return {}  # content filter: go on without contexts for this batch
       raise error  # 429, 5xx, ...: for the retry policy
   
   
   @task.agent(
       llm_conn_id="pydanticai_default",
       system_prompt=SYSTEM_PROMPT,
       output_type=dict[str, str],
       output_processor=contexts_of_batch,
       error_handler=no_contexts_when_refused,
   )
   def contextualise_batch(batch: dict) -> str:
       ...  # returns the prompt
   
   
   contexts = contextualise_batch.iterate(batch=batches)
   ```
   
   With `.iterate()` (#62922) or `.expand()`, the results of the batches come 
back as a list: `output_processor` lets each result say which batch it is for. 
With #74201 the function can be `async def`, and the iterated runs then share 
one event loop.
   
   - `output_processor(output, ...)` is called with the agent's output, as the 
task would return it, and returns the task's result. A mapped task can then say 
which input its answer is for, so results are joined by key instead of by 
position.
   - `error_handler(error, ...)` is called with the exception of a failed agent 
run. It returns the output to use instead, which then goes through 
`output_processor`, or raises. Today a request the model refuses, such as a 
content-filter 400, fails the task again on every retry.
   - After their first parameter, both name the arguments of the decorated 
function they need (`batch`, `ti`, ...), as the function itself does.
   
   This is the follow-up mentioned on #74201; it is based on main and does not 
need that PR.
   
   **Behaviour to review**
   
   - A run that paused for tool approval or for a review resumes in 
`resume_after_tool_approval` or `resume_hitl_review`, not in `execute`. The 
decorated operator routes all three through the callbacks, so a resumed run is 
processed the same way.
   - A pause (`TaskDeferred`, `TaskAwaitingInput`) is a `BaseException` and is 
never handed to `error_handler`.
   - An error of the decorated function itself is not handed to `error_handler`.
   - `error_handler` is refused with `durable=True` when the task is built: the 
journal of the failed run would stay behind a task that succeeded.
   - After a handled failure the saved usage budget is cleared, as it is after 
a successful run.
   
   **Open questions**
   
   1. Is this shape acceptable, or would you rather expose pydantic-ai's output 
functions and deps?
   2. Should `@task.llm` get the same two callables?
   3. #74201 edits the same decorator. Whichever lands second calls the 
callbacks on both paths, `execute` and `aexecute`; on the async path they would 
run in a worker thread.
   
   Tests: 13 new unit tests, two of them through the real 
`AgentOperator.execute` with only the model stubbed.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to