Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Remove screenshot from microagent prompt (#1550)

Graham Neubig74e159add6bb
diff --git a/agenthub/micro/agent.py b/agenthub/micro/agent.py
index 4050929838..14a467d2ce 100644
--- a/agenthub/micro/agent.py
+++ b/agenthub/micro/agent.py
@@ -1,3 +1,4 @@
+import copy
import json
from typing import Dict, List
@@ -41,11 +42,39 @@ def my_encoder(obj):
return obj.to_dict()
+def _remove_fields(obj, fields: set[str]):
+ """
+ Remove fields from an object
+
+ Parameters:
+ - obj (Object): The object to remove fields from
+ - fields (set[str]): A set of field names to remove from the object
+ """
+ if isinstance(obj, dict):
+ for field in fields:
+ if field in obj:
+ del obj[field]
+ for _, value in obj.items():
+ _remove_fields(value, fields)
+ elif isinstance(obj, list) or isinstance(obj, tuple):
+ for item in obj:
+ _remove_fields(item, fields)
+ elif hasattr(obj, '__dataclass_fields__'):
+ for field in fields:
+ if field in obj.__dataclass_fields__:
+ setattr(obj, field, None)
+ for value in obj.__dict__.values():
+ _remove_fields(value, fields)
+
+
def to_json(obj, **kwargs):
"""
Serialize an object to str format
"""
- return json.dumps(obj, default=my_encoder, **kwargs)
+ # Remove things like screenshots that shouldn't be in a prompt
+ sanitized_obj = copy.deepcopy(obj)
+ _remove_fields(sanitized_obj, {'screenshot'})
+ return json.dumps(sanitized_obj, default=my_encoder, **kwargs)
class MicroAgent(Agent):