amoghrajesh opened a new pull request, #67418:
URL: https://github.com/apache/airflow/pull/67418

    <!-- SPDX-License-Identifier: Apache-2.0
         https://www.apache.org/licenses/LICENSE-2.0 -->
   
   <!--
   Thank you for contributing!
   
   Please provide above a brief description of the changes made in this pull 
request.
   Write a good git commit message following this guide: 
http://chris.beams.io/posts/git-commit/
   
   Please make sure that your code changes are covered with tests.
   And in case of new features or big changes remember to adjust the 
documentation.
   
   Feel free to ping (in general) for the review if you do not see reaction for 
a few days
   (72 Hours is the minimum reaction time you can expect from volunteers) - we 
sometimes miss notifications.
   
   In case of an existing issue, reference it using one of the following:
   
   * closes: #ISSUE
   * related: #ISSUE
   -->
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, 
please
   change below checkbox to `[X]` followed by the name of the tool, uncomment 
the "Generated-by".
   -->
   
   - [ ] Yes (please specify the tool below)
   
   <!--
   Generated-by: [Tool Name] following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   -->
   
   Discovered during: https://github.com/apache/airflow/pull/67376
   
   ### What
   
   Task and asset state is designed for storing lightweight metadata like job 
handles, counters, timestamps, small status dictionaries. But the 
`set()`/`get()` API forced everything through `str`, making common patterns 
awkward:
   
   ```python
   task_state.set("retry_count", str(3))
   task_state.set("poll_result", json.dumps({"status": "succeeded", "rows": 
1234}))
   
   count = int(task_state.get("retry_count"))
   result = json.loads(task_state.get("poll_result"))
   ```
   
   Proposed change
   `set()` and `get()` now accept and return any JSON native type. No manual 
serialization needed:
   
   ```python
   # After
   task_state.set("retry_count", 3)
   task_state.set("poll_result", {"status": "succeeded", "rows": 1234})
   task_state.set("job_id", "spark-abc") 
   
   count = task_state.get("retry_count")       # → 3 (int)
   result = task_state.get("poll_result")      # → dict
   ```
   
   Using plain `json.dumps`/`json.loads` at the execution API boundary rather 
than a heavier serialization library like serde. Task state is not intended 
Python objects and JSON covers every real use case with no additional 
dependencies.
   
   The DB column remains TEXT with JSON-encoded strings; the encoding/decoding 
is transparent to callers.
   
   ### Testing
   
   ```python
   from __future__ import annotations
   
   from datetime import datetime
   
   from airflow.sdk import DAG, task
   from airflow.sdk.execution_time.context import NEVER_EXPIRE
   
   with DAG(
       dag_id="test_task_state_types",
       schedule=None,
       start_date=datetime(2026, 1, 1),
       catchup=False,
       tags=["test", "aip-103"],
   ):
   
       @task(retries=0)
       def test_types(**context):
           ts = context["task_state"]
   
           # primitive types
           ts.set("a_str", "hello")
           ts.set("an_int", 42)
           ts.set("a_float", 3.14)
           ts.set("a_bool_true", True)
           ts.set("a_bool_false", False)
   
           assert ts.get("a_str") == "hello"
           assert ts.get("an_int") == 42
           assert ts.get("a_float") == 3.14
           assert ts.get("a_bool_true") is True
           assert ts.get("a_bool_false") is False
   
           # collection
           ts.set("a_list", [1, "two", 3.0, True])
           ts.set("a_dict", {"status": "ok", "count": 7})
           ts.set("nested", {"rows": [1, 2, 3], "meta": {"done": True}})
   
           assert ts.get("a_list") == [1, "two", 3.0, True]
           assert ts.get("a_dict") == {"status": "ok", "count": 7}
           assert ts.get("nested") == {"rows": [1, 2, 3], "meta": {"done": 
True}}
   
           # missing key returns None
           assert ts.get("never_set") is None
   
           print("All type round-trips passed")
   
       test_types()
   
   ```
   
   <img width="1728" height="964" alt="image" 
src="https://github.com/user-attachments/assets/b28e8414-7b74-4950-90c4-31604931b7ef";
 />
   
   
   <img width="1728" height="964" alt="image" 
src="https://github.com/user-attachments/assets/c31f04ed-d1b1-4427-b2b9-9b52884776c1";
 />
   
   
   
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to