Skip to content

Automate browser based workflows with AI

fix(SKY-9704): restore legacy loop block output shape on cached path (#5938)

Celal Zamanoğlu1dca64ea8af0
diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.py
index 338081e28..fc89771c5 100644
--- a/skyvern/forge/sdk/core/skyvern_context.py
+++ b/skyvern/forge/sdk/core/skyvern_context.py
@@ -109,7 +109,7 @@ class SkyvernContext:
workflow_run_block_id: str | None = None
loop_metadata: dict[str, Any] | None = None
loop_internal_state: dict[str, Any] | None = None
- loop_output_values: list[dict[str, Any]] | None = None
+ loop_output_values: list[list[dict[str, Any]]] | None = None
script_run_parameters: dict[str, Any] = field(default_factory=dict)
script_mode: bool = False
is_static_script: bool = False
diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.py
index 92f32f9e3..bf9420b49 100644
--- a/skyvern/services/script_service.py
+++ b/skyvern/services/script_service.py
@@ -1,7 +1,6 @@
import ast
import asyncio
import base64
-import copy
import hashlib
import importlib.util
import json
@@ -605,15 +604,15 @@ async def _record_output_parameter_value(
organization_id: str,
output: dict[str, Any] | list | str | None,
label: str | None = None,
-) -> None:
+) -> OutputParameter | None:
if not label:
- return
+ return None
# TODO support this in the future
workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(workflow_run_id)
# get the workflow
workflow = await app.DATABASE.workflows.get_workflow(workflow_id=workflow_id, organization_id=organization_id)
if not workflow:
- return
+ return None
# get the output_paramter
output_parameter = workflow.get_output_parameter(label)
@@ -638,6 +637,7 @@ async def _record_output_parameter_value(
output_parameter_id=output_parameter.output_parameter_id,
value=output,
)
+ return output_parameter
async def _handle_script_termination(
@@ -816,7 +816,7 @@ async def _update_workflow_block(
ai_fallback_triggered=ai_fallback_triggered,
)
- await _record_output_parameter_value(
+ recorded_output_parameter = await _record_output_parameter_value(
context.workflow_run_id,
context.workflow_id,
context.organization_id,
@@ -831,7 +831,7 @@ async def _update_workflow_block(
and context.parent_workflow_run_block_id
and workflow_run_block_id != context.parent_workflow_run_block_id
):
- _append_to_loop_output(final_output, label)
+ _append_to_loop_output(final_output, label, output_parameter=recorded_output_parameter)
except Exception as e:
LOG.warning(
@@ -848,31 +848,53 @@ async def _run_cached_function(cached_fn: Callable) -> Any:
return await cached_fn(page=run_context.page, context=run_context)
-def _append_to_loop_output(output: Any, label: str | None = None) -> None:
- """If executing inside a for_loop, collect this block's output for loop aggregation"""
+def _append_to_loop_output(
+ output: Any,
+ label: str | None = None,
+ output_parameter: OutputParameter | None = None,
+) -> None:
+ """If executing inside a for_loop, collect this block's output for loop aggregation.
+
+ Emits the legacy ``List[List[{loop_value, output_parameter, output_value}]]`` shape so
+ cached-path webhook payloads match the agentic ForLoopBlock.execute contract that
+ downstream consumers parse against. When ``output_parameter`` is provided we preserve
+ the real persisted ID; otherwise we synthesize a key-only fallback.
+ """
context = skyvern_context.current()
if not context or context.loop_output_values is None or context.loop_metadata is None:
return
- # Read the current loop item's raw value from loop metadata
+ if not label:
+ # No label = no output_parameter.key to route on; emit a warning so the
+ # drop is visible (callers passing label="" defensively still get logged).
+ LOG.warning("Skipping loop output append: missing block label")
+ return
+
loop_value = context.loop_metadata.get("current_value")
- current_value: Any = loop_value
- # If the loop value is a dictionary, we'll create a safe copy so we can
- # enrich it with block output data without mutating the original object.
- # Only copy downloaded_files here — extracted_information is already present
- # in output_value and copying it into current_value causes duplication when
- # _collect_extracted_information recursively walks both fields.
- if isinstance(loop_value, dict):
- current_value = copy.deepcopy(loop_value)
- if isinstance(output, dict):
- if "downloaded_files" in output:
- current_value["downloaded_files"] = output.get("downloaded_files")
-
- context.loop_output_values.append(
+
+ if output_parameter is None:
+ # Fallback when the caller can't hand us the workflow's persisted
+ # OutputParameter. Synthesizes the same key the legacy path uses so
+ # consumers that read .key still match; consumers that read
+ # output_parameter_id get a fresh UUID rather than the persisted one.
+ output_parameter = OutputParameter(
+ output_parameter_id=str(uuid.uuid4()),
+ key=f"{label}_output",
+ workflow_id=context.workflow_id or "",
+ created_at=datetime.now(),
+ modified_at=datetime.now(),
+ parameter_type=ParameterType.OUTPUT,
+ )
+
+ # Defensive: the loop generator should have appended the current iteration's
+ # sub-list before yielding, but if an inner block calls this before that
+ # happened, fall back to creating one rather than raising IndexError.
+ if not context.loop_output_values:
+ context.loop_output_values.append([])
+ context.loop_output_values[-1].append(
{
"loop_value": loop_value,
- "current_value": current_value,
+ "output_parameter": output_parameter,
"output_value": output,
- "label": label,
}
)
@@ -1924,7 +1946,11 @@ async def run_task(
organization_id=block_validation_output.organization_id,
browser_session_id=block_validation_output.browser_session_id,
)
- _append_to_loop_output(block_output.output_parameter_value, label)
+ _append_to_loop_output(
+ block_output.output_parameter_value,
+ label,
+ output_parameter=block_validation_output.output_parameter,
+ )
return block_output.output_parameter_value
@@ -2464,7 +2490,11 @@ async def extract(
organization_id=block_validation_output.organization_id,
browser_session_id=block_validation_output.browser_session_id,
)
- _append_to_loop_output(block_result.output_parameter_value, label)
+ _append_to_loop_output(
+ block_result.output_parameter_value,
+ label,
+ output_parameter=block_validation_output.output_parameter,
+ )
return block_result.output_parameter_value
@@ -2568,7 +2598,11 @@ async def execute_validation(
organization_id=block_validation_output.organization_id,
browser_session_id=block_validation_output.browser_session_id,
)
- _append_to_loop_output(result.output_parameter_value, label)
+ _append_to_loop_output(
+ result.output_parameter_value,
+ label,
+ output_parameter=block_validation_output.output_parameter,
+ )
return result
@@ -2797,7 +2831,11 @@ async def run_code(
organization_id=block_validation_output.organization_id,
browser_session_id=block_validation_output.browser_session_id,
)
- _append_to_loop_output(block_result.output_parameter_value, label)
+ _append_to_loop_output(
+ block_result.output_parameter_value,
+ label,
+ output_parameter=block_validation_output.output_parameter,
+ )
return cast(dict[str, Any], block_result.output_parameter_value)
@@ -3040,7 +3078,11 @@ async def trigger_workflow(
organization_id=block_validation_output.organization_id,
browser_session_id=block_validation_output.browser_session_id,
)
- _append_to_loop_output(result.output_parameter_value, label)
+ _append_to_loop_output(
+ result.output_parameter_value,
+ label,
+ output_parameter=block_validation_output.output_parameter,
+ )
return result.output_parameter_value
@@ -3067,7 +3109,11 @@ async def prompt(
organization_id=block_validation_output.organization_id,
browser_session_id=block_validation_output.browser_session_id,
)
- _append_to_loop_output(result.output_parameter_value, label)
+ _append_to_loop_output(
+ result.output_parameter_value,
+ label,
+ output_parameter=block_validation_output.output_parameter,
+ )
return result.output_parameter_value
@@ -3182,6 +3228,11 @@ async def loop(
"downloaded_file_signatures_before_iteration": downloaded_file_signatures_before_iteration,
}
workflow_run_context.update_block_metadata(block_validation_output.label, loop_metadata)
+ # Open a fresh per-iteration sub-list so inner blocks aggregate into it,
+ # matching ForLoopBlock.execute's outputs_with_loop_values shape.
+ # Invariant: loop_output_values was just set to [] above (step 5 setup).
+ assert block_validation_output.context.loop_output_values is not None
+ block_validation_output.context.loop_output_values.append([])
# Build the SkyvernLoopItem for this loop
yield SkyvernLoopItem(index, value)
@@ -3327,6 +3378,11 @@ async def while_loop(
}
workflow_run_context.update_block_metadata(block_validation_output.label, loop_metadata)
+ # Open a fresh per-iteration sub-list so inner blocks aggregate into it,
+ # matching ForLoopBlock.execute's outputs_with_loop_values shape.
+ # Invariant: loop_output_values was set to [] before the while True loop.
+ assert block_validation_output.context.loop_output_values is not None
+ block_validation_output.context.loop_output_values.append([])
yield SkyvernLoopItem(loop_idx, None)
loop_idx += 1
diff --git a/tests/unit/test_script_service_loop_download_filter.py b/tests/unit/test_script_service_loop_download_filter.py
index 61256f36d..d8e4476b9 100644
--- a/tests/unit/test_script_service_loop_download_filter.py
+++ b/tests/unit/test_script_service_loop_download_filter.py
@@ -1,6 +1,8 @@
+from datetime import datetime
from unittest.mock import MagicMock, patch
from skyvern.forge.sdk.schemas.files import FileInfo
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType
from skyvern.services.script_service import (
_append_to_loop_output,
_filter_downloaded_files_for_current_iteration,
@@ -100,87 +102,136 @@ def test_filter_downloaded_files_handles_urls_without_query_strings() -> None:
# --- _append_to_loop_output tests ---
+#
+# Pin the legacy webhook contract: cached for-loop output is
+# List[List[{loop_value, output_parameter, output_value}]] — list-of-lists,
+# per-item keyed by output_parameter (key ends in _output), matching
+# ForLoopBlock.execute's outputs_with_loop_values shape.
-def test_append_to_loop_output_merges_downloaded_files() -> None:
- """When loop_value is a dict and output has downloaded_files, current_value should include them."""
- mock_context = MagicMock()
- mock_context.loop_output_values = []
- mock_context.loop_metadata = {
+def _mock_context(loop_output_values: list[list[dict]] | None, loop_value: object = "item") -> MagicMock:
+ ctx = MagicMock()
+ ctx.loop_output_values = loop_output_values
+ ctx.loop_metadata = {
"current_index": 0,
- "current_value": {"name": "test"},
- "current_item": {"name": "test"},
+ "current_value": loop_value,
+ "current_item": loop_value,
}
+ ctx.workflow_id = "wf_test"
+ return ctx
- with patch("skyvern.services.script_service.skyvern_context.current", return_value=mock_context):
- _append_to_loop_output(
- {"downloaded_files": ["/path/to/file.pdf"], "extracted_information": {"key": "val"}},
- label="my_block",
- )
-
- assert len(mock_context.loop_output_values) == 1
- entry = mock_context.loop_output_values[0]
- assert entry["current_value"]["name"] == "test"
- assert entry["current_value"]["downloaded_files"] == ["/path/to/file.pdf"]
- # extracted_information should NOT be copied into current_value — it already
- # lives in output_value and duplicating it causes _collect_extracted_information
- # to count it twice.
- assert "extracted_information" not in entry["current_value"]
- assert entry["output_value"]["extracted_information"] == {"key": "val"}
- assert entry["loop_value"] == {"name": "test"}
- assert entry["label"] == "my_block"
+def test_append_to_loop_output_emits_legacy_per_item_shape() -> None:
+ ctx = _mock_context([[]], loop_value={"name": "test"})
+ with patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx):
+ _append_to_loop_output({"extracted_information": {"key": "val"}}, label="my_block")
-def test_append_to_loop_output_non_dict_loop_value() -> None:
- """When loop_value is not a dict, current_value should equal loop_value."""
- mock_context = MagicMock()
- mock_context.loop_output_values = []
- mock_context.loop_metadata = {
- "current_index": 0,
- "current_value": "just_a_string",
- "current_item": "just_a_string",
- }
+ assert len(ctx.loop_output_values) == 1
+ iteration = ctx.loop_output_values[0]
+ assert len(iteration) == 1
+ entry = iteration[0]
+ # Per-item legacy shape: loop_value + output_parameter + output_value, nothing else.
+ assert set(entry.keys()) == {"loop_value", "output_parameter", "output_value"}
+ assert entry["loop_value"] == {"name": "test"}
+ assert entry["output_value"] == {"extracted_information": {"key": "val"}}
+ # output_parameter.key must carry the legacy _output suffix.
+ assert entry["output_parameter"].key == "my_block_output"
+ assert entry["output_parameter"].workflow_id == "wf_test"
+
+
+def test_append_to_loop_output_groups_items_per_iteration() -> None:
+ """Items from the same iteration land in the same sub-list; new sub-list = new iteration."""
+ ctx = _mock_context([[]])
+ with patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx):
+ _append_to_loop_output({"k": 1}, label="navigate")
+ _append_to_loop_output({"k": 2}, label="extract")
+
+ # Simulate the loop generator opening a new iteration.
+ ctx.loop_output_values.append([])
+ _append_to_loop_output({"k": 3}, label="navigate")
+
+ assert len(ctx.loop_output_values) == 2
+ assert len(ctx.loop_output_values[0]) == 2
+ assert len(ctx.loop_output_values[1]) == 1
+ assert [e["output_parameter"].key for e in ctx.loop_output_values[0]] == [
+ "navigate_output",
+ "extract_output",
+ ]
+ assert ctx.loop_output_values[1][0]["output_parameter"].key == "navigate_output"
- with patch("skyvern.services.script_service.skyvern_context.current", return_value=mock_context):
- _append_to_loop_output({"some": "output"}, label="block_1")
- assert len(mock_context.loop_output_values) == 1
- entry = mock_context.loop_output_values[0]
- assert entry["current_value"] == "just_a_string"
- assert entry["loop_value"] == "just_a_string"
+def test_append_to_loop_output_initializes_sublist_when_empty() -> None:
+ """Defensive fallback: if the generator never appended a sub-list, one gets created."""
+ ctx = _mock_context([])
+ with patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx):
+ _append_to_loop_output({"k": "v"}, label="block_1")
+ assert len(ctx.loop_output_values) == 1
+ assert len(ctx.loop_output_values[0]) == 1
-def test_append_to_loop_output_does_not_mutate_loop_value() -> None:
- """Modifying current_value after append must not affect the original loop_value."""
- original_value = {"name": "test", "nested": {"key": "original"}}
- mock_context = MagicMock()
- mock_context.loop_output_values = []
- mock_context.loop_metadata = {
- "current_index": 0,
- "current_value": original_value,
- "current_item": original_value,
- }
-
- with patch("skyvern.services.script_service.skyvern_context.current", return_value=mock_context):
- _append_to_loop_output({"downloaded_files": ["file.pdf"]}, label="block_1")
- entry = mock_context.loop_output_values[0]
- # Mutate the copied current_value
- entry["current_value"]["nested"]["key"] = "mutated"
- entry["current_value"]["new_key"] = "added"
+def test_append_to_loop_output_non_dict_loop_value() -> None:
+ ctx = _mock_context([[]], loop_value="just_a_string")
+ with patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx):
+ _append_to_loop_output({"some": "output"}, label="block_1")
- # Original should be unaffected
- assert original_value["nested"]["key"] == "original"
- assert "new_key" not in original_value
- assert "downloaded_files" not in original_value
+ entry = ctx.loop_output_values[0][0]
+ assert entry["loop_value"] == "just_a_string"
def test_append_to_loop_output_noop_when_no_context() -> None:
- """Should return without error when context is None."""
with patch("skyvern.services.script_service.skyvern_context.current", return_value=None):
_append_to_loop_output({"output": "data"}) # Should not raise
+def test_append_to_loop_output_noop_when_label_missing() -> None:
+ """No label means no output_parameter.key to route on; entry is dropped and the drop is logged (not silent)."""
+ ctx = _mock_context([[]])
+ with (
+ patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx),
+ patch("skyvern.services.script_service.LOG") as mock_log,
+ ):
+ _append_to_loop_output({"output": "data"}, label=None)
+ _append_to_loop_output({"output": "data2"}) # default label=None
+ _append_to_loop_output({"output": "data3"}, label="")
+
+ assert ctx.loop_output_values == [[]]
+ # All three drops should have logged — empty-string callers don't get silently dropped.
+ assert mock_log.warning.call_count == 3
+
+
+def test_append_to_loop_output_preserves_passed_output_parameter() -> None:
+ """When the caller hands us the real OutputParameter, we preserve its ID (don't synthesize a fresh UUID)."""
+ real_op = OutputParameter(
+ output_parameter_id="op_real_123",
+ key="my_block_output",
+ workflow_id="wf_test",
+ created_at=datetime(2026, 1, 1),
+ modified_at=datetime(2026, 1, 1),
+ parameter_type=ParameterType.OUTPUT,
+ )
+ ctx = _mock_context([[]])
+ with patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx):
+ _append_to_loop_output({"k": "v"}, label="my_block", output_parameter=real_op)
+
+ entry = ctx.loop_output_values[0][0]
+ assert entry["output_parameter"] is real_op
+ assert entry["output_parameter"].output_parameter_id == "op_real_123"
+
+
+def test_append_to_loop_output_synthesizes_when_output_parameter_missing() -> None:
+ """Fallback path: no OutputParameter passed -> synthesize one from the label."""
+ ctx = _mock_context([[]])
+ with patch("skyvern.services.script_service.skyvern_context.current", return_value=ctx):
+ _append_to_loop_output({"k": "v"}, label="my_block")
+
+ entry = ctx.loop_output_values[0][0]
+ assert entry["output_parameter"].key == "my_block_output"
+ # Synthesized fallback gets a fresh UUID, never the empty string.
+ assert entry["output_parameter"].output_parameter_id
+ assert entry["output_parameter"].output_parameter_id != "op_real_123"
+
+
# --- _to_downloaded_file_signature tests ---