Agent Zero AI framework
fix (browser-use): Gemini JSON-pure outputs + action normalization
Alessandroa7d3fdc8e41b
diff --git a/models.py b/models.pyindex 9fe322eae..8ea0f64b9 100644--- a/models.py+++ b/models.py@@ -435,6 +435,52 @@ class BrowserCompatibleChatWrapper(ChatOpenRouter):def get_client(self, *args, **kwargs): # type: ignorereturn AsyncAIChatReplacement(self, *args, **kwargs)+ # -- Gemini helper -----------------------------------------+ def _gemini_clean_and_conform(self, text: str):+ obj = None+ try:+ # dirty_json parser is robust enough to handle markdown fences+ obj = dirty_json.parse(text)+ except Exception:+ return None # return None if parsing fails++ if not isinstance(obj, dict):+ return None++ # Conform actions to browser-use expectations+ if isinstance(obj.get("action"), list):+ normalized_actions = []+ for item in obj["action"]:+ if not isinstance(item, dict):+ continue # Skip non-dict items++ action_key, action_value = next(iter(item.items()), (None, None))+ if not action_key:+ continue++ # Create a mutable copy of the value+ v = (action_value or {}).copy()++ if action_key in ("scroll_down", "scroll_up", "scroll"):+ is_down = action_key != "scroll_up"+ v.setdefault("down", is_down)+ v.setdefault("num_pages", 1.0)+ normalized_actions.append({"scroll": v})+ elif action_key == "go_to_url":+ v.setdefault("new_tab", False)+ normalized_actions.append({action_key: v})+ elif action_key == "done":+ if "text" in v and "data" not in v:+ t = v.pop("text", "")+ v["data"] = {"title": "Task result", "response": t, "page_summary": t}+ v.setdefault("success", True)+ normalized_actions.append({action_key: v})+ else:+ normalized_actions.append(item)+ obj["action"] = normalized_actions++ return dirty_json.stringify(obj)+async def _acall(self,messages: List[BaseMessage],@@ -450,7 +496,7 @@ class BrowserCompatibleChatWrapper(ChatOpenRouter):model = kwargs.pop("model", None)kwrgs = {**self._wrapper.kwargs, **kwargs}- # hack from browser-use to fix json schema for gemini+ # hack from browser-use to fix json schema for gemini (additionalProperties, $defs, $ref)if "response_format" in kwrgs and "json_schema" in kwrgs["response_format"] and model.startswith("gemini/"):kwrgs["response_format"]["json_schema"] = ChatGoogle("")._fix_gemini_schema(self._wrapper.kwargs)@@ -460,6 +506,17 @@ class BrowserCompatibleChatWrapper(ChatOpenRouter):stop=stop,**kwrgs,)++ # Gemini: strip triple backticks and conform schema+ try:+ msg = resp.choices[0].message+ if self.provider == "gemini" and isinstance(getattr(msg, "content", None), str):+ cleaned = self._gemini_clean_and_conform(msg.content)+ if cleaned:+ msg.content = cleaned+ except Exception:+ pass+except Exception as e:raise e