Skip to content

feat(serialization): implement truncation logic for rendered values#61878

Merged
potiuk merged 25 commits intoapache:mainfrom
imrichardwu:truncation
Mar 13, 2026
Merged

feat(serialization): implement truncation logic for rendered values#61878
potiuk merged 25 commits intoapache:mainfrom
imrichardwu:truncation

Conversation

@imrichardwu
Copy link
Contributor

@imrichardwu imrichardwu commented Feb 13, 2026

This pull request create truncate_rendered_value helper into the shared library so it can be used consistently across Airflow core and the task SDK without crossing package boundaries.

Truncation logic changes

  • Added a new shared package apache-airflow-shared-template-rendering containing truncate_rendered_value along with exported constants TRUNCATE_PREFIX, TRUNCATE_SUFFIX, and TRUNCATE_MIN_CONTENT_LENGTH.
  • Updated airflow-core and task-sdk to import from the shared module, removing the cross-boundary import of airflow.utils.helpers in task_runner.py.

Tests

  • Added unit tests in shared/template_rendering/tests/ to validate truncation behavior and exact output using the exported constants.
  • Updated test_helpers.py and test_renderedtifields.py to use the shared constants instead of duplicating literal strings, and to build expected values from truncate_rendered_value directly rather than the higher-level serialize_template_field.
  • Fixed a self-fulfilling assertion in the task SDK truncation+masking test to explicitly verify that env_vars is truncated with the correct prefix/suffix and secrets are redacted.

Closes: #59877


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)
  • Used ChatGPT to help create and edit my test cases that were mentioned by a reviewer in a closed pr.

  • Read the 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) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

…n template fields

Added a new function to truncate rendered values based on a specified maximum length, ensuring that truncation messages are prioritized. This functionality is integrated into the serialization of template fields, enhancing the handling of long strings in the system.
@imrichardwu imrichardwu marked this pull request as draft February 13, 2026 20:05
Modified the test for runtime task instances to dynamically retrieve the rendered fields from the mock supervisor communications, ensuring accurate assertions for the SetRenderedFields message type. This change enhances the robustness of the test by adapting to varying truncation formats based on configuration.
…ialization

Replaced direct truncation messages in test assertions with calls to the new serialize_template_field function. This change ensures consistency in how large strings and objects are handled in the rendered task instance fields tests, leveraging the updated serialization logic for better clarity and maintainability.
@imrichardwu imrichardwu marked this pull request as ready for review February 13, 2026 22:37
@imrichardwu imrichardwu marked this pull request as draft February 15, 2026 21:24
@imrichardwu imrichardwu marked this pull request as ready for review February 15, 2026 21:24
Copy link
Contributor

@SameerMesiah97 SameerMesiah97 left a comment

Choose a reason for hiding this comment

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

I think this needs some refactoring. The current implementation looks functional but it is not very clear. I think the algorithm should be as follows:

  1. If max_length <= 0, return ""

  2. Build the truncation message once and, if max_length is smaller than its length, return it immediately.

  3. Determine the quoting strategy and compute the formatting overhead (prefix + quotes + suffix) and calculate available space.

  4. If available space is less than MIN_CONTENT_LENGTH, return the truncation message only.

  5. Otherwise slice the content to fit and construct the final string and ensure the result does not exceed max_length, trimming it if necessary.

Currently, I think you are mixing 3, 4 and 5 when they should each be in disparate blocks of code. And this is resulting in needless duplication.

Also, I was just wondering why this has to be duplicated across 2 files? Perhaps, a maintainer/committer could weigh in on this but is it possible to have this in airflow.utils.helpers instead?

@imrichardwu
Copy link
Contributor Author

I think this needs some refactoring. The current implementation looks functional but it is not very clear. I think the algorithm should be as follows:

  1. If max_length <= 0, return ""
  2. Build the truncation message once and, if max_length is smaller than its length, return it immediately.
  3. Determine the quoting strategy and compute the formatting overhead (prefix + quotes + suffix) and calculate available space.
  4. If available space is less than MIN_CONTENT_LENGTH, return the truncation message only.
  5. Otherwise slice the content to fit and construct the final string and ensure the result does not exceed max_length, trimming it if necessary.

