Fix #8510: Improve error messages for invalid microagent format (#8511)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Graham Neubig
2025-05-16 11:01:39 -04:00
committed by GitHub
parent 15f71e7ed6
commit 25619c5a93
3 changed files with 72 additions and 7 deletions

View File

@@ -65,7 +65,14 @@ class BaseMicroagent(BaseModel):
try:
metadata = MicroagentMetadata(**metadata_dict)
except Exception as e:
raise MicroagentValidationError(f'Error loading metadata: {e}') from e
# Provide more detailed error message for validation errors
error_msg = f'Error validating microagent metadata in {path.name}: {str(e)}'
if 'type' in metadata_dict and metadata_dict['type'] not in [
t.value for t in MicroagentType
]:
valid_types = ', '.join([f'"{t.value}"' for t in MicroagentType])
error_msg += f'. Invalid "type" value: "{metadata_dict["type"]}". Valid types are: {valid_types}'
raise MicroagentValidationError(error_msg) from e
# Create appropriate subclass based on type
subclass_map = {
@@ -184,7 +191,13 @@ def load_microagents_from_dir(
elif isinstance(agent, KnowledgeMicroagent):
knowledge_agents[agent.name] = agent
logger.debug(f'Loaded agent {agent.name} from {file}')
except MicroagentValidationError as e:
# For validation errors, include the original exception
error_msg = f'Error loading microagent from {file}: {str(e)}'
raise MicroagentValidationError(error_msg) from e
except Exception as e:
raise ValueError(f'Error loading agent from {file}: {e}')
# For other errors, wrap in a ValueError with detailed message
error_msg = f'Error loading microagent from {file}: {str(e)}'
raise ValueError(error_msg) from e
return repo_agents, knowledge_agents