Skip to content

Commit

Permalink
Fix issue #5637: [Bug]: Incorrectly interpreting a Python error as a …
Browse files Browse the repository at this point in the history
…success message
  • Loading branch information
openhands-agent committed Dec 17, 2024
1 parent ee8438c commit 6a999b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 10 additions & 2 deletions openhands/events/observation/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ class IPythonRunCellObservation(Observation):

@property
def error(self) -> bool:
return False # IPython cells do not return exit codes
# Check for common error indicators in IPython output
error_indicators = [
'ERROR:',
'Error:',
'Exception:',
'Parameter `',
'is required for command:',
]
return any(indicator in self.content for indicator in error_indicators)

@property
def message(self) -> str:
return 'Code executed in IPython cell.'

@property
def success(self) -> bool:
return True # IPython cells are always considered successful
return not self.error

def __str__(self) -> str:
return f'**IPythonRunCellObservation**\n{self.content}'
27 changes: 27 additions & 0 deletions tests/unit/test_observation_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ def serialization_deserialization(


# Additional tests for various observation subclasses can be included here
def test_ipython_error_detection():
from openhands.events.observation import IPythonRunCellObservation

# Test error detection for various error patterns
error_cases = [
'ERROR: Something went wrong',
'Error: Invalid syntax',
'Exception: Division by zero',
'Parameter `old_str` is required for command: str_replace',
]
for error_content in error_cases:
obs = IPythonRunCellObservation(content=error_content, code='print("test")')
serialized = event_to_dict(obs)
assert (
serialized['success'] is False
), f'Failed to detect error in: {error_content}'
assert obs.error is True, f'Failed to detect error in: {error_content}'

# Test success case
obs = IPythonRunCellObservation(
content='Hello World!', code='print("Hello World!")'
)
serialized = event_to_dict(obs)
assert serialized['success'] is True, 'Failed to detect success'
assert obs.error is False, 'Failed to detect success'


def test_success_field_serialization():
# Test success=True
obs = CmdOutputObservation(
Expand Down

0 comments on commit 6a999b0

Please sign in to comment.