Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-8694): wrap jinja errors with template context in all format calls (#5678)
LawyZhengf5a828f213d7
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 383ba98ab..7a487d0fa 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -50,6 +50,7 @@ from skyvern.exceptions import (MissingBrowserStatePage,MissingStarterUrl,PDFParsingError,+ SkyvernException,TaskNotFound,UnexpectedTaskStatus,get_user_facing_exception_message,@@ -433,7 +434,10 @@ class Block(BaseModel, abc.ABC):BlockType.HTTP_REQUEST,]- template = (env or jinja_sandbox_env).from_string(potential_template)+ try:+ template = (env or jinja_sandbox_env).from_string(potential_template)+ except Exception as exc:+ raise FailedToFormatJinjaStyleParameter(potential_template, str(exc)) from excblock_reference_data: dict[str, Any] = workflow_run_context.get_block_metadata(self.label)template_data = workflow_run_context.values.copy()@@ -528,7 +532,12 @@ class Block(BaseModel, abc.ABC):variables=missing_variables,)- return template.render(template_data)+ try:+ return template.render(template_data)+ except SkyvernException:+ raise+ except Exception as exc:+ raise FailedToFormatJinjaStyleParameter(potential_template, str(exc)) from excdef _apply_workflow_system_prompt(self,diff --git a/tests/unit/workflow/test_task_block_template_rendering.py b/tests/unit/workflow/test_task_block_template_rendering.pyindex 6620de231..38e69a6fd 100644--- a/tests/unit/workflow/test_task_block_template_rendering.py+++ b/tests/unit/workflow/test_task_block_template_rendering.py@@ -1,7 +1,10 @@from datetime import datetime, timezonefrom unittest.mock import MagicMock+import pytest+from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext+from skyvern.forge.sdk.workflow.exceptions import FailedToFormatJinjaStyleParameterfrom skyvern.forge.sdk.workflow.models.block import TaskBlockfrom skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType@@ -63,3 +66,57 @@ def test_format_potential_template_parameters_with_no_error_code_mapping() -> Noblock.format_potential_template_parameters(ctx)assert block.error_code_mapping is None+++def test_malformed_jinja_in_title_raises_with_template_context() -> None:+ """Syntax error in title template should raise FailedToFormatJinjaStyleParameter with the template string."""+ block = TaskBlock(+ label="bad_title",+ output_parameter=_make_output_parameter(),+ title="{{ unclosed",+ )+ ctx = _make_workflow_run_context()++ with pytest.raises(FailedToFormatJinjaStyleParameter, match="unclosed"):+ block.format_potential_template_parameters(ctx)+++def test_malformed_jinja_in_navigation_goal_raises_with_template_context() -> None:+ """Syntax error in navigation_goal should raise FailedToFormatJinjaStyleParameter."""+ block = TaskBlock(+ label="bad_nav",+ output_parameter=_make_output_parameter(),+ title="ok title",+ navigation_goal="{{ {% bad }}",+ )+ ctx = _make_workflow_run_context()++ with pytest.raises(FailedToFormatJinjaStyleParameter, match="bad"):+ block.format_potential_template_parameters(ctx)+++def test_malformed_jinja_in_error_code_mapping_raises_with_template_context() -> None:+ """Syntax error in error_code_mapping value should raise FailedToFormatJinjaStyleParameter."""+ block = TaskBlock(+ label="bad_ecm",+ output_parameter=_make_output_parameter(),+ title="ok title",+ error_code_mapping={"ERR_1": "{{ unclosed"},+ )+ ctx = _make_workflow_run_context()++ with pytest.raises(FailedToFormatJinjaStyleParameter, match="unclosed"):+ block.format_potential_template_parameters(ctx)+++def test_render_error_raises_with_template_context() -> None:+ """A template that compiles but fails at render time should also raise with template context."""+ block = TaskBlock(+ label="render_err",+ output_parameter=_make_output_parameter(),+ title="{{ foo | no_such_filter }}",+ )+ ctx = _make_workflow_run_context()++ with pytest.raises(FailedToFormatJinjaStyleParameter, match="no_such_filter"):+ block.format_potential_template_parameters(ctx)