-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidate_output.py
More file actions
295 lines (241 loc) · 8.73 KB
/
Copy pathvalidate_output.py
File metadata and controls
295 lines (241 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""
Validate benchmark output files against the required submission schema.
Each output file must be a JSON array of records matching this structure:
[
{
"uuid": "<string>",
"domain": "<string>",
"status": "success" | "error",
"error": "<string>",
"duration_s": <float>,
"output": [
{
"turn_id": <int>,
"query": "<string>",
"answer": "<string>",
"sequence": {
"tool_call": [
{"name": "<string>", "arguments": {<object>}}
]
}
}
]
}
]
Usage:
# Validate all four capabilities under output/
python validate_output.py --all
# Validate a single capability (finds all output/capability_2_*/ dirs)
python validate_output.py --capability 2
# Validate a specific run directory directly
python validate_output.py output/capability_2_mar_22_11_30am/
# Validate a single domain file
python validate_output.py output/capability_2_mar_22_11_30am/hockey.json
# Use a non-default output root
python validate_output.py --all --output-dir my_results/
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import Any, Literal
from pydantic import BaseModel, ValidationError
# ---------------------------------------------------------------------------
# Schema models — keep in sync with examples/quick_start_benchmark/run_benchmark.py
# ---------------------------------------------------------------------------
class ToolCall(BaseModel):
name: str
arguments: dict[str, Any]
class Sequence(BaseModel):
tool_call: list[ToolCall]
class Turn(BaseModel):
turn_id: int
query: str
answer: str
sequence: Sequence
class OutputRecord(BaseModel):
uuid: str
domain: str
status: Literal["success", "error"]
error: str
duration_s: float
output: list[Turn]
# ---------------------------------------------------------------------------
# Validation logic
# ---------------------------------------------------------------------------
def validate_file(path: Path) -> list[str]:
"""
Validate a single output file. Returns a list of error strings (empty
list means the file is valid).
"""
errors: list[str] = []
try:
raw = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
return [f"Invalid JSON: {exc}"]
if not isinstance(raw, list):
return ["Top-level value must be a JSON array."]
if len(raw) == 0:
errors.append("Array is empty — no records found.")
return errors
for i, item in enumerate(raw):
if not isinstance(item, dict):
errors.append(f"Record {i}: expected an object, got {type(item).__name__}")
continue
uuid_label = item.get("uuid", f"<index {i}>")
try:
OutputRecord.model_validate(item)
except ValidationError as exc:
for e in exc.errors():
loc = " -> ".join(str(x) for x in e["loc"])
errors.append(f"Record {i} (uuid={uuid_label}) [{loc}]: {e['msg']}")
return errors
def collect_files(targets: list[str]) -> list[Path]:
"""Expand file paths and directories into a flat list of .json files.
When scanning a directory, *_tools.json sidecar files are skipped — they
use a different schema (tool shortlisting logs) and are not submission
output.
"""
paths: list[Path] = []
for t in targets:
p = Path(t)
if p.is_dir():
found = sorted(
f for f in p.glob("*.json") if not f.name.endswith("_tools.json")
)
if not found:
print(f" Warning: no output .json files found in {p}")
paths.extend(found)
elif p.exists():
if p.name.endswith("_tools.json"):
print(f" Skipping tool-log file: {p}")
else:
paths.append(p)
else:
print(f" Warning: path not found: {p}")
return paths
def find_capability_dirs(capability_id: int, output_dir: Path) -> list[Path]:
"""Return all output directories matching capability_{id}_* under output_dir."""
return sorted(
d for d in output_dir.glob(f"capability_{capability_id}_*") if d.is_dir()
)
def validate_targets(targets: list[str]) -> tuple[int, int]:
"""Validate a list of file/dir targets. Returns (total_errors, file_count)."""
files = collect_files(targets)
if not files:
print(" No files to validate.")
return 0, 0
total_errors = 0
for path in files:
errors = validate_file(path)
if errors:
print(f" FAIL {path} ({len(errors)} error(s))")
for err in errors:
print(f" {err}")
total_errors += len(errors)
else:
try:
n = len(json.loads(path.read_text(encoding="utf-8")))
except Exception:
n = 0
print(f" OK {path} ({n} record(s))")
return total_errors, len(files)
def main() -> int:
parser = argparse.ArgumentParser(
description="Validate benchmark output files against the submission schema.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"targets",
nargs="*",
metavar="FILE_OR_DIR",
help="Output JSON file(s) or directory/directories to validate.",
)
parser.add_argument(
"--capability", "-c",
type=int,
choices=[1, 2, 3, 4],
metavar="N",
help="Validate all output directories for capability N (looks in --output-dir).",
)
parser.add_argument(
"--all", "-a",
action="store_true",
help="Validate output directories for all four capabilities.",
)
parser.add_argument(
"--output-dir",
default="output",
metavar="DIR",
help="Root output directory to search when using --capability or --all (default: output/).",
)
args = parser.parse_args()
if not args.targets and not args.capability and not args.all:
parser.print_help()
return 1
output_dir = Path(args.output_dir)
# Determine which capability IDs to scan
if args.all:
cap_ids = [1, 2, 3, 4]
elif args.capability:
cap_ids = [args.capability]
else:
cap_ids = []
# When specific file/dir targets are given, validate them directly
if args.targets and not cap_ids:
files = collect_files(args.targets)
if not files:
print("No files to validate.")
return 1
total_errors = 0
for path in files:
errors = validate_file(path)
if errors:
print(f"FAIL {path} ({len(errors)} error(s))")
for err in errors:
print(f" {err}")
total_errors += len(errors)
else:
try:
n = len(json.loads(path.read_text(encoding="utf-8")))
except Exception:
n = 0
print(f"OK {path} ({n} record(s))")
print()
if total_errors:
print(f"FAILED — {total_errors} schema error(s) across {len(files)} file(s).")
return 1
print(f"PASSED — {len(files)} file(s) valid.")
return 0
# Capability-scoped validation
grand_errors = 0
grand_files = 0
for cap_id in cap_ids:
cap_dirs = find_capability_dirs(cap_id, output_dir)
# Also include any explicit targets alongside --capability
extra = list(args.targets) if args.targets else []
targets_for_cap = [str(d) for d in cap_dirs] + extra
print(f"\n── Capability {cap_id} ──────────────────────────────────────────")
if not cap_dirs and not extra:
print(f" No output directories found under {output_dir}/capability_{cap_id}_*/")
continue
errs, nfiles = validate_targets(targets_for_cap)
grand_errors += errs
grand_files += nfiles
if nfiles:
status = "PASSED" if errs == 0 else "FAILED"
print(f" → {status}: {nfiles} file(s), {errs} error(s)")
print()
if grand_files == 0:
print("No files found to validate.")
return 1
if grand_errors:
print(f"OVERALL FAILED — {grand_errors} schema error(s) across {grand_files} file(s).")
return 1
print(f"OVERALL PASSED — {grand_files} file(s) valid.")
return 0
if __name__ == "__main__":
sys.exit(main())