Load and validate records from a ``.jsonl`` or ``.json`` file. Args: path: A ``.jsonl`` file (one record per line; blank lines are skipped) or a ``.json`` file containing an array of records. Returns: The validated records, in file order. Raises: Va
(path: str | Path)
| 225 | |
| 226 | |
| 227 | def load_dataset(path: str | Path) -> list[RouteRecord | GuardrailRecord]: |
| 228 | """Load and validate records from a ``.jsonl`` or ``.json`` file. |
| 229 | |
| 230 | Args: |
| 231 | path: A ``.jsonl`` file (one record per line; blank lines are skipped) |
| 232 | or a ``.json`` file containing an array of records. |
| 233 | |
| 234 | Returns: |
| 235 | The validated records, in file order. |
| 236 | |
| 237 | Raises: |
| 238 | ValueError: If the file or any record is malformed. |
| 239 | """ |
| 240 | path = Path(path) |
| 241 | text = path.read_text(encoding="utf-8-sig") |
| 242 | if path.suffix == ".jsonl": |
| 243 | items = _parse_jsonl(text) |
| 244 | elif path.suffix == ".json": |
| 245 | try: |
| 246 | data = json.loads(text) |
| 247 | except ValueError as e: |
| 248 | raise ValueError(f"Invalid JSON: {e}") from e |
| 249 | if not isinstance(data, list): |
| 250 | raise ValueError("A .json dataset must be an array of records") |
| 251 | items = [(f"index {i}", item) for i, item in enumerate(data)] |
| 252 | else: |
| 253 | raise ValueError(f"Unsupported dataset extension: {path.suffix!r}") |
| 254 | |
| 255 | records: list[RouteRecord | GuardrailRecord] = [] |
| 256 | seen: set[str] = set() |
| 257 | for location, item in items: |
| 258 | try: |
| 259 | record = _RECORD_ADAPTER.validate_python(item) |
| 260 | except ValidationError as e: |
| 261 | raise ValueError(f"Invalid record at {location}: {e}") from e |
| 262 | if record.id in seen: |
| 263 | raise ValueError(f"Duplicate record id {record.id!r} at {location}") |
| 264 | seen.add(record.id) |
| 265 | records.append(record) |
| 266 | return records |
| 267 | |
| 268 | |
| 269 | def evaluate( |