Fix variable truncator handling for UpdatedVariable (#27197)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
-LAN- 2025-10-21 16:23:17 +08:00 committed by GitHub
parent 4a6398fc1f
commit caf1a5fbab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 2 deletions

View File

@ -17,6 +17,7 @@ from core.variables.segments import (
StringSegment,
)
from core.variables.utils import dumps_with_segments
from core.workflow.nodes.variable_assigner.common.helpers import UpdatedVariable
_MAX_DEPTH = 100
@ -202,6 +203,9 @@ class VariableTruncator:
"""Recursively calculate JSON size without serialization."""
if isinstance(value, Segment):
return VariableTruncator.calculate_json_size(value.value)
if isinstance(value, UpdatedVariable):
# TODO(Workflow): migrate UpdatedVariable serialization upstream and drop this fallback.
return VariableTruncator.calculate_json_size(value.model_dump(), depth=depth + 1)
if depth > _MAX_DEPTH:
raise MaxDepthExceededError()
if isinstance(value, str):
@ -387,10 +391,13 @@ class VariableTruncator:
def _truncate_json_primitives(self, val: None, target_size: int) -> _PartResult[None]: ...
def _truncate_json_primitives(
self, val: str | list | dict | bool | int | float | None, target_size: int
self, val: UpdatedVariable | str | list | dict | bool | int | float | None, target_size: int
) -> _PartResult[Any]:
"""Truncate a value within an object to fit within budget."""
if isinstance(val, str):
if isinstance(val, UpdatedVariable):
# TODO(Workflow): push UpdatedVariable normalization closer to its producer.
return self._truncate_object(val.model_dump(), target_size)
elif isinstance(val, str):
return self._truncate_string(val, target_size)
elif isinstance(val, list):
return self._truncate_array(val, target_size)