Skip to content

Commit 383b18b

Browse files
committed
perf: use pre-compiled JSON schema validator
- Create Draft7Validator instance at module level for better performance - Avoid loading and compiling schema on every validation call - Schema is loaded once at import time and validator is reused - Maintains all existing validation functionality and error messages - Standard best practice for jsonschema validation performance 🤖 Assisted by Amazon Q Developer
1 parent 874c42f commit 383b18b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/strands/experimental/agent_config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def _load_schema() -> dict:
1919
return json.load(f)
2020

2121

22+
# Pre-compile validator for better performance
23+
_VALIDATOR = jsonschema.Draft7Validator(_load_schema())
24+
25+
2226
def config_to_agent(config: str | dict[str, any], **kwargs) -> Agent:
2327
"""Create an Agent from a configuration file or dictionary.
2428
@@ -68,8 +72,7 @@ def config_to_agent(config: str | dict[str, any], **kwargs) -> Agent:
6872

6973
# Validate configuration against schema
7074
try:
71-
schema = _load_schema()
72-
jsonschema.validate(config_dict, schema)
75+
_VALIDATOR.validate(config_dict)
7376
except ValidationError as e:
7477
# Provide more detailed error message
7578
error_path = " -> ".join(str(p) for p in e.absolute_path) if e.absolute_path else "root"

0 commit comments

Comments
 (0)