Skip to content

Render only the returned value in TaskFlow decorators that template it - #76244

Merged
potiuk merged 2 commits into
apache:mainfrom
potiuk:taskflow-decorators-render-returned-value-only
Nov 2, 2026
Merged

potiuk merged 2 commits into
apache:mainfrom
potiuk:taskflow-decorators-render-returned-value-only

Conversation

@potiuk

@potiuk potiuk commented Nov 1, 2026

Copy link
Copy Markdown
Member

The TaskFlow decorators that build their main field from the function's return value (@task.bash, @task.sql, @task.analytics, @task.kubernetes_cmd and the six common.ai decorators @task.llm, @task.agent, @task.llm_sql, @task.llm_branch, @task.llm_file_analysis, @task.llm_schema_compare) called self.render_template_fields(context) inside execute() to render that returned value. The task runner has already rendered every template field once before execute(), so that call rendered all the other template fields (env, cwd, op_args, op_kwargs, parameters, system_prompt, ...) a second time. A value that came out of the first pass containing {{ ... }} was rendered again, which BashOperator and the other plain operators never do.

This renders only the returned value (self.bash_command = self.render_template(self.bash_command, context) and the equivalent for sql, queries, the generated pod command and prompt), the same approach as #71402. The returned value is still rendered as before, including template_ext file lookup. @task.kubernetes_cmd also rendered all fields once more before calling the function; that call is dropped too.

The common.ai docs did not say that the returned prompt is rendered as a template, and the ticket-triage example formatted the ticket text into it with an f-string. llm.rst now explains it and shows how to reference a function argument by name in the returned template, and the example uses that.

Changes:

  • Render only the returned value in the 10 decorators (11 call sites, 4 providers).
  • A test per decorator that renders the fields once, as the task runner does, and checks that other fields keep their rendered value while the returned value is rendered.
  • A common.ai test that an argument referenced in the returned prompt is inserted as is.
  • The kubernetes decorator test helper renders the fields before execute(), as at runtime.
  • Changelog notes for the four providers; llm.rst and example_llm_analysis_pipeline.py updated.

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Opus 5

Generated-by: Claude Opus 5 following the guidelines

The decorators that build their main field from the function's return value
called render_template_fields() inside execute() to render it, which rendered
every other template field a second time after the task runner had already
rendered them. Render only the returned value, as apache#71402 did, and document how
to reference function arguments in the returned @task.llm prompt.

Generated-by: Claude Opus 5
Only op_kwargs were merged into the context, so a prompt that referenced an argument by name
failed with an undefined variable when the task was called positionally, as in the docs'
summarize("...") example. The six common.ai decorators now merge the bound call arguments,
without defaults so that a ti=None default cannot shadow the context's ti, just before rendering
the returned prompt. The docs, the examples the page includes and the Usage docstrings reference
the argument by name instead of formatting it in with an f-string.

The returned-command test of @task.bash runs on Airflow 3 only: on Airflow 2 the decorator is the
one in Airflow core.

Generated-by: Claude Opus 5
@potiuk
potiuk force-pushed the taskflow-decorators-render-returned-value-only branch from d7ffcd5 to 52de6de Compare November 1, 2026 23:18
@potiuk
potiuk enabled auto-merge (squash) November 1, 2026 23:51
@potiuk
potiuk merged commit bbe441e into apache:main Nov 2, 2026
150 checks passed
@potiuk
potiuk deleted the taskflow-decorators-render-returned-value-only branch November 2, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants