Skip to content

Automate browser based workflows with AI

fix(SKY-9820): surface YAML parser detail on workflow save (#5981)

Aaron Perez99e8dd32b898
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py
index d228879ac..065808f59 100644
--- a/skyvern/forge/sdk/routes/agent_protocol.py
+++ b/skyvern/forge/sdk/routes/agent_protocol.py
@@ -142,7 +142,7 @@ from skyvern.schemas.workflows import (
)
from skyvern.services import block_service, run_service, task_v1_service, task_v2_service, workflow_service
from skyvern.services.pdf_import_service import pdf_import_service
-from skyvern.utils.yaml_loader import safe_load_no_dates
+from skyvern.utils.yaml_loader import format_yaml_error, safe_load_no_dates
from skyvern.webeye.actions.actions import Action
LOG = structlog.get_logger()
@@ -601,8 +601,8 @@ async def create_workflow_legacy(
raw_yaml = await request.body()
try:
workflow_yaml = safe_load_no_dates(raw_yaml)
- except yaml.YAMLError:
- raise HTTPException(status_code=422, detail="Invalid YAML")
+ except yaml.YAMLError as exc:
+ raise HTTPException(status_code=422, detail=format_yaml_error(exc))
# Auto-sanitize block labels and update references for imports
workflow_yaml = sanitize_workflow_yaml_with_references(workflow_yaml)
@@ -678,8 +678,8 @@ async def create_workflow(
organization=current_org,
request=workflow_definition,
)
- except yaml.YAMLError:
- raise HTTPException(status_code=422, detail="Invalid YAML")
+ except yaml.YAMLError as exc:
+ raise HTTPException(status_code=422, detail=format_yaml_error(exc))
except WorkflowDefinitionValidationException as e:
raise e
except (SkyvernHTTPException, ValidationError) as e:
@@ -1007,8 +1007,8 @@ async def update_workflow_legacy(
raw_yaml = await request.body()
try:
workflow_yaml = safe_load_no_dates(raw_yaml)
- except yaml.YAMLError:
- raise HTTPException(status_code=422, detail="Invalid YAML")
+ except yaml.YAMLError as exc:
+ raise HTTPException(status_code=422, detail=format_yaml_error(exc))
try:
workflow_create_request = WorkflowCreateYAMLRequest.model_validate(workflow_yaml)
@@ -1091,8 +1091,8 @@ async def update_workflow(
request=workflow_definition,
workflow_permanent_id=workflow_id,
)
- except yaml.YAMLError:
- raise HTTPException(status_code=422, detail="Invalid YAML")
+ except yaml.YAMLError as exc:
+ raise HTTPException(status_code=422, detail=format_yaml_error(exc))
except WorkflowDefinitionValidationException as e:
raise e
except (SkyvernHTTPException, ValidationError) as e:
diff --git a/skyvern/utils/yaml_loader.py b/skyvern/utils/yaml_loader.py
index f2a63e22f..f30fc5c1e 100644
--- a/skyvern/utils/yaml_loader.py
+++ b/skyvern/utils/yaml_loader.py
@@ -41,3 +41,24 @@ def safe_load_no_dates(stream: Any) -> Any:
return loader.get_single_data()
finally:
loader.dispose() # type: ignore[no-untyped-call]
+
+
+def format_yaml_error(exc: yaml.YAMLError) -> str:
+ """Build a user-actionable detail string from a PyYAML parse error.
+
+ ``MarkedYAMLError`` (parent of ScannerError/ParserError/ComposerError) carries
+ a ``problem`` description plus 1-indexed-friendly line/column marks. Plain
+ ``yaml.YAMLError`` instances fall back to ``str(exc)``.
+ """
+ if isinstance(exc, yaml.MarkedYAMLError):
+ problem = (exc.problem or "could not parse YAML").strip()
+ parts = [f"Invalid YAML: {problem}"]
+ if exc.problem_mark is not None:
+ parts.append(f"at line {exc.problem_mark.line + 1}, column {exc.problem_mark.column + 1}")
+ if exc.context and exc.context_mark is not None:
+ parts.append(
+ f"({exc.context.strip()} at line {exc.context_mark.line + 1}, column {exc.context_mark.column + 1})"
+ )
+ return " ".join(parts)
+ message = str(exc).strip()
+ return f"Invalid YAML: {message}" if message else "Invalid YAML"
diff --git a/tests/unit/test_yaml_loader_no_dates.py b/tests/unit/test_yaml_loader_no_dates.py
index c654b31c3..02732c981 100644
--- a/tests/unit/test_yaml_loader_no_dates.py
+++ b/tests/unit/test_yaml_loader_no_dates.py
@@ -14,10 +14,11 @@ implicit resolver so the values stay as plain strings.
import json
+import pytest
import yaml
from skyvern.forge.sdk.routes.workflow_copilot import _process_workflow_yaml
-from skyvern.utils.yaml_loader import safe_load_no_dates
+from skyvern.utils.yaml_loader import format_yaml_error, safe_load_no_dates
ISO_BLOB = """
parameters:
@@ -113,3 +114,30 @@ workflow_definition:
assert metadata["updated_at"] == "2023-10-28T14:30:00Z"
assert isinstance(metadata["created_at"], str)
assert isinstance(metadata["updated_at"], str)
+
+
+def test_format_yaml_error_pins_line_and_column_for_marked_errors() -> None:
+ # Misaligned indent under "- label: first" — PyYAML reports the
+ # block-collection failure at line 4 col 4 (1-indexed), with the
+ # parent collection context at line 3 col 3.
+ bad_yaml = "title: Test\nblocks:\n - label: first\n bad_indent: oops\n"
+
+ with pytest.raises(yaml.YAMLError) as excinfo:
+ safe_load_no_dates(bad_yaml)
+
+ detail = format_yaml_error(excinfo.value)
+ assert detail == (
+ "Invalid YAML: expected <block end>, but found '<block mapping start>' "
+ "at line 4, column 4 "
+ "(while parsing a block collection at line 3, column 3)"
+ )
+
+
+def test_format_yaml_error_falls_back_for_plain_yaml_errors() -> None:
+ detail = format_yaml_error(yaml.YAMLError("something generic"))
+ assert detail == "Invalid YAML: something generic"
+
+
+def test_format_yaml_error_handles_blank_message() -> None:
+ detail = format_yaml_error(yaml.YAMLError(""))
+ assert detail == "Invalid YAML"