Currently, I think you are mixing 3, 4 and 5 when they should each be in disparate blocks of code. And this is resulting in needless duplication.

Also, I was just wondering why this has to be duplicated across 2 files? Perhaps, a maintainer/committer could weigh in on this but is it possible to have this in airflow.utils.helpers instead?

I implemented all your feedback and would love for you to review my pr again!

Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

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

Looks good overall but would love another pair of eyes.

A couple of suggestions.

@kaxil kaxil requested review from Copilot and removed request for SameerMesiah97 February 25, 2026 03:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Introduces a shared truncate_rendered_value helper to standardize truncation of rendered template fields across Airflow core and the task SDK, and updates serialization/task-runner code paths to use it.

Changes:

  • Added shared truncate_rendered_value utility (new shared package) and wired it into core + task SDK serialization.
  • Updated tests to cover truncation edge cases and adjusted existing assertions to accommodate the new behavior.
  • Registered the new shared package in workspace/build configuration for core and task SDK.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
task-sdk/tests/task_sdk/execution_time/test_task_runner.py Loosens rendered-fields assertion by deriving expected data from the mocked call
task-sdk/src/airflow/sdk/execution_time/task_runner.py Replaces inline truncation formatting with truncate_rendered_value
task-sdk/src/airflow/sdk/_shared/template_rendering Adds shared-module path link for template rendering utilities
task-sdk/pyproject.toml Adds build mapping for shared template rendering package
shared/template_rendering/tests/template_rendering/test_truncate_rendered_value.py Adds unit tests for truncation helper edge cases and exact output
shared/template_rendering/tests/template_rendering/init.py Initializes shared template_rendering test package
shared/template_rendering/src/airflow_shared/template_rendering/init.py Implements truncate_rendered_value
shared/template_rendering/pyproject.toml Defines new shared workspace package metadata/build config
pyproject.toml Registers new shared package in dev deps + workspace members
airflow-core/tests/unit/serialization/test_helpers.py Adds unit tests for truncation helper and very-small max length behavior
airflow-core/tests/unit/models/test_renderedtifields.py Updates expected truncation outputs to use serialize_template_field
airflow-core/src/airflow/utils/helpers.py Adds module logger
airflow-core/src/airflow/serialization/helpers.py Uses shared truncate_rendered_value instead of inline truncation
airflow-core/src/airflow/_shared/template_rendering Adds shared-module path link for template rendering utilities
airflow-core/pyproject.toml Adds build mapping for shared template rendering package

@imrichardwu imrichardwu requested a review from Copilot February 25, 2026 04:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 7 comments.

Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

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

Looks good to me but we need another pair of eyes. @ashb @uranusjr ?

@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Mar 11, 2026
@eladkal eladkal added this to the Airflow 3.2.0 milestone Mar 11, 2026
@eladkal eladkal added the type:bug-fix Changelog: Bug Fixes label Mar 11, 2026
@potiuk
Copy link
Member

potiuk commented Mar 12, 2026

@uranusjr — Could you please check whether your review feedback on this PR has been addressed? The author appears to have responded to your comments. If the concerns are resolved, please resolve the conversation threads. Thank you!

@potiuk potiuk removed the ready for maintainer review Set after triaging when all criteria pass. label Mar 12, 2026
@potiuk
Copy link
Member

potiuk commented Mar 13, 2026

@uranusjr — Could you please check whether your review feedback on this PR has been addressed? @imrichardwu appears to have responded to your comments. @imrichardwu, do you believe the reviewer's concerns have been resolved?

If the concerns are resolved, please resolve the conversation threads. Thank you!

@imrichardwu
Copy link
Contributor Author

@uranusjr — Could you please check whether your review feedback on this PR has been addressed? @imrichardwu appears to have responded to your comments. @imrichardwu, do you believe the reviewer's concerns have been resolved?

If the concerns are resolved, please resolve the conversation threads. Thank you!

Yes

@potiuk potiuk merged commit da77f37 into apache:main Mar 13, 2026
230 checks passed
@potiuk
Copy link
Member

potiuk commented Mar 13, 2026

Merging it regardless. @uranusjr -> please check it retroactively if you stil have concerns.

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

Labels

type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect rendered template truncation for small max_templated_fields_length config

8 participants