Skip to content

Conversation

@jason810496
Copy link
Member

@jason810496 jason810496 commented Mar 24, 2025

closes: #47920

Why

Setting a variable in Dag is failing due to 'Direct database access via the ORM is not allowed in Airflow 3.0' ( details in issue context )

from datetime import datetime

from airflow.models import Variable
from airflow import DAG

my_var = Variable.set("param_variable", 10)

What

Port Variable.set from TaskSDK to airflow.models.Variable.set

@jason810496 jason810496 force-pushed the feature/task-sdk/port-variable-set-method branch 3 times, most recently from 49268f9 to 51a3a7e Compare March 25, 2025 07:44
@jason810496 jason810496 marked this pull request as ready for review March 25, 2025 07:49
@jason810496 jason810496 force-pushed the feature/task-sdk/port-variable-set-method branch from 51a3a7e to a076fa4 Compare March 25, 2025 07:49
Comment on lines +196 to +229
def _set_variable(key: str, value: Any, description: str | None = None, serialize_json: bool = False) -> None:
from airflow.sdk.execution_time.comms import ErrorResponse, PutVariable
from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS

if serialize_json:
import json

value = json.dumps(value, indent=2)
else:
value = str(value)

SUPERVISOR_COMMS.send_request(log=log, msg=PutVariable(key=key, value=value, description=description))
msg = SUPERVISOR_COMMS.get_message()
if isinstance(msg, ErrorResponse):
raise AirflowRuntimeError(msg)
return


Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats right we need to do that as well

@Lee-W Lee-W self-requested a review March 25, 2025 11:34
@jason810496 jason810496 force-pushed the feature/task-sdk/port-variable-set-method branch from a076fa4 to 9f83b4c Compare March 30, 2025 13:49
@jason810496
Copy link
Member Author

Just rebased to latest main to resolve conflict.

Copy link
Contributor

@amoghrajesh amoghrajesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jason810496 this only handles the top level variables. We will also have to port over / allow this from the task runner level too.

Could you please handle that as well?

Comment on lines +196 to +229
def _set_variable(key: str, value: Any, description: str | None = None, serialize_json: bool = False) -> None:
from airflow.sdk.execution_time.comms import ErrorResponse, PutVariable
from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS

if serialize_json:
import json

value = json.dumps(value, indent=2)
else:
value = str(value)

SUPERVISOR_COMMS.send_request(log=log, msg=PutVariable(key=key, value=value, description=description))
msg = SUPERVISOR_COMMS.get_message()
if isinstance(msg, ErrorResponse):
raise AirflowRuntimeError(msg)
return


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats right we need to do that as well

@jason810496 jason810496 force-pushed the feature/task-sdk/port-variable-set-method branch from 9f83b4c to 3ba45df Compare April 9, 2025 07:53
@amoghrajesh
Copy link
Contributor

Discussed with @jason810496 on this one.

There is more work left to do on this and it would be nice to do this in RC tomorrow eod. I will be working on this parallely and creating a companion PR that handles the missing gaps in this one too.

@jason810496
Copy link
Member Author

Closing this one, as #49005 will address the issue.

@jason810496 jason810496 closed this Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:DAG-processing area:task-execution-interface-aip72 AIP-72: Task Execution Interface (TEI) aka Task SDK area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting a variable in Dag is failing due to 'Direct database access via the ORM is not allowed in Airflow 3.0'

2 participants