Dev-iL opened a new pull request, #77113:
URL: https://github.com/apache/airflow/pull/77113
related: #67799
A task state store or asset state store request with a large value spends
most of its CPU time in pydantic's checks on that value, not in the database.
This PR adds a `DecodedJsonValue` type for already-decoded JSON and uses it for
the `value` field of the four Execution API state store models:
* `TaskStateStoreResponse`, `TaskStateStorePutBody`
* `AssetStateStoreResponse`, `AssetStateStorePutBody`
For a 689 KB value, the median event loop stall drops by 70–85% (GET 126–153
ms → 34–35 ms, PUT 81–90 ms → 12–24 ms), and the median request duration by
39–68%. The published OpenAPI schema does not change, so the generated Task SDK
models and the HTTP contract stay the same.
<details><summary><b>Details</b></summary>
<p>
* Pydantic checks a `JsonValue` with a Python discriminator function
(`pydantic.types._get_type_name`) that it calls once for every element, and it
walks the value again to serialize it. A state store request does this several
times: FastAPI validates the body, cadwyn dumps and re-validates it for the
version migration, and a read validates and serializes the response. For a
100,000-element value, the loop-thread profile at the measured base below shows
400,004 calls for a GET and 300,003 for a PUT. On `main` these routes are
synchronous, but this framework work runs on the event loop, so it already
blocks every other request there.
* `DecodedJsonValue` wraps the `JsonValue` schema with a validator that
returns the value unchanged and a serializer that passes it through. The JSON
schema is generated from the wrapped `JsonValue`, so the field still references
the `JsonValue` component. The OpenAPI document of the Execution API app is
identical before and after this change, and the `generate-tasksdk-datamodels`
hook produces no diff.
* The field validators that reject `null` and NaN values still run.
* What remains on the loop for a large GET is rendering the response body:
pydantic's `to_python` in JSON mode, which no longer calls back into Python,
and the stdlib `json` encoder. Each takes about 14 ms for a 689 KB value under
the profiler.
Behaviour differences:
* A request body is always decoded JSON, so the walk could never reject one,
and PUT accepts and rejects the same bodies as before.
* A read returns what the state backend's `get_value` returns. With the
built-in backends, this is the decoded JSON from storage. A custom backend that
overrides `get_value` and returns a Python object that is not JSON, such as a
`datetime`, got a 500 before, because the response model rejected it. Now
pydantic serializes it by inferring its type, as it does for an `Any` field (a
`datetime` becomes an ISO 8601 string). I left this unguarded because
`get_value` is typed to return `JsonValue`, so such a backend already breaks
its contract.
Not in this PR:
* The XCom models (`execution_api/datamodels/xcom.py`) and the public API
state store models have the same `JsonValue` fields. I want to measure those
routes before changing them, and I will open follow-ups.
* Upstream pydantic merged pydantic/pydantic#13995 ("Improve performance of
`JsonValue` Python serialization") on 2026-10-14. It is not in a release yet
(the latest is 2.14.3), and it covers serialization only. Validation, which is
most of the PUT cost, still walks every element.
* #75381 converts the task state store routes to async and is independent of
this PR. The two do not touch the same files, and earlier runs of a
functionally identical revision of this type on top of it showed the same gain.
</p>
</details>
<details><summary><b>Benchmarks and Testing</b></summary>
<p>
### Event-loop stall measurements
The harness is the one from #75381. Each request runs over HTTP through
`TestClient` from a separate thread. A 1 ms ticker on the server loop records
its longest sleep per request. Every case asserts the status and the returned
or stored value. 10 warm requests per case, cold request dropped, 2 rounds per
version run interleaved in one container. Values are integer arrays.
* **base**: `main` at `5addde0337`, with synchronous routes. This PR's
parent (`ce559e40ef`) is 18 commits newer; none of them touch `api_fastapi`,
the state store code or `uv.lock`.
* **this PR**: base plus this PR's `airflow-core/src` diff, applied
unchanged.
Each stall cell shows `median / max` in ms, and each duration cell the
median request duration in ms, for round 1; round 2.
Python 3.14.10, SQLAlchemy 2.0.57, FastAPI 0.162.0, cadwyn 8.2.0, pydantic
2.14.2, Breeze CI image. These are small local samples of event-loop
availability, not throughput. Only the task state store routes were measured;
the asset state store routes use the same type.
**SQLite (aiosqlite)**
| Request | Value | base stall | this PR stall | base duration | this PR
duration |
|---|---:|---|---|---|---|
| GET | 0.4 KB | 2 / 7; 3 / 5 | 3 / 4; 3 / 4 | 22; 22 | 22; 21 |
| GET | 58.9 KB | 25 / 36; 24 / 25 | 8 / 10; 9 / 10 | 52; 53 | 28; 27 |
| GET | 688.9 KB | 133 / 137; 126 / 151 | 34 / 38; 34 / 41 | 228; 199 | 79;
80 |
| PUT | 0.4 KB | 4 / 7; 2 / 4 | 5 / 6; 3 / 4 | 26; 14 | 26; 24 |
| PUT | 58.9 KB | 15 / 17; 8 / 20 | 6 / 7; 3 / 4 | 51; 28 | 34; 19 |
| PUT | 688.9 KB | 81 / 84; 82 / 92 | 24 / 28; 24 / 28 | 190; 189 | 98; 94 |
**PostgreSQL (psycopg async)**
| Request | Value | base stall | this PR stall | base duration | this PR
duration |
|---|---:|---|---|---|---|
| GET | 0.4 KB | 4 / 6; 4 / 6 | 4 / 5; 4 / 4 | 26; 26 | 25; 26 |
| GET | 58.9 KB | 25 / 31; 29 / 36 | 7 / 8; 7 / 8 | 56; 61 | 32; 32 |
| GET | 688.9 KB | 135 / 137; 153 / 225 | 35 / 42; 34 / 35 | 240; 260 | 88;
89 |
| PUT | 0.4 KB | 3 / 4; 4 / 5 | 4 / 4; 3 / 4 | 30; 32 | 31; 30 |
| PUT | 58.9 KB | 15 / 16; 17 / 20 | 3 / 5; 4 / 5 | 62; 65 | 22; 43 |
| PUT | 688.9 KB | 81 / 85; 90 / 100 | 12 / 18; 24 / 35 | 251; 263 | 81; 161
|
Small values do not change beyond noise. The PostgreSQL PUT rounds of this
PR differ (81 ms and 161 ms for 689 KB); both are below base.
## Tests
`TestDecodedJsonValue` has three tests:
* `test_validation_returns_decoded_containers_unchanged` asserts that
validation returns the same list or dict object.
* `test_serialization_skips_the_json_value_checks` serializes a `datetime`
with pydantic's serializer warnings turned into errors. Plain `JsonValue`
produces the same output but warns that the value is none of its members.
* `test_model_field_publishes_the_json_value_schema` compares the JSON
schema of a model field, in validation and serialization mode, with a plain
`JsonValue` field. The comparison has to be on a model field: for a bare
`TypeAdapter`, `Any` and `JsonValue` publish the same schema.
Each test fails under at least one of the implementations I tried and
rejected:
| `DecodedJsonValue` defined as | Failing tests |
|---|---|
| plain `JsonValue` | validation (both cases), serialization |
| this type without the pass-through serializer | serialization |
| `Any` (drops the `$ref`) | schema (both modes) |
| `SkipValidation[JsonValue]` (adds `title: Value`) | serialization, schema
(both modes) |
The existing route tests cover the null and NaN rejection and the round trip
of stored values through both versions of the API.
Checks run on this revision, in Breeze with Python 3.14.10:
* `tests/unit/api_fastapi/common/test_types.py`,
`execution_api/versions/head/test_task_state_store.py`,
`execution_api/versions/head/test_asset_state_store.py`,
`execution_api/versions/v2026_10_30/test_task_state_store.py` and
`execution_api/test_app.py`: 153 passed on each backend:
* SQLite (`sqlite` / `sqlite+aiosqlite`)
* PostgreSQL (`postgresql+psycopg` / `postgresql+psycopg_async`)
* MySQL (`mysql` / `mysql+asyncmy`)
* `prek run mypy-airflow-core` on the changed files: no issues.
* `prek run --stage pre-commit` on the changed files: passed, including
`generate-tasksdk-datamodels` with no diff.
</p>
</details>
---
##### 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 (please specify the tool below)
Generated-by: Claude Code (Opus 5.5) following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
---
* 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]