Skip to content

Commit 0c612fb

Browse files
jsondaicopybara-github
authored andcommitted
chore: Update evaluation dataset converter to decode string responses to JSON.
PiperOrigin-RevId: 776331488
1 parent 9463018 commit 0c612fb

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

vertexai/_genai/_evals_data_converters.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Dataset converters for evals."""
1616

1717
import abc
18+
import json
1819
import logging
1920
from typing import Any, Optional
2021

@@ -355,7 +356,29 @@ def convert(self, raw_data: list[dict[str, Any]]) -> types.EvaluationDataset:
355356
continue
356357

357358
request_data = item.get("request", {})
358-
response_data = item.get("response", {})
359+
response_data_raw = item.get("response", {})
360+
361+
response_data = {}
362+
if isinstance(response_data_raw, str):
363+
try:
364+
loaded_json = json.loads(response_data_raw)
365+
if isinstance(loaded_json, dict):
366+
response_data = loaded_json
367+
else:
368+
logger.warning(
369+
"Decoded response JSON is not a dictionary for case"
370+
" %s. Type: %s",
371+
i,
372+
type(loaded_json),
373+
)
374+
except json.JSONDecodeError:
375+
logger.warning(
376+
"Could not decode response JSON string for case %s."
377+
" Treating as empty response.",
378+
i,
379+
)
380+
elif isinstance(response_data_raw, dict):
381+
response_data = response_data_raw
359382

360383
messages = request_data.get("messages", [])
361384
choices = response_data.get("choices", [])

0 commit comments

Comments
 (0)