airflow.models.xcom

Attributes

log

Classes

XComModel

XCom model class. Contains table and some utilities.

Functions

__getattr__(name)

Module Contents

airflow.models.xcom.log[source]
class airflow.models.xcom.XComModel[source]

Bases: airflow.models.base.TaskInstanceDependencies

XCom model class. Contains table and some utilities.

__tablename__ = 'xcom'[source]
dag_run_id[source]
task_id[source]
map_index[source]
key[source]
dag_id[source]
run_id[source]
value[source]
timestamp[source]
__table_args__[source]
dag_run[source]
logical_date[source]
classmethod clear(*, dag_id, task_id, run_id, map_index=None, session=NEW_SESSION)[source]

Clear all XCom data from the database for the given task instance.

Note

This will not purge any data from a custom XCom backend.

Parameters:
  • dag_id (str) – ID of DAG to clear the XCom for.

  • task_id (str) – ID of task to clear the XCom for.

  • run_id (str) – ID of DAG run to clear the XCom for.

  • map_index (int | None) – If given, only clear XCom from this particular mapped task. The default None clears all XComs from the task.

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

classmethod set(key, value, *, dag_id, task_id, run_id, map_index=-1, session=NEW_SESSION)[source]

Store an XCom value.

Parameters:
  • key (str) – Key to store the XCom.

  • value (Any) – XCom value to store.

  • dag_id (str) – DAG ID.

  • task_id (str) – Task ID.

  • run_id (str) – DAG run ID for the task.

  • map_index (int) – Optional map index to assign XCom for a mapped task. The default is -1 (set for a non-mapped task).

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

classmethod get_many(*, run_id, key=None, task_ids=None, dag_ids=None, map_indexes=None, include_prior_dates=False, limit=None, session=NEW_SESSION)[source]

Composes a query to get one or more XCom entries.

This function returns an SQLAlchemy query of full XCom objects. If you just want one stored value, use get_one() instead.

Parameters:
  • run_id (str) – DAG run ID for the task.

  • key (str | None) – A key for the XComs. If provided, only XComs with matching keys will be returned. Pass None (default) to remove the filter.

  • task_ids (str | collections.abc.Iterable[str] | None) – Only XComs from task with matching IDs will be pulled. Pass None (default) to remove the filter.

  • dag_ids (str | collections.abc.Iterable[str] | None) – Only pulls XComs from specified DAGs. Pass None (default) to remove the filter.

  • map_indexes (int | collections.abc.Iterable[int] | None) – Only XComs from matching map indexes will be pulled. Pass None (default) to remove the filter.

  • include_prior_dates (bool) – If False (default), only XComs from the specified DAG run are returned. If True, all matching XComs are returned regardless of the run it belongs to.

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

  • limit (int | None) – Limiting returning XComs

static serialize_value(value, *, key=None, task_id=None, dag_id=None, run_id=None, map_index=None)[source]

Serialize XCom value to JSON str.

static deserialize_value(result)[source]

Deserialize XCom value from a database result.

If deserialization fails, the raw value is returned, which must still be a valid Python JSON-compatible type (e.g., dict, list, str, int, float, or bool).

XCom values are stored as JSON in the database, and SQLAlchemy automatically handles serialization (json.dumps) and deserialization (json.loads). However, we use a custom encoder for serialization (serialize_value) and deserialization to handle special cases, such as encoding tuples via the Airflow Serialization module. These must be decoded using XComDecoder to restore original types.

Some XCom values, such as those set via the Task Execution API, bypass serialize_value and are stored directly in JSON format. Since these values are already deserialized by SQLAlchemy, they are returned as-is.

Example: Handling a tuple:

original_value = (1, 2, 3)
serialized_value = XComModel.serialize_value(original_value)
print(serialized_value)
# '{"__classname__": "builtins.tuple", "__version__": 1, "__data__": [1, 2, 3]}'

This serialized value is stored in the database. When deserialized, the value is restored to the original tuple.

Parameters:

result – The XCom database row or object containing a value attribute.

Returns:

The deserialized Python object.

Return type:

Any

airflow.models.xcom.__getattr__(name)[source]

Was this entry helpful?