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

    <!-- 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".
   -->
   
   - [x] Yes: claude sonnet 4.6
   
   <!--
   Generated-by: [Tool Name] following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   -->
   
   ### What problem are we solving?
   
   Task and asset state written by workers (via the Execution API) is stored as 
JSON-encoded strings in the DB — e.g., the integer `42` is stored as `"42"`, 
and the string `"hello"` as `'"hello"'`. The core API read path returned the 
raw DB string without decoding it, so callers saw `'"hello"'` instead of 
`"hello"` and `"42"` (a string) instead of `42` (an integer). 
   
   The write path had the inverse bug: values posted to the core API were 
stored without `json.dumps`, so a worker reading them back via the execution 
API would hit a `JSONDecodeError`.
   
   
   ### Current behaviour
   - GET `/dags/{dag_id}/dagRuns/{run_id}/taskInstances/{task_id}/states` and 
the equivalent asset state endpoints return raw JSON-encoded strings in the 
value field (e.g. "\"hello\"", "42" as a string).
   - PUT on these endpoints stores the value as-is without JSON-encoding, so 
values written via the Core API are unreadable by workers.
   - The value field on request/response models is typed as str, with no null 
or size validation.
   
   Classic example is if I run this task:
   
   ```python
       @task
       def my_task(**context: Context):
           task_state = context["task_state"]
   
           task_state.set("job_id", "12345")
           task_state.set("secret-dict", {"key": "value"})
           task_state.set("int_value", 42)
   
           print("Fetching task states I stored earlier")
   
           print("job_id:", task_state.get("job_id"))
           print("secret-dict:", task_state.get("secret-dict"))
           print("int_value:", task_state.get("int_value"))
   
       my_task()
   ```
   
   Core API call to get all task states returned this:
   
   ```shell
   {
       "task_states": [
           {
               "key": "int_value",
               "value": "42",
               "updated_at": "2026-05-26T10:07:01.872139Z",
               "expires_at": "2026-06-25T10:07:01.868443Z"
           },
           {
               "key": "job_id",
               "value": "\"12345\"",
               "updated_at": "2026-05-26T10:07:01.855922Z",
               "expires_at": "2026-06-25T10:07:01.851283Z"
           },
           {
               "key": "secret-dict",
               "value": "{\"key\": \"value\"}",
               "updated_at": "2026-05-26T10:07:01.864312Z",
               "expires_at": "2026-06-25T10:07:01.860810Z"
           }
       ],
       "total_entries": 3
   }
   ```
   
   ### Proposed change
   - `value: str` widened to `value: JsonValue` on all four core API datamodels 
(TaskStateBody, TaskStateResponse, AssetStateBody, AssetStateResponse).
   - Write paths now call `json.dumps(body.value)` before passing to the 
backend, matching the execution API as introduced in 
https://github.com/apache/airflow/pull/67418
   - Read paths now call `json.loads(r.value)` when constructing responses, so 
callers receive native types (int, dict, list, bool, str).
   - Body models now have validators rejecting `null`, `non-finite floats (NaN, 
Inf, -Inf)`, and values whose serialized form exceeds 65535 bytes (preserving 
the old max_length constraint).
   
   NOTE: The UI PR: https://github.com/apache/airflow/pull/67292 sends in raw 
values as entered by user, so no impact there.
   
   ### Testing
   
   
   #### Test 1: Set using task execution, Get using core API and task execution 
should have same response type
   
   <img width="1708" height="855" alt="image" 
src="https://github.com/user-attachments/assets/f9708969-bf6b-4a3f-8df4-dc797728479c";
 />
   
   
   <img width="1301" height="1213" alt="image" 
src="https://github.com/user-attachments/assets/9006df0a-8ea6-4c11-9fc9-f30730e702ed";
 />
   
   
   #### Test 2: Value set using core API, getting via task execution and core 
API should have same response type
   
   Set values using curl for this task:
   ```python
       @task
       def my_task(**context: Context):
           task_state = context["task_state"]
   
           print("Fetching task states that are set by API")
   
           print("str_value:", task_state.get("str_value"), 
type(task_state.get("str_value")))
           print("dict-value:", task_state.get("dict-value"), 
type(task_state.get("dict-value")))
           print("int_value:", task_state.get("int_value"), 
type(task_state.get("int_value")))
   
       my_task()
   ```
   
   <img width="1708" height="855" alt="image" 
src="https://github.com/user-attachments/assets/8b91ba8d-3b6f-4892-8040-deda6270f0c1";
 />
   
   
   <img width="1455" height="1098" alt="image" 
src="https://github.com/user-attachments/assets/5f9c42c1-8bcc-401b-bb54-aa28975c32e7";
 />
   
   
   
   ---
   
   * 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