mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Fix double JSON encoding when saving eval set results
The `model_dump_json()` method already returns a JSON string, so wrapping it in `json.dumps()` was causing double encoding Close #3993 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 852455534
This commit is contained in:
committed by
Copybara-Service
parent
bfed19cd78
commit
fc4e3d6f60
@@ -12,6 +12,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
|
||||
from google.adk.errors.not_found_error import NotFoundError
|
||||
from google.adk.evaluation._eval_set_results_manager_utils import _sanitize_eval_set_result_name
|
||||
from google.adk.evaluation._eval_set_results_manager_utils import create_eval_set_result
|
||||
@@ -165,6 +167,33 @@ class TestGcsEvalSetResultsManager:
|
||||
)
|
||||
assert retrieved_eval_set_result == eval_set_result
|
||||
|
||||
def test_get_eval_set_result_double_encoded_legacy(
|
||||
self, gcs_eval_set_results_manager, mocker
|
||||
):
|
||||
mocker.patch("time.time", return_value=12345678)
|
||||
app_name = "test_app"
|
||||
eval_set_id = "test_eval_set"
|
||||
eval_case_results = _get_test_eval_case_results()
|
||||
eval_set_result = create_eval_set_result(
|
||||
app_name, eval_set_id, eval_case_results
|
||||
)
|
||||
|
||||
blob_name = gcs_eval_set_results_manager._get_eval_set_result_blob_name(
|
||||
app_name, eval_set_result.eval_set_result_id
|
||||
)
|
||||
blob = gcs_eval_set_results_manager.bucket.blob(blob_name)
|
||||
double_encoded_json = json.dumps(eval_set_result.model_dump_json())
|
||||
blob.upload_from_string(
|
||||
double_encoded_json, content_type="application/json"
|
||||
)
|
||||
|
||||
retrieved_eval_set_result = (
|
||||
gcs_eval_set_results_manager.get_eval_set_result(
|
||||
app_name, eval_set_result.eval_set_result_id
|
||||
)
|
||||
)
|
||||
assert retrieved_eval_set_result == eval_set_result
|
||||
|
||||
def test_list_eval_set_results(self, gcs_eval_set_results_manager, mocker):
|
||||
mocker.patch("time.time", return_value=123)
|
||||
app_name = "test_app"
|
||||
|
||||
@@ -85,11 +85,12 @@ class TestLocalEvalSetResultsManager:
|
||||
)
|
||||
assert os.path.exists(expected_file_path)
|
||||
with open(expected_file_path, "r") as f:
|
||||
actual_eval_set_result_json = json.load(f)
|
||||
actual_eval_set_result_data = json.load(f)
|
||||
|
||||
# need to convert eval_set_result to json
|
||||
expected_eval_set_result_json = self.eval_set_result.model_dump_json()
|
||||
assert expected_eval_set_result_json == actual_eval_set_result_json
|
||||
# Verify the file contains a proper JSON object (not double-encoded)
|
||||
# Use mode='json' to serialize enums to their values for comparison
|
||||
expected_eval_set_result_data = self.eval_set_result.model_dump(mode="json")
|
||||
assert expected_eval_set_result_data == actual_eval_set_result_data
|
||||
|
||||
def test_get_eval_set_result(self, mocker):
|
||||
mock_time = mocker.patch("time.time")
|
||||
@@ -102,6 +103,24 @@ class TestLocalEvalSetResultsManager:
|
||||
)
|
||||
assert retrieved_result == self.eval_set_result
|
||||
|
||||
def test_get_eval_set_result_double_encoded_legacy(self):
|
||||
eval_history_dir = os.path.join(
|
||||
self.agents_dir, self.app_name, _ADK_EVAL_HISTORY_DIR
|
||||
)
|
||||
os.makedirs(eval_history_dir, exist_ok=True)
|
||||
eval_set_result_file_path = os.path.join(
|
||||
eval_history_dir,
|
||||
self.eval_set_result_name + _EVAL_SET_RESULT_FILE_EXTENSION,
|
||||
)
|
||||
double_encoded_json = json.dumps(self.eval_set_result.model_dump_json())
|
||||
with open(eval_set_result_file_path, "w", encoding="utf-8") as f:
|
||||
f.write(double_encoded_json)
|
||||
|
||||
retrieved_result = self.manager.get_eval_set_result(
|
||||
self.app_name, self.eval_set_result_name
|
||||
)
|
||||
assert retrieved_result == self.eval_set_result
|
||||
|
||||
def test_get_eval_set_result_not_found(self, mocker):
|
||||
mock_time = mocker.patch("time.time")
|
||||
mock_time.return_value = self.timestamp
|
||||
|
||||
Reference in New Issue
Block a user