Skip to content

Skip redundant JsonValue checks for XCom values - #77533

Open
PCTEJA wants to merge 2 commits into
apache:mainfrom
PCTEJA:fix/airflow-77502-large-json-event-loop-stall
Open

PCTEJA wants to merge 2 commits into
apache:mainfrom
PCTEJA:fix/airflow-77502-large-json-event-loop-stall

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Large XCom values stall the Execution API event loop because pydantic re-validates and re-serializes every element of a JsonValue on each pass through the request and response models. A value arriving from the API is already decoded JSON, and a value read back comes from a JSON store, so those checks cannot fail. This PR annotates the XCom request body and response models with DecodedJsonValue, a JsonValue that passes the value through unchanged while publishing the same JSON schema.

closes #77502

What changes

  • airflow-core/src/airflow/api_fastapi/common/types.py: adds DecodedJsonValue, defined exactly as in Speed up Execution API state store requests with large values #77113 so the two PRs converge on one definition.
  • airflow-core/src/airflow/api_fastapi/execution_api/datamodels/xcom.py: XComResponse.value, XComSequenceIndexResponse.root and XComSequenceSliceResponse.root use DecodedJsonValue.
  • airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py: the set_xcom request body uses DecodedJsonValue.

Scope relative to #77113

#77113 covers the state-store models (task_state_store, asset_state_store) and explicitly leaves the XCom models out ("Not in this PR: The XCom models ... I will open follow-ups"). This PR covers exactly that leftover scope: the XCom models and the set_xcom request body. It does not touch the state-store models, the asset models, or the public API XCom models.

Benchmarks

Measured with the real Execution API app and SQLite, a 688,890-byte value (100,000-element integer array), 10 requests per run, median of 3 alternating runs each. The small-value control is unchanged, so the machine was quiet.

Route Base (ms) This PR (ms) Change
POST XCom (set) 123.0 81.5 -41.5 ms (-34%)
GET XCom 142.2 58.2 -84.0 ms (-59%)
POST small value (control) 40.1 39.9 no change
GET small value (control) 19.5 19.5 no change

Model level, same 689 KB value: XComResponse validation 19.61 ms to 0.03 ms, JSON serialization 22.13 ms to 3.29 ms. The Cadwyn migration overhead (about 50 ms over plain FastAPI at any version) is unchanged by this PR; the remaining cost is JSON parsing, the database write, and response encoding.

Tests

  • New airflow-core/tests/unit/api_fastapi/execution_api/datamodels/test_xcom.py (9 tests): validation returns the decoded value unchanged, serialization skips the JsonValue checks, and the published schema matches plain JsonValue. 6 of the 9 fail on the base commit for the expected reason; all 9 pass with the fix.
  • Relevant set (new tests, test_types.py, head test_xcoms.py, v2025_04_28/test_xcom.py): 210 passed, run twice with identical results.
  • The served OpenAPI spec for the XCom paths and schemas is byte-identical between base and this PR, so no VersionChange and no Task SDK model regeneration are needed. The generate-tasksdk-datamodels prek hook passes with no diff.
  • Ruff format and check are clean; mypy-airflow-core passes.

Notes

  • No newsfragment: this is a performance improvement with no behavior change for JSON values.
  • Edge case, same as Speed up Execution API state store requests with large values #77113: a custom XCom backend returning a non-JSON object such as a datetime previously got a 500 on response validation. It is now serialized by pydantic inferring its type, as for an Any field.
  • Two tests in head test_xcoms.py (test_xcom_access_denied, get and set) error at setup in this VM because airflow_shared is not importable here; test_app.py fails to collect for the same reason, identically on the base commit. The served-spec comparison above covers what test_app.py checks for this change.
  • The pre-push Codex review could not run on this VM: it failed with 401 Unauthorized ("Missing bearer basic authentication"), no Codex auth here. The review was not silently skipped; it could not run.
  • No live MySQL or Postgres run here (no Docker on this VM); SQLite only. CI is authoritative for those.

Was generative AI tooling used to co-author this PR?
  • Yes

Generated-by: GPT Astra following the guidelines.

I used GPT Astra only for inline code completion.

@fat-catTW fat-catTW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR.
text/plain with body hello returns 500 after this change (previously 422). Could we reject raw bytes before they reach the database and add a test

Large XCom values stall the Execution API event loop because pydantic re-validates and re-serializes every element of a JsonValue on each pass through the request and response models. Values arriving from the API are already decoded JSON, and values read back come from a JSON store, so the checks cannot fail. Annotate the XCom request body and response models with DecodedJsonValue, which passes the value through unchanged while publishing the same JSON schema as JsonValue.
…th 422

A body that is not JSON reaches the endpoint as raw bytes, and those bytes
reached the database, where serializing them failed with a 500. Rejecting
them at validation restores the 422 the endpoint returned before this
change.
@PCTEJA
PCTEJA force-pushed the fix/airflow-77502-large-json-event-loop-stall branch from de22a21 to fba70ba Compare November 29, 2026 01:52
@PCTEJA

PCTEJA commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

@fat-catTW, text/plain 500 fixed — raw bytes rejected before the database, returning 422, with a test. Please re-review and approve the workflow run?

@PCTEJA
PCTEJA requested a review from fat-catTW November 29, 2026 02:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce Execution API event-loop stalls for large JSON request bodies

2 participants