From 0bf42bd462da39c4f653e28f3a9788fbe770ecb9 Mon Sep 17 00:00:00 2001 From: Kushal Patil Date: Wed, 24 Sep 2025 14:11:30 +0530 Subject: [PATCH 1/2] updated observe to handle my variables as well --- stagehand/handlers/observe_handler.py | 1 + stagehand/llm/inference.py | 2 ++ stagehand/llm/prompts.py | 8 +++++++- stagehand/schemas.py | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/stagehand/handlers/observe_handler.py b/stagehand/handlers/observe_handler.py index f0f29181..2c68b986 100644 --- a/stagehand/handlers/observe_handler.py +++ b/stagehand/handlers/observe_handler.py @@ -82,6 +82,7 @@ async def observe( logger=self.logger, log_inference_to_file=False, # TODO: Implement logging to file if needed from_act=from_act, + variables= options.variables ) # Extract metrics from response diff --git a/stagehand/llm/inference.py b/stagehand/llm/inference.py index 24f0de91..73594a0d 100644 --- a/stagehand/llm/inference.py +++ b/stagehand/llm/inference.py @@ -29,6 +29,7 @@ def observe( logger: Optional[Callable] = None, log_inference_to_file: bool = False, from_act: bool = False, + variables = {} ) -> dict[str, Any]: """ Call LLM to find elements in the DOM/accessibility tree based on an instruction. @@ -54,6 +55,7 @@ def observe( user_prompt = build_observe_user_message( instruction=instruction, tree_elements=tree_elements, + variables = variables ) messages = [ diff --git a/stagehand/llm/prompts.py b/stagehand/llm/prompts.py index 5080a857..65a4c785 100644 --- a/stagehand/llm/prompts.py +++ b/stagehand/llm/prompts.py @@ -177,12 +177,18 @@ def build_observe_system_prompt( def build_observe_user_message( instruction: str, tree_elements: str, + variables, ) -> ChatMessage: tree_or_dom = "Accessibility Tree" + print("###############", variables) return ChatMessage( role="user", content=f"""instruction: {instruction} -{tree_or_dom}: {tree_elements}""", +Below are the variables that are accessible in jinja style in the instruction. +For the 'fill' and 'type' instructions, don't replace the variables in the response. For the rest of the actions please do. In the response in the arguments try and use the same jinja style variables that are in the instruction, if it is suitable. +variables: {variables} +{tree_or_dom}: {tree_elements} +""", ) diff --git a/stagehand/schemas.py b/stagehand/schemas.py index 5ff23fb2..50065f2b 100644 --- a/stagehand/schemas.py +++ b/stagehand/schemas.py @@ -199,6 +199,8 @@ class ObserveOptions(StagehandBaseModel): dom_settle_timeout_ms: Optional[int] = None model_client_options: Optional[dict[str, Any]] = None iframes: Optional[bool] = None + variables: Optional[dict[str, str]] = None + class ObserveResult(StagehandBaseModel): From 0b6817c2fe9586c12fe29277d2947f55c6720330 Mon Sep 17 00:00:00 2001 From: Kushal Patil Date: Fri, 26 Sep 2025 11:21:56 +0530 Subject: [PATCH 2/2] small cleanup --- stagehand/llm/prompts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/stagehand/llm/prompts.py b/stagehand/llm/prompts.py index 65a4c785..b90be3bd 100644 --- a/stagehand/llm/prompts.py +++ b/stagehand/llm/prompts.py @@ -180,7 +180,6 @@ def build_observe_user_message( variables, ) -> ChatMessage: tree_or_dom = "Accessibility Tree" - print("###############", variables) return ChatMessage( role="user", content=f"""instruction: {instruction}