Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-9512): harden hydrate_action against malformed rows (#5856)
Celal Zamanoğlu1906b1d2ad5e
diff --git a/skyvern/forge/sdk/db/utils.py b/skyvern/forge/sdk/db/utils.pyindex fda99f67b..be64c2927 100644--- a/skyvern/forge/sdk/db/utils.py+++ b/skyvern/forge/sdk/db/utils.py@@ -1,6 +1,7 @@import jsonimport typing+import pydanticimport pydantic.jsonimport structlog@@ -818,12 +819,55 @@ def hydrate_action(action_model: ActionModel, empty_element_id: bool = False) ->if value is not None:action_data[key] = value- # Get the appropriate action class and instantiate it+ # Get the appropriate action class and instantiate it. Fall back to base Action on+ # validation/lookup failure so a single malformed row never poisons list endpoints+ # like the workflow run timeline (SKY-9512).action_class = ACTION_TYPE_TO_CLASS.get(action_model.action_type)if action_class is None:- raise ValueError(f"Unsupported action type: {action_model.action_type}")+ LOG.warning(+ "Unknown action_type in DB, hydrating as base Action",+ action_id=action_model.action_id,+ action_type=action_model.action_type,+ )+ return _hydrate_as_base_action(action_data, action_model)++ try:+ return action_class(**action_data)+ except pydantic.ValidationError as exc:+ LOG.warning(+ "Failed to hydrate action as typed subclass, falling back to base Action",+ action_id=action_model.action_id,+ action_type=action_model.action_type,+ errors=exc.errors(),+ )+ return _hydrate_as_base_action(action_data, action_model)+- return action_class(**action_data)+def _hydrate_as_base_action(action_data: dict[str, typing.Any], action_model: ActionModel) -> Action:+ """Construct a base Action, dropping fields the base model rejects.++ The action_json may contain subclass-only fields (e.g. MoveAction.x) or values that+ fail validation against the base Action schema. Strip anything the base model can't+ accept so we always return *something* renderable for the UI.+ """+ try:+ return Action(**action_data)+ except pydantic.ValidationError:+ minimal: dict[str, typing.Any] = {+ "action_type": action_model.action_type,+ "status": action_model.status,+ "action_id": action_model.action_id,+ "organization_id": action_model.organization_id,+ "workflow_run_id": action_model.workflow_run_id,+ "task_id": action_model.task_id,+ "step_id": action_model.step_id,+ "created_at": action_model.created_at,+ "modified_at": action_model.modified_at,+ }+ try:+ return Action(**minimal)+ except pydantic.ValidationError:+ return Action.model_construct(**minimal)def convert_to_workflow_schedule(diff --git a/tests/unit/test_hydrate_action_resilience.py b/tests/unit/test_hydrate_action_resilience.pynew file mode 100644index 000000000..aae2e4f98--- /dev/null+++ b/tests/unit/test_hydrate_action_resilience.py@@ -0,0 +1,78 @@+"""Tests for hydrate_action resilience to malformed action rows.++Regression for SKY-9512: a single bad action row should not crash the timeline+endpoint via ValidationError propagation.+"""++from datetime import datetime+from types import SimpleNamespace+from typing import Any++import pytest++from skyvern.forge.sdk.db.utils import hydrate_action+from skyvern.webeye.actions.actions import Action, ActionStatus, ActionType, MoveAction+++def _action_row(**overrides: Any) -> SimpleNamespace:+ """Build a minimal duck-typed stand-in for ActionModel."""+ base: dict[str, Any] = {+ "action_id": "act_test",+ "action_type": ActionType.MOVE,+ "status": ActionStatus.completed,+ "source_action_id": None,+ "organization_id": "o_test",+ "workflow_run_id": "wr_test",+ "task_id": "tsk_test",+ "step_id": "stp_test",+ "step_order": 0,+ "action_order": 0,+ "confidence_float": None,+ "reasoning": None,+ "intention": None,+ "response": None,+ "element_id": None,+ "skyvern_element_hash": None,+ "skyvern_element_data": None,+ "screenshot_artifact_id": None,+ "created_at": datetime(2026, 5, 6, 0, 0, 0),+ "modified_at": datetime(2026, 5, 6, 0, 0, 0),+ "action_json": None,+ }+ base.update(overrides)+ return SimpleNamespace(**base)+++def test_hydrate_action_happy_path_returns_subclass() -> None:+ row = _action_row(action_json={"x": 10, "y": 20})++ result = hydrate_action(row)++ assert isinstance(result, MoveAction)+ assert result.x == 10+ assert result.y == 20+++def test_hydrate_action_falls_back_to_base_action_on_validation_error(+ caplog: pytest.LogCaptureFixture,+) -> None:+ # x must be int|None. A list will not coerce, so MoveAction(...) raises ValidationError.+ row = _action_row(action_json={"x": [1, 2, 3]})++ result = hydrate_action(row)++ assert isinstance(result, Action)+ assert not isinstance(result, MoveAction)+ assert result.action_id == "act_test"+ assert result.action_type == ActionType.MOVE+ assert result.status == ActionStatus.completed+ assert result.task_id == "tsk_test"+++def test_hydrate_action_unknown_action_type_falls_back_to_base_action() -> None:+ row = _action_row(action_type="not_a_real_action_type")++ result = hydrate_action(row)++ assert isinstance(result, Action)+ assert result.action_id == "act_test"