generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add LocalFileTaskResultStore for caching task results locally #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
afarntrog
merged 1 commit into
strands-agents:main
from
afarntrog:eval_store_filesystem
Mar 26, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from pathlib import Path | ||
|
|
||
| from .types.evaluation import EvaluationData | ||
|
|
||
|
|
||
| class LocalFileTaskResultStore: | ||
| """Task result store backed by local JSON files. | ||
|
|
||
| Saves one JSON file per case in the specified directory, | ||
| using the case name as the filename. | ||
| """ | ||
|
|
||
| def __init__(self, directory: str | Path): | ||
| self._directory = Path(directory) | ||
| self._directory.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| def load(self, case_name: str) -> EvaluationData | None: | ||
| """Load a cached task result from a JSON file. | ||
|
|
||
| Args: | ||
| case_name: The name of the case to load results for. | ||
|
|
||
| Returns: | ||
| The cached EvaluationData if the file exists, None otherwise. | ||
| """ | ||
| path = self._directory / f"{case_name}.json" | ||
| if not path.exists(): | ||
| return None | ||
| return EvaluationData.model_validate_json(path.read_text()) | ||
poshinchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def save(self, case_name: str, result: EvaluationData) -> None: | ||
| """Save a task result to a JSON file. | ||
|
|
||
| Args: | ||
| case_name: The name of the case to save results for. | ||
| result: The EvaluationData to save. | ||
| """ | ||
| path = self._directory / f"{case_name}.json" | ||
| path.write_text(result.model_dump_json(indent=2)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| from strands_evals.local_file_task_result_store import LocalFileTaskResultStore | ||
| from strands_evals.types import EvaluationData | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def evaluation_data(): | ||
| return EvaluationData( | ||
| input="What is 2+2?", | ||
| actual_output="4", | ||
| name="math_case", | ||
| expected_output="4", | ||
| metadata={"difficulty": "easy"}, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def store(tmp_path): | ||
| return LocalFileTaskResultStore(directory=tmp_path / "results") | ||
|
|
||
|
|
||
| class TestLocalFileTaskResultStore: | ||
| def test_save_and_load(self, store, evaluation_data): | ||
| store.save("math_case", evaluation_data) | ||
| loaded = store.load("math_case") | ||
|
|
||
| assert loaded is not None | ||
| assert loaded.input == evaluation_data.input | ||
| assert loaded.actual_output == evaluation_data.actual_output | ||
| assert loaded.expected_output == evaluation_data.expected_output | ||
| assert loaded.name == evaluation_data.name | ||
| assert loaded.metadata == evaluation_data.metadata | ||
|
|
||
| def test_load_missing_returns_none(self, store): | ||
| result = store.load("nonexistent_case") | ||
| assert result is None | ||
|
|
||
| def test_creates_directory(self, tmp_path): | ||
| new_dir = tmp_path / "nested" / "results" | ||
| assert not new_dir.exists() | ||
| LocalFileTaskResultStore(directory=new_dir) | ||
| assert new_dir.exists() | ||
|
|
||
| def test_save_writes_json_file(self, store, evaluation_data, tmp_path): | ||
| store.save("math_case", evaluation_data) | ||
| file_path = tmp_path / "results" / "math_case.json" | ||
| assert file_path.exists() | ||
| data = json.loads(file_path.read_text()) | ||
| assert data["input"] == "What is 2+2?" | ||
| assert data["actual_output"] == "4" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.