-
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
base: main
Are you sure you want to change the base?
Changes from all commits
4d70dcb
bb84488
cf60aca
b533ac0
6ebf3bf
aad0a01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| from airflow.api_fastapi.core_api.base import BaseModel, StrictBaseModel | ||
| from airflow.api_fastapi.execution_api.datamodels.asset import AssetProfile | ||
| from airflow.api_fastapi.execution_api.datamodels.connection import ConnectionResponse | ||
| from airflow.api_fastapi.execution_api.datamodels.task_arg_binding import TaskArgBinding | ||
| from airflow.api_fastapi.execution_api.datamodels.variable import VariableResponse | ||
| from airflow.utils.state import ( | ||
| DagRunState, | ||
|
|
@@ -435,6 +436,13 @@ class TIRunContext(BaseModel): | |
| always reflects when the task *first* started, not when it was rescheduled/resumed. | ||
| """ | ||
|
|
||
| arg_bindings: list[TaskArgBinding] | None = None | ||
| """ | ||
| Ordered positional-argument binding spec for stub (foreign-runtime) tasks. | ||
|
|
||
| ``None`` for regular tasks and for stub tasks that declare no parameters. | ||
| """ | ||
|
Comment on lines
+439
to
+444
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... 🤔
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You resolved this but didn't change anything or say why it's fine as it is. |
||
|
|
||
|
|
||
| class PrevSuccessfulDagRunResponse(BaseModel): | ||
| """Schema for response with previous successful DagRun information for Task Template Context.""" | ||
|
|
||
| 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. |
| 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"}) | ||
|
Comment on lines
+28
to
+32
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gating with operator type name is not a good idea. Add a flag on _StubOperator, serialize it, and read it here instead.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is what we do for EmptyOperator -- we have a "is_empty" field or similar. |
||
|
|
||
|
|
||
| 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) | ||
| 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) |
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.
Elsewhere we do this:
It might be worth seeing if we can do the same thing here to apply a default too?
Or this approach from ExecuteTask workload: