From 9ce4efd1ec2fdec05cba940bcd8c370d0e49af87 Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 23 Feb 2026 16:11:33 -0800 Subject: [PATCH] Accept both %-wrapped and non-wrapped variable arguments in act --- pyproject.toml | 2 +- stagehand/handlers/act_handler.py | 15 ++++++++++----- stagehand/llm/prompts.py | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fef8501..406b155 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "stagehand" -version = "0.5.10" +version = "0.5.11" description = "Python SDK for Stagehand" readme = "README.md" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent",] diff --git a/stagehand/handlers/act_handler.py b/stagehand/handlers/act_handler.py index 8d530ea..df4981f 100644 --- a/stagehand/handlers/act_handler.py +++ b/stagehand/handlers/act_handler.py @@ -95,11 +95,16 @@ async def act(self, options: Union[ActOptions, ObserveResult]) -> ActResult: # Substitute variables in arguments if options.get("variables"): variables = options.get("variables", {}) - element_to_act_on.arguments = [ - str(arg).replace(f"{key}", str(value)) - for arg in element_to_act_on.arguments or [] - for key, value in variables.items() - ] + replaced_args = [] + for arg in element_to_act_on.arguments or []: + replaced = str(arg) + for key, value in variables.items(): + if f"%{key}%" in replaced: + replaced = replaced.replace(f"%{key}%", str(value)) + else: + replaced = replaced.replace(key, str(value)) + replaced_args.append(replaced) + element_to_act_on.arguments = replaced_args # domSettleTimeoutMs might come from options if specified for act dom_settle_timeout_ms = options.get("dom_settle_timeout_ms") diff --git a/stagehand/llm/prompts.py b/stagehand/llm/prompts.py index 5080a85..db2d713 100644 --- a/stagehand/llm/prompts.py +++ b/stagehand/llm/prompts.py @@ -205,7 +205,7 @@ def build_act_observe_prompt( If the action implies choosing an option from a dropdown, and the corresponding element is NOT a 'select' element, choose the click method.""" if variables and len(variables) > 0: - variables_prompt = f"The following variables are available to use in the action: {', '.join(variables.keys())}. Fill the argument variables with the variable name." + variables_prompt = f"The following variables are available to use in the action: {', '.join(variables.keys())}. Fill the argument variables with the variable name. Wrap the argument in % symbols." instruction += f" {variables_prompt}" return instruction