Skip to content

Automate browser based workflows with AI

fix: deduplicate extracted_information in code v2 loop output (#5051)

pedrohsdbea803a2328f6
diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.py
index 0f9ebc62b..71b845732 100644
--- a/skyvern/services/script_service.py
+++ b/skyvern/services/script_service.py
@@ -753,14 +753,15 @@ def _append_to_loop_output(output: Any, label: str | None = None) -> None:
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
+ # 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")
- if "extracted_information" in output:
- current_value["extracted_information"] = output.get("extracted_information")
context.loop_output_values.append(
{
diff --git a/tests/unit/test_script_service_loop_download_filter.py b/tests/unit/test_script_service_loop_download_filter.py
index b31163ebb..61256f36d 100644
--- a/tests/unit/test_script_service_loop_download_filter.py
+++ b/tests/unit/test_script_service_loop_download_filter.py
@@ -122,7 +122,11 @@ def test_append_to_loop_output_merges_downloaded_files() -> None:
entry = mock_context.loop_output_values[0]
assert entry["current_value"]["name"] == "test"
assert entry["current_value"]["downloaded_files"] == ["/path/to/file.pdf"]
- assert entry["current_value"]["extracted_information"] == {"key": "val"}
+ # 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"