Ajay9704 commented on code in PR #59999:
URL: https://github.com/apache/airflow/pull/59999#discussion_r2660339581
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -138,6 +137,34 @@ class TaskRunnerMarker:
"""Marker for listener hooks, to properly detect from which component they
are called."""
+def truncate_rendered_value(rendered: Union[str, bytes, Sequence], max_length:
int) -> str:
+ """
+ Truncate rendered value with a reasonable minimum length to avoid edge
cases.
+
+ Args:
+ rendered: The rendered value to truncate
+ max_length: The maximum allowed length for the output
+
+ Returns:
+ Truncated string that respects the max_length constraint
+ """
+ # Set a reasonable minimum to avoid complex edge cases with very small
values
+ if max_length < 100:
+ max_length = 100
+
+ prefix = "Truncated. You can change this behaviour in
[core]max_templated_field_length. "
+ suffix = "... "
+
+ available_length = max_length - len(prefix) - len(suffix)
Review Comment:
done sir
##########
airflow-core/src/airflow/serialization/helpers.py:
##########
@@ -29,6 +30,33 @@
from airflow.timetables.base import Timetable as CoreTimetable
+def truncate_rendered_value(rendered: Union[str, bytes, Sequence], max_length:
int) -> str:
+ """
+ Truncate rendered value with a reasonable minimum length to avoid edge
cases.
+
+ Args:
+ rendered: The rendered value to truncate
+ max_length: The maximum allowed length for the output
+
+ Returns:
+ Truncated string that respects the max_length constraint
+ """
+ # Set a reasonable minimum to avoid complex edge cases with very small
values
+ if max_length < 100:
+ max_length = 100
+
+ prefix = "Truncated. You can change this behaviour in
[core]max_templated_field_length. "
+ suffix = "... "
+
+ available_length = max_length - len(prefix) - len(suffix)
+ if available_length <= 0:
+ return (prefix + suffix)[:max_length]
+
+ content_length = max(0, available_length)
+ content_part = rendered[:content_length]
+ return f"{prefix}{repr(content_part)}{suffix}"
Review Comment:
@jscheffl Thank you for this feedback. I've updated the implementation to
apply repr() to the content before truncation, rather than after. This ensures
that the quote characters added by repr() are properly accounted for in the
length calculation, preventing the output from exceeding the maximum length
constraint.The updated approach now:
Applies repr() to the full rendered content first
Calculates the available space accounting for the prefix, suffix, and the
quotes that will be part of the repr output
Truncates the repr-formatted string to fit within the available space
Combines prefix + truncated repr content + suffix
This properly handles the quote overhead from repr() and ensures the output
never exceeds the configured maximum length, addressing the core issue with
small max_templated_field_length values.
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -896,13 +892,13 @@ def sort_dict_recursively(obj: Any) -> Any:
serialized = str(template_field)
if len(serialized) > max_length:
rendered = redact(serialized, name)
- return (
- "Truncated. You can change this behaviour in
[core]max_templated_field_length. "
- f"{rendered[: max_length - 79]!r}... "
- )
+ return truncate_rendered_value(rendered, max_length)
return template_field
+
+
+
Review Comment:
yes sir , its done
##########
airflow-core/pyproject.toml:
##########
@@ -229,8 +229,8 @@ exclude = [
"../shared/module_loading/src/airflow_shared/module_loading" =
"src/airflow/_shared/module_loading"
"../shared/observability/src/airflow_shared/observability" =
"src/airflow/_shared/observability"
"../shared/secrets_backend/src/airflow_shared/secrets_backend" =
"src/airflow/_shared/secrets_backend"
-"../shared/secrets_masker/src/airflow_shared/secrets_masker" =
"src/airflow/_shared/secrets_masker"
-"../shared/timezones/src/airflow_shared/timezones" =
"src/airflow/_shared/timezones"
+ "../shared/secrets_masker/src/airflow_shared/secrets_masker" =
"src/airflow/_shared/secrets_masker"
+ "../shared/timezones/src/airflow_shared/timezones" =
"src/airflow/_shared/timezones"
Review Comment:
done sir , i have reverted the changes
--
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]