Skip to content

Commit 953ae34

Browse files
committed
fix: improve tool validation error messages and add comprehensive tests
- Fix error message for missing modules to be more descriptive - Remove redundant 'to properly import this tool' text from error messages - Add specific error messages for missing modules vs missing functions - Add unit tests for each error case: - Invalid tool (not file/module/@tool) - Missing module (module doesn't exist) - Missing function (function not found in existing module) - All 17 tests passing with better error coverage 🤖 Assisted by Amazon Q Developer
1 parent a051bef commit 953ae34

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/strands/experimental/agent_config.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ def _validate_tools(tools: list[str]) -> None:
8282
module = importlib.import_module(module_path)
8383
if not hasattr(module, func_name):
8484
raise ValueError(
85-
f"Tool '{tool}' not found. The configured tool is not annotated with @tool, "
86-
f"and is not a module or file. To properly import this tool, you must annotate it with @tool."
85+
f"Function '{func_name}' not found in module '{module_path}'. "
86+
f"Ensure the function exists and is annotated with @tool."
8787
)
8888
except ImportError:
8989
raise ValueError(
90-
f"Tool '{tool}' not found. The configured tool is not annotated with @tool, "
91-
f"and is not a module or file. To properly import this tool, you must annotate it with @tool."
90+
f"Module '{module_path}' not found. "
91+
f"Ensure the module exists and is importable, or use a valid file path."
9292
)
9393
else:
9494
raise ValueError(
95-
f"Tool '{tool}' not found. The configured tool is not annotated with @tool, "
96-
f"and is not a module or file. To properly import this tool, you must annotate it with @tool."
95+
f"Tool '{tool}' not found. "
96+
f"The configured tool is not annotated with @tool, and is not a module or file."
9797
)
9898

9999

tests/strands/experimental/test_agent_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,15 @@ def test_config_to_agent_validation_error_invalid_tool(self):
136136
config = {"model": "test-model", "tools": ["nonexistent_tool"]}
137137
with pytest.raises(ValueError, match="The configured tool is not annotated with @tool"):
138138
config_to_agent(config)
139+
140+
def test_config_to_agent_validation_error_missing_module(self):
141+
"""Test that missing modules raise helpful error messages."""
142+
config = {"model": "test-model", "tools": ["nonexistent.module.tool"]}
143+
with pytest.raises(ValueError, match="Module 'nonexistent.module' not found"):
144+
config_to_agent(config)
145+
146+
def test_config_to_agent_validation_error_missing_function(self):
147+
"""Test that missing functions in existing modules raise helpful error messages."""
148+
config = {"model": "test-model", "tools": ["json.nonexistent_function"]}
149+
with pytest.raises(ValueError, match="Function 'nonexistent_function' not found in module 'json'"):
150+
config_to_agent(config)

0 commit comments

Comments
 (0)