Skip to content

Commit 2f5d692

Browse files
committed
feat: add JSON schema validation to config_to_agent
- Add jsonschema dependency for configuration validation - Implement JSON schema based on supported configuration keys - Provide detailed validation error messages with field paths - Add validation tests for invalid fields, types, and tool items - Support null values for optional fields (model, prompt, name) - Reject additional properties not in the schema - All 14 tests passing including new validation tests 🤖 Assisted by Amazon Q Developer
1 parent 3b4f9fd commit 2f5d692

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
"boto3>=1.26.0,<2.0.0",
3131
"botocore>=1.29.0,<2.0.0",
3232
"docstring_parser>=0.15,<1.0",
33+
"jsonschema>=4.0.0,<5.0.0",
3334
"mcp>=1.11.0,<2.0.0",
3435
"pydantic>=2.4.0,<3.0.0",
3536
"typing-extensions>=4.13.2,<5.0.0",

src/strands/experimental/agent_config.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,45 @@
66
import json
77
from pathlib import Path
88

9+
import jsonschema
10+
from jsonschema import ValidationError
11+
912
from ..agent import Agent
1013

14+
# JSON Schema for agent configuration
15+
AGENT_CONFIG_SCHEMA = {
16+
"$schema": "http://json-schema.org/draft-07/schema#",
17+
"title": "Agent Configuration",
18+
"description": "Configuration schema for creating agents",
19+
"type": "object",
20+
"properties": {
21+
"name": {
22+
"description": "Name of the agent",
23+
"type": ["string", "null"],
24+
"default": None
25+
},
26+
"model": {
27+
"description": "The model ID to use for this agent. If not specified, uses the default model.",
28+
"type": ["string", "null"],
29+
"default": None
30+
},
31+
"prompt": {
32+
"description": "The system prompt for the agent. Provides high level context to the agent.",
33+
"type": ["string", "null"],
34+
"default": None
35+
},
36+
"tools": {
37+
"description": "List of tools the agent can use. Can be module paths, file paths, or tool names.",
38+
"type": "array",
39+
"items": {
40+
"type": "string"
41+
},
42+
"default": []
43+
}
44+
},
45+
"additionalProperties": False
46+
}
47+
1148

1249
def config_to_agent(config: str | dict[str, any], **kwargs) -> Agent:
1350
"""Create an Agent from a configuration file or dictionary.
@@ -56,6 +93,14 @@ def config_to_agent(config: str | dict[str, any], **kwargs) -> Agent:
5693
else:
5794
raise ValueError("Config must be a file path string or dictionary")
5895

96+
# Validate configuration against schema
97+
try:
98+
jsonschema.validate(config_dict, AGENT_CONFIG_SCHEMA)
99+
except ValidationError as e:
100+
# Provide more detailed error message
101+
error_path = " -> ".join(str(p) for p in e.absolute_path) if e.absolute_path else "root"
102+
raise ValueError(f"Configuration validation error at {error_path}: {e.message}") from e
103+
59104
# Prepare Agent constructor arguments
60105
agent_kwargs = {}
61106

tests/strands/experimental/test_agent_config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,21 @@ def test_config_to_agent_ignores_none_values(self):
112112
agent = config_to_agent(config)
113113
assert agent.model.config["model_id"] == "test-model"
114114
# Agent should use its defaults for None values
115+
116+
def test_config_to_agent_validation_error_invalid_field(self):
117+
"""Test that invalid fields raise validation errors."""
118+
config = {"model": "test-model", "invalid_field": "value"}
119+
with pytest.raises(ValueError, match="Configuration validation error"):
120+
config_to_agent(config)
121+
122+
def test_config_to_agent_validation_error_wrong_type(self):
123+
"""Test that wrong field types raise validation errors."""
124+
config = {"model": "test-model", "tools": "not-a-list"}
125+
with pytest.raises(ValueError, match="Configuration validation error"):
126+
config_to_agent(config)
127+
128+
def test_config_to_agent_validation_error_invalid_tool_item(self):
129+
"""Test that invalid tool items raise validation errors."""
130+
config = {"model": "test-model", "tools": ["valid-tool", 123]}
131+
with pytest.raises(ValueError, match="Configuration validation error"):
132+
config_to_agent(config)

0 commit comments

Comments
 (0)