-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Support TaskFlow call syntax on stub tasks for the Lang SDK #69757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jason810496
wants to merge
5
commits into
apache:main
Choose a base branch
from
jason810496:feature/lang-sdk/taskflow-stub-dag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,725
−64
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
486095a
Cut common-compat 1.19.0 with the SDK surface the stub decorator needs
jason810496 a36d43e
Support TaskFlow call syntax on @task.stub tasks
jason810496 5427831
Ship stub arg_bindings in a new execution API version
jason810496 69f514b
Deliver stub arg bindings to SDK runtimes via the supervisor schema
jason810496 1b7e1d6
Reject upstream outputs nested inside stub literal collections
jason810496 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
airflow-core/src/airflow/api_fastapi/execution_api/datamodels/task_arg_binding.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Positional-argument binding spec for stub (foreign-runtime) tasks. | ||
|
|
||
| Captured at parse time from the ``@task.stub`` TaskFlow call, stored in the serialized | ||
| Dag, and delivered to the lang-SDK runtime via ``TIRunContext.arg_bindings``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from functools import cache | ||
| from typing import Annotated, Literal | ||
|
|
||
| from pydantic import Field, JsonValue, TypeAdapter | ||
| from typing_extensions import TypeAliasType | ||
|
|
||
| from airflow.api_fastapi.core_api.base import BaseModel | ||
|
|
||
| # A named, titled alias (like TaskArgBinding below) kept as free-form JSON rather than a | ||
| # typed model, so unknown JSON-schema keywords survive re-serialization along the way. | ||
| ArgValueSchema = TypeAliasType( | ||
| "ArgValueSchema", Annotated[dict[str, JsonValue], Field(title="ArgValueSchema")] | ||
| ) | ||
| """JSON-schema fragment constraining the value a stub-task argument binds to; generated | ||
| by pydantic from the stub annotation, carried verbatim, unknown keywords ignored.""" | ||
|
|
||
|
|
||
| class _ArgBindingBase(BaseModel): | ||
| """Fields every :class:`TaskArgBinding` variant carries, regardless of ``kind``.""" | ||
|
|
||
| name: str | ||
| """The stub function's parameter name this binding fills, in declaration order.""" | ||
|
|
||
| value_schema: ArgValueSchema | None = None | ||
| """Schema fragment from the stub function's annotation; omitted when unconstrained.""" | ||
|
|
||
|
|
||
| class XComArgBinding(_ArgBindingBase): | ||
| """One positional stub-task argument pulled from an upstream task's XCom.""" | ||
|
|
||
| # No default: it would drop ``kind`` from ``required``, and the generated task-sdk | ||
| # client then types it ``Literal | None``, invalid as a tagged-union discriminator. | ||
| kind: Literal["xcom"] | ||
|
|
||
| task_id: str | ||
| """Upstream task id whose ``return_value`` XCom is pulled.""" | ||
|
|
||
|
|
||
| class LiteralArgBinding(_ArgBindingBase): | ||
| """One positional stub-task argument carrying an inline literal from the Dag file.""" | ||
|
|
||
| kind: Literal["literal"] | ||
| """No default, for the same generated-client reason as ``XComArgBinding.kind``.""" | ||
|
|
||
| value: JsonValue | None = None | ||
| """The literal value from the Dag file.""" | ||
|
|
||
| from_default: bool = False | ||
| """True when the value was filled from the stub signature's default rather than passed in the call.""" | ||
|
|
||
|
|
||
| # A named alias with an explicit title so the union lands in every schema as its own | ||
| # named definition, which the supervisor-schema dump dedups with its task-sdk twin by title. | ||
| TaskArgBinding = TypeAliasType( | ||
| "TaskArgBinding", | ||
| Annotated[XComArgBinding | LiteralArgBinding, Field(discriminator="kind", title="TaskArgBinding")], | ||
| ) | ||
| """One positional argument of a stub (foreign-runtime) task, in declaration order.""" | ||
|
|
||
|
|
||
| @cache | ||
| def get_arg_bindings_adapter() -> TypeAdapter[list[TaskArgBinding]]: | ||
| """ | ||
| Build (lazily, then cache) the adapter validating serialized dicts into ``TaskArgBinding``. | ||
|
|
||
| Only the stub-task path in the execution API needs it, so regular runs never pay for it. | ||
| """ | ||
| return TypeAdapter(list[TaskArgBinding]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
airflow-core/src/airflow/api_fastapi/execution_api/services/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. |
67 changes: 67 additions & 0 deletions
67
airflow-core/src/airflow/api_fastapi/execution_api/services/task_instances.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Business logic backing the task-instance execution routes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from airflow.models.dagbag import DBDagBag | ||
|
|
||
| # Task types (``TaskInstance.operator``, the operator class name) whose tasks carry a | ||
| # lang-SDK ``arg_bindings`` spec. Used to gate the serialized-Dag lookup so regular tasks | ||
| # never pay for it. The gate matches exact class names; a new lang-SDK operator adds its | ||
| # name here. | ||
| LANG_SDK_OPERATORS = frozenset({"_StubOperator"}) | ||
|
|
||
|
|
||
| def client_supports_arg_bindings() -> bool: | ||
| """ | ||
| Whether the request's negotiated API version can receive ``arg_bindings``. | ||
|
|
||
| Clients on older versions never see the field (the version migration strips it from | ||
| the response), so the derivation must not run for them. | ||
|
|
||
| Rather than comparing the negotiated version by date, we check the | ||
| ``VersionChangeWithSideEffects`` subclass's ``is_applied`` flag; see | ||
| https://docs.cadwyn.dev/concepts/version_changes/#version-changes-with-side-effects | ||
| """ | ||
|
jason810496 marked this conversation as resolved.
|
||
| # Imported locally: the versions package transitively imports the routes, which import | ||
| # this module, so a top-level import here would be circular. | ||
| from airflow.api_fastapi.execution_api.versions.v2026_10_30 import AddArgBindingsToTIRunContext | ||
|
|
||
| return AddArgBindingsToTIRunContext.is_applied | ||
|
|
||
|
|
||
| def get_arg_bindings(dag_bag: DBDagBag, ti: Any, *, session: Session) -> list | None: | ||
| """ | ||
| Extract the stub task's TaskFlow arg spec from its Dag version. | ||
|
|
||
| Mapped (``.expand()``) stubs never capture a parse-time spec, so they resolve to | ||
| ``None`` here and keep the legacy ignored-args behavior; per-map-index delivery | ||
| lands in a follow-up. | ||
| """ | ||
| if ti.dag_version_id is None: | ||
| return None | ||
| if (dag := dag_bag.get_dag(ti.dag_version_id, session=session)) is None: | ||
| return None | ||
| if (task := dag.task_dict.get(ti.task_id)) is None: | ||
| return None | ||
| return getattr(task, "_arg_bindings", None) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
airflow-core/src/airflow/api_fastapi/execution_api/versions/v2026_10_30.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from cadwyn import ( | ||
| ResponseInfo, | ||
| VersionChangeWithSideEffects, | ||
| convert_response_to_previous_version_for, | ||
| schema, | ||
| ) | ||
|
|
||
| from airflow.api_fastapi.execution_api.datamodels.taskinstance import TIRunContext | ||
|
|
||
|
|
||
| class AddArgBindingsToTIRunContext(VersionChangeWithSideEffects): | ||
| """Add the ``arg_bindings`` argument-binding spec for stub (foreign-runtime) tasks.""" | ||
|
|
||
| description = __doc__ | ||
|
|
||
| # A side-effect change, not just a schema one, so ti_run can gate the server-side spec | ||
| # derivation on ``is_applied``: clients older than this version never receive the field. | ||
| instructions_to_migrate_to_previous_version = (schema(TIRunContext).field("arg_bindings").didnt_exist,) | ||
|
|
||
| @convert_response_to_previous_version_for(TIRunContext) # type: ignore[arg-type] | ||
| def remove_arg_bindings_field(response: ResponseInfo) -> None: # type: ignore[misc] | ||
| """Strip ``arg_bindings`` from the run context for older clients.""" | ||
| response.body.pop("arg_bindings", None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmmm, I wonder if this should not allow none, and make it an empty list in that case. I don't think it functionally makes a difference but... 🤔