Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Enhance JSON extraction in utils.py to handle stray characters and ensure valid JSON parsing. Added logic to count braces and trim content to the last valid JSON position if necessary, improving robustness in JSON handling.

Magnus Müller90a4d858f113
diff --git a/browser_use/agent/message_manager/utils.py b/browser_use/agent/message_manager/utils.py
index 953964101..315b11260 100644
--- a/browser_use/agent/message_manager/utils.py
+++ b/browser_use/agent/message_manager/utils.py
@@ -52,6 +52,30 @@ def extract_json_from_model_output(content: str | BaseMessage) -> dict:
content = re.sub(r'\}(\s*<[^>]*>.*?$)', '}', content, flags=re.DOTALL)
content = re.sub(r'\}(\s*<\|[^|]*\|>.*?$)', '}', content, flags=re.DOTALL)
+ # Handle extra characters after the JSON, including stray braces
+ # Find the position of the last } that would close the main JSON object
+ content = content.strip()
+ if content.endswith('}'):
+ # Try to parse and see if we get valid JSON
+ try:
+ json.loads(content)
+ except json.JSONDecodeError:
+ # If parsing fails, try to find the correct end of the JSON
+ # by counting braces and removing anything after the balanced JSON
+ brace_count = 0
+ last_valid_pos = -1
+ for i, char in enumerate(content):
+ if char == '{':
+ brace_count += 1
+ elif char == '}':
+ brace_count -= 1
+ if brace_count == 0:
+ last_valid_pos = i + 1
+ break
+
+ if last_valid_pos > 0:
+ content = content[:last_valid_pos]
+
# Parse the cleaned content
result_dict = json.loads(content)