Skip to content

Agent Zero AI framework

fix: add missing error handling (#405)

ehl0wr0ld640e9afdda69
diff --git a/python/extensions/monologue_end/_50_memorize_fragments.py b/python/extensions/monologue_end/_50_memorize_fragments.py
index 3587ca2d8..7a3fed06e 100644
--- a/python/extensions/monologue_end/_50_memorize_fragments.py
+++ b/python/extensions/monologue_end/_50_memorize_fragments.py
@@ -45,7 +45,36 @@ class MemorizeMemories(Extension):
background=True,
)
- memories = DirtyJson.parse_string(memories_json)
+ # Add validation and error handling for memories_json
+ if not memories_json or not isinstance(memories_json, str):
+ log_item.update(heading="No response from utility model.")
+ return
+
+ # Strip any whitespace that might cause issues
+ memories_json = memories_json.strip()
+
+ if not memories_json:
+ log_item.update(heading="Empty response from utility model.")
+ return
+
+ try:
+ memories = DirtyJson.parse_string(memories_json)
+ except Exception as e:
+ log_item.update(heading=f"Failed to parse memories response: {str(e)}")
+ return
+
+ # Validate that memories is a list or convertible to one
+ if memories is None:
+ log_item.update(heading="No valid memories found in response.")
+ return
+
+ # If memories is not a list, try to make it one
+ if not isinstance(memories, list):
+ if isinstance(memories, (str, dict)):
+ memories = [memories]
+ else:
+ log_item.update(heading="Invalid memories format received.")
+ return
if not isinstance(memories, list) or len(memories) == 0:
log_item.update(heading="No useful information to memorize.")
diff --git a/python/extensions/monologue_end/_51_memorize_solutions.py b/python/extensions/monologue_end/_51_memorize_solutions.py
index f6dd19dec..47a383a4c 100644
--- a/python/extensions/monologue_end/_51_memorize_solutions.py
+++ b/python/extensions/monologue_end/_51_memorize_solutions.py
@@ -24,8 +24,8 @@ class MemorizeSolutions(Extension):
heading="Memorizing succesful solutions...",
)
- #memorize in background
- asyncio.create_task(self.memorize(loop_data, log_item))
+ # memorize in background
+ asyncio.create_task(self.memorize(loop_data, log_item))
async def memorize(self, loop_data: LoopData, log_item: LogItem, **kwargs):
# get system message and chat history for util llm
@@ -44,7 +44,36 @@ class MemorizeSolutions(Extension):
background=True,
)
- solutions = DirtyJson.parse_string(solutions_json)
+ # Add validation and error handling for solutions_json
+ if not solutions_json or not isinstance(solutions_json, str):
+ log_item.update(heading="No response from utility model.")
+ return
+
+ # Strip any whitespace that might cause issues
+ solutions_json = solutions_json.strip()
+
+ if not solutions_json:
+ log_item.update(heading="Empty response from utility model.")
+ return
+
+ try:
+ solutions = DirtyJson.parse_string(solutions_json)
+ except Exception as e:
+ log_item.update(heading=f"Failed to parse solutions response: {str(e)}")
+ return
+
+ # Validate that solutions is a list or convertible to one
+ if solutions is None:
+ log_item.update(heading="No valid solutions found in response.")
+ return
+
+ # If solutions is not a list, try to make it one
+ if not isinstance(solutions, list):
+ if isinstance(solutions, (str, dict)):
+ solutions = [solutions]
+ else:
+ log_item.update(heading="Invalid solutions format received.")
+ return
if not isinstance(solutions, list) or len(solutions) == 0:
log_item.update(heading="No successful solutions to memorize.")
@@ -61,7 +90,13 @@ class MemorizeSolutions(Extension):
rem = []
for solution in solutions:
# solution to plain text:
- txt = f"# Problem\n {solution['problem']}\n# Solution\n {solution['solution']}"
+ if isinstance(solution, dict):
+ problem = solution.get('problem', 'Unknown problem')
+ solution_text = solution.get('solution', 'Unknown solution')
+ txt = f"# Problem\n {problem}\n# Solution\n {solution_text}"
+ else:
+ # If solution is not a dict, convert it to string
+ txt = f"# Solution\n {str(solution)}"
solutions_txt += txt + "\n\n"
# remove previous solutions too similiar to this one
diff --git a/python/helpers/dirty_json.py b/python/helpers/dirty_json.py
index e52082e43..8d67b39ee 100644
--- a/python/helpers/dirty_json.py
+++ b/python/helpers/dirty_json.py
@@ -34,9 +34,19 @@ class DirtyJson:
def parse(self, json_string):
self._reset()
self.json_string = json_string
- self.index = self.get_start_pos(
- self.json_string
- ) # skip any text up to the first brace
+
+ # Add bounds checking to prevent IndexError
+ if not json_string:
+ # Return None for empty strings
+ return None
+
+ self.index = self.get_start_pos(self.json_string)
+
+ # Ensure index is within bounds
+ if self.index >= len(self.json_string):
+ # If start position is beyond string length, return None
+ return None
+
self.current_char = self.json_string[self.index]
self._parse()
return self.result
diff --git a/python/helpers/extract_tools.py b/python/helpers/extract_tools.py
index c654f9db2..e4d1fcb74 100644
--- a/python/helpers/extract_tools.py
+++ b/python/helpers/extract_tools.py
@@ -6,10 +6,17 @@ import regex
from fnmatch import fnmatch
def json_parse_dirty(json:str) -> dict[str,Any] | None:
- ext_json = extract_json_object_string(json)
+ if not json or not isinstance(json, str):
+ return None
+
+ ext_json = extract_json_object_string(json.strip())
if ext_json:
- data = DirtyJson.parse_string(ext_json)
- if isinstance(data,dict): return data
+ try:
+ data = DirtyJson.parse_string(ext_json)
+ if isinstance(data,dict): return data
+ except Exception:
+ # If parsing fails, return None instead of crashing
+ return None
return None
def extract_json_object_string(content):
@@ -29,10 +36,10 @@ def extract_json_object_string(content):
def extract_json_string(content):
# Regular expression pattern to match a JSON object
pattern = r'\{(?:[^{}]|(?R))*\}|\[(?:[^\[\]]|(?R))*\]|"(?:\\.|[^"\\])*"|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?'
-
+
# Search for the pattern in the content
match = regex.search(pattern, content)
-
+
if match:
# Return the matched JSON string
return match.group(0)
@@ -77,4 +84,4 @@ def load_classes_from_folder(folder: str, name_pattern: str, base_class: Type[T]
if one_per_file:
break
- return classes
\ No newline at end of file
+ return classes
diff --git a/python/tools/browser_agent.py b/python/tools/browser_agent.py
index d06494a6b..1ce595d56 100644
--- a/python/tools/browser_agent.py
+++ b/python/tools/browser_agent.py
@@ -5,7 +5,7 @@ from agent import Agent, InterventionException
import models
from python.helpers.tool import Tool, Response
-from python.helpers import files, defer, persist_chat
+from python.helpers import files, defer, persist_chat, strings
from python.helpers.browser_use import browser_use
from python.extensions.message_loop_start._10_iteration_no import get_iter_no
from pydantic import BaseModel
@@ -184,10 +184,13 @@ class BrowserAgent(Tool):
result = await task.result()
answer = result.final_result()
try:
- answer_data = DirtyJson.parse_string(answer)
- answer_text = strings.dict_to_text(answer_data) # type: ignore
+ if answer and isinstance(answer, str) and answer.strip():
+ answer_data = DirtyJson.parse_string(answer)
+ answer_text = strings.dict_to_text(answer_data) # type: ignore
+ else:
+ answer_text = str(answer) if answer else "No result returned"
except Exception as e:
- answer_text = answer
+ answer_text = str(answer) if answer else f"Error processing result: {str(e)}"
self.log.update(answer=answer_text)
return Response(message=answer, break_loop=False)