nemo-evaluator-launcher 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nemo-evaluator-launcher might be problematic. Click here for more details.

Files changed (57) hide show
  1. nemo_evaluator_launcher/__init__.py +65 -0
  2. nemo_evaluator_launcher/api/__init__.py +24 -0
  3. nemo_evaluator_launcher/api/functional.py +678 -0
  4. nemo_evaluator_launcher/api/types.py +89 -0
  5. nemo_evaluator_launcher/api/utils.py +19 -0
  6. nemo_evaluator_launcher/cli/__init__.py +15 -0
  7. nemo_evaluator_launcher/cli/export.py +148 -0
  8. nemo_evaluator_launcher/cli/info.py +117 -0
  9. nemo_evaluator_launcher/cli/kill.py +39 -0
  10. nemo_evaluator_launcher/cli/ls_runs.py +113 -0
  11. nemo_evaluator_launcher/cli/ls_tasks.py +134 -0
  12. nemo_evaluator_launcher/cli/main.py +143 -0
  13. nemo_evaluator_launcher/cli/run.py +135 -0
  14. nemo_evaluator_launcher/cli/status.py +120 -0
  15. nemo_evaluator_launcher/cli/version.py +52 -0
  16. nemo_evaluator_launcher/common/__init__.py +16 -0
  17. nemo_evaluator_launcher/common/execdb.py +189 -0
  18. nemo_evaluator_launcher/common/helpers.py +194 -0
  19. nemo_evaluator_launcher/common/logging_utils.py +349 -0
  20. nemo_evaluator_launcher/common/mapping.py +295 -0
  21. nemo_evaluator_launcher/configs/__init__.py +15 -0
  22. nemo_evaluator_launcher/configs/default.yaml +28 -0
  23. nemo_evaluator_launcher/configs/deployment/nim.yaml +32 -0
  24. nemo_evaluator_launcher/configs/deployment/none.yaml +16 -0
  25. nemo_evaluator_launcher/configs/deployment/sglang.yaml +38 -0
  26. nemo_evaluator_launcher/configs/deployment/vllm.yaml +41 -0
  27. nemo_evaluator_launcher/configs/execution/lepton/default.yaml +92 -0
  28. nemo_evaluator_launcher/configs/execution/local.yaml +17 -0
  29. nemo_evaluator_launcher/configs/execution/slurm/default.yaml +33 -0
  30. nemo_evaluator_launcher/executors/__init__.py +22 -0
  31. nemo_evaluator_launcher/executors/base.py +97 -0
  32. nemo_evaluator_launcher/executors/lepton/__init__.py +16 -0
  33. nemo_evaluator_launcher/executors/lepton/deployment_helpers.py +589 -0
  34. nemo_evaluator_launcher/executors/lepton/executor.py +905 -0
  35. nemo_evaluator_launcher/executors/lepton/job_helpers.py +394 -0
  36. nemo_evaluator_launcher/executors/local/__init__.py +15 -0
  37. nemo_evaluator_launcher/executors/local/executor.py +491 -0
  38. nemo_evaluator_launcher/executors/local/run.template.sh +88 -0
  39. nemo_evaluator_launcher/executors/registry.py +38 -0
  40. nemo_evaluator_launcher/executors/slurm/__init__.py +15 -0
  41. nemo_evaluator_launcher/executors/slurm/executor.py +996 -0
  42. nemo_evaluator_launcher/exporters/__init__.py +36 -0
  43. nemo_evaluator_launcher/exporters/base.py +112 -0
  44. nemo_evaluator_launcher/exporters/gsheets.py +391 -0
  45. nemo_evaluator_launcher/exporters/local.py +488 -0
  46. nemo_evaluator_launcher/exporters/mlflow.py +448 -0
  47. nemo_evaluator_launcher/exporters/registry.py +40 -0
  48. nemo_evaluator_launcher/exporters/utils.py +669 -0
  49. nemo_evaluator_launcher/exporters/wandb.py +376 -0
  50. nemo_evaluator_launcher/package_info.py +38 -0
  51. nemo_evaluator_launcher/resources/mapping.toml +344 -0
  52. nemo_evaluator_launcher-0.1.0.dist-info/METADATA +494 -0
  53. nemo_evaluator_launcher-0.1.0.dist-info/RECORD +57 -0
  54. nemo_evaluator_launcher-0.1.0.dist-info/WHEEL +5 -0
  55. nemo_evaluator_launcher-0.1.0.dist-info/entry_points.txt +3 -0
  56. nemo_evaluator_launcher-0.1.0.dist-info/licenses/LICENSE +451 -0
  57. nemo_evaluator_launcher-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,678 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ """Public API functions for nemo-evaluator-launcher.
17
+
18
+ This module provides the main functional entry points for running evaluations, querying job status, and listing available tasks. These functions are intended to be used by CLI commands and external integrations.
19
+ """
20
+
21
+ from pathlib import Path
22
+ from typing import Any, List, Optional, Union
23
+
24
+ import yaml
25
+ from omegaconf import DictConfig, OmegaConf
26
+
27
+ from nemo_evaluator_launcher.api.types import RunConfig
28
+ from nemo_evaluator_launcher.common.execdb import ExecutionDB, JobData
29
+ from nemo_evaluator_launcher.common.mapping import load_tasks_mapping
30
+ from nemo_evaluator_launcher.executors.registry import get_executor
31
+ from nemo_evaluator_launcher.exporters import create_exporter
32
+
33
+
34
+ def get_tasks_list() -> list[list[Any]]:
35
+ """Get a list of available tasks from the mapping.
36
+
37
+ Returns:
38
+ list[list[Any]]: Each sublist contains task name, endpoint type, harness, and container.
39
+ """
40
+ mapping = load_tasks_mapping()
41
+ data = [
42
+ [
43
+ task_data.get("task"),
44
+ task_data.get("endpoint_type"),
45
+ task_data.get("harness"),
46
+ task_data.get("container"),
47
+ ]
48
+ for task_data in mapping.values()
49
+ ]
50
+ return data
51
+
52
+
53
+ def _validate_no_missing_values(cfg: Any, path: str = "") -> None:
54
+ """Recursively validate that no MISSING values exist in the configuration.
55
+
56
+ Args:
57
+ cfg: The configuration object to validate.
58
+ path: Current path in the configuration for error reporting.
59
+
60
+ Raises:
61
+ ValueError: If any MISSING values are found in the configuration.
62
+ """
63
+ if OmegaConf.is_dict(cfg):
64
+ for key, value in cfg.items():
65
+ current_path = f"{path}.{key!s}" if path else str(key)
66
+ # Check if this specific key has a MISSING value
67
+ if OmegaConf.is_missing(cfg, key):
68
+ raise ValueError(
69
+ f"Configuration has MISSING value at path: {current_path!s}"
70
+ )
71
+ _validate_no_missing_values(value, current_path)
72
+ elif OmegaConf.is_list(cfg):
73
+ for i, value in enumerate(cfg):
74
+ current_path = f"{path}[{i}]"
75
+ _validate_no_missing_values(value, current_path)
76
+
77
+
78
+ def run_eval(cfg: RunConfig, dry_run: bool = False) -> Optional[str]:
79
+ """Run evaluation with specified config and overrides.
80
+
81
+ Args:
82
+ cfg: The configuration object for the evaluation run.
83
+ dry_run: If True, do not run the evaluation, just prepare scripts and save them.
84
+
85
+ Returns:
86
+ Optional[str]: The invocation ID for the evaluation run.
87
+
88
+ Raises:
89
+ ValueError: If configuration validation fails or MISSING values are found.
90
+ RuntimeError: If the executor fails to start the evaluation.
91
+ """
92
+ # Validate that no MISSING values exist in the configuration
93
+ _validate_no_missing_values(cfg)
94
+
95
+ if dry_run:
96
+ print(OmegaConf.to_yaml(cfg))
97
+
98
+ _check_api_endpoint_when_deployment_is_configured(cfg)
99
+ return get_executor(cfg.execution.type).execute_eval(cfg, dry_run)
100
+
101
+
102
+ def get_status(job_ids: list[str]) -> list[dict[str, Any]]:
103
+ """Get status of jobs by their IDs or invocation IDs.
104
+
105
+ Args:
106
+ job_ids: List of job IDs or invocation IDs to check status for.
107
+
108
+ Returns:
109
+ list[dict[str, Any]]: List of status dictionaries for each job or invocation.
110
+ Each dictionary contains keys: 'invocation', 'job_id', 'status', and 'data'.
111
+ If a job or invocation is not found, status is 'not_found'.
112
+ If an error occurs, status is 'error' and 'data' contains error details.
113
+ """
114
+ db = ExecutionDB()
115
+ results: List[dict[str, Any]] = []
116
+
117
+ for job_id in job_ids:
118
+ # If id looks like an invocation_id (8 hex digits, no dot), get all jobs for it
119
+ if len(job_id) == 8 and "." not in job_id:
120
+ jobs = db.get_jobs(job_id)
121
+ if not jobs:
122
+ results.append(
123
+ {
124
+ "invocation": job_id,
125
+ "job_id": None,
126
+ "status": "not_found",
127
+ "data": {},
128
+ }
129
+ )
130
+ continue
131
+
132
+ # Get the executor class from the first job
133
+ first_job_data = next(iter(jobs.values()))
134
+ try:
135
+ executor_cls = get_executor(first_job_data.executor)
136
+ except ValueError as e:
137
+ results.append(
138
+ {
139
+ "invocation": job_id,
140
+ "job_id": None,
141
+ "status": "error",
142
+ "data": {"error": str(e)},
143
+ }
144
+ )
145
+ continue
146
+
147
+ # Get status from the executor for all jobs in the invocation
148
+ try:
149
+ status_list = executor_cls.get_status(job_id)
150
+
151
+ # Create a result for each job in the invocation
152
+ for job_id_in_invocation, job_data in jobs.items():
153
+ # Find the status for this specific job
154
+ job_status: str | None = None
155
+ job_progress: Optional[dict[str, Any]] = None
156
+ for status in status_list:
157
+ if status.id == job_id_in_invocation:
158
+ job_status = status.state.value
159
+ job_progress = status.progress
160
+ break
161
+
162
+ results.append(
163
+ {
164
+ "invocation": job_id,
165
+ "job_id": job_id_in_invocation,
166
+ "status": (
167
+ job_status if job_status is not None else "unknown"
168
+ ),
169
+ "progress": (
170
+ job_progress if job_progress is not None else "unknown"
171
+ ),
172
+ "data": job_data.data,
173
+ }
174
+ )
175
+
176
+ except Exception as e:
177
+ results.append(
178
+ {
179
+ "invocation": job_id,
180
+ "job_id": None,
181
+ "status": "error",
182
+ "data": {"error": str(e)},
183
+ }
184
+ )
185
+ else:
186
+ # Otherwise, treat as job_id
187
+ single_job_data: Optional[JobData] = db.get_job(job_id)
188
+
189
+ if single_job_data is None:
190
+ results.append(
191
+ {
192
+ "invocation": None,
193
+ "job_id": job_id,
194
+ "status": "not_found",
195
+ "data": {},
196
+ }
197
+ )
198
+ continue
199
+
200
+ # Get the executor class
201
+ try:
202
+ executor_cls = get_executor(single_job_data.executor)
203
+ except ValueError as e:
204
+ results.append(
205
+ {
206
+ "invocation": None,
207
+ "job_id": job_id,
208
+ "status": "error",
209
+ "data": {"error": str(e)},
210
+ }
211
+ )
212
+ continue
213
+
214
+ # Get status from the executor
215
+ try:
216
+ status_list = executor_cls.get_status(job_id)
217
+
218
+ if not status_list:
219
+ results.append(
220
+ {
221
+ "invocation": single_job_data.invocation_id,
222
+ "job_id": job_id,
223
+ "status": "unknown",
224
+ "data": single_job_data.data,
225
+ }
226
+ )
227
+ else:
228
+ # For individual job queries, return the first status
229
+ results.append(
230
+ {
231
+ "invocation": single_job_data.invocation_id,
232
+ "job_id": job_id,
233
+ "status": (
234
+ status_list[0].state.value if status_list else "unknown"
235
+ ),
236
+ "progress": (
237
+ status_list[0].progress if status_list else "unknown"
238
+ ),
239
+ "data": single_job_data.data,
240
+ }
241
+ )
242
+
243
+ except Exception as e:
244
+ results.append(
245
+ {
246
+ "invocation": (
247
+ single_job_data.invocation_id if single_job_data else None
248
+ ),
249
+ "job_id": job_id,
250
+ "status": "error",
251
+ "data": {"error": str(e)},
252
+ }
253
+ )
254
+
255
+ return results
256
+
257
+
258
+ def list_all_invocations_summary() -> list[dict[str, Any]]:
259
+ """Return a concise per-invocation summary from the exec DB.
260
+
261
+ Columns: invocation_id, earliest_job_ts, num_jobs, executor (or 'mixed').
262
+ Sorted by earliest_job_ts (newest first).
263
+ """
264
+ db = ExecutionDB()
265
+ jobs = db.get_all_jobs()
266
+
267
+ inv_to_earliest: dict[str, float] = {}
268
+ inv_to_count: dict[str, int] = {}
269
+ inv_to_execs: dict[str, set[str]] = {}
270
+
271
+ for jd in jobs.values():
272
+ inv = jd.invocation_id
273
+ ts = jd.timestamp or 0.0
274
+ if inv not in inv_to_earliest or ts < inv_to_earliest[inv]:
275
+ inv_to_earliest[inv] = ts
276
+ inv_to_count[inv] = inv_to_count.get(inv, 0) + 1
277
+ if inv not in inv_to_execs:
278
+ inv_to_execs[inv] = set()
279
+ inv_to_execs[inv].add(jd.executor)
280
+
281
+ rows: list[dict[str, Any]] = []
282
+ for inv, earliest_ts in inv_to_earliest.items():
283
+ execs = inv_to_execs.get(inv, set())
284
+ executor = (
285
+ next(iter(execs)) if len(execs) == 1 else ("mixed" if execs else None)
286
+ )
287
+ rows.append(
288
+ {
289
+ "invocation_id": inv,
290
+ "earliest_job_ts": earliest_ts,
291
+ "num_jobs": inv_to_count.get(inv, 0),
292
+ "executor": executor,
293
+ }
294
+ )
295
+
296
+ rows.sort(key=lambda r: r.get("earliest_job_ts") or 0, reverse=True)
297
+ return rows
298
+
299
+
300
+ def get_invocation_benchmarks(invocation_id: str) -> list[str]:
301
+ """Return a sorted list of benchmark/task names for a given invocation.
302
+
303
+ Extracted from stored configs in the execution DB. If anything goes wrong,
304
+ returns an empty list; callers can display 'unknown' if desired.
305
+ """
306
+ db = ExecutionDB()
307
+ jobs = db.get_jobs(invocation_id)
308
+ names: set[str] = set()
309
+ for jd in jobs.values():
310
+ try:
311
+ cfg = jd.config or {}
312
+ tasks = (cfg.get("evaluation", {}) or {}).get("tasks", []) or []
313
+ for t in tasks:
314
+ n = t.get("name") if isinstance(t, dict) else None
315
+ if n:
316
+ names.add(str(n))
317
+ except Exception:
318
+ # Ignore malformed entries; continue collecting from others
319
+ continue
320
+ return sorted(names)
321
+
322
+
323
+ def kill_job_or_invocation(id: str) -> list[dict[str, Any]]:
324
+ """Kill a job or an entire invocation by its ID.
325
+
326
+ Args:
327
+ id: The job ID (e.g., aefc4819.0) or invocation ID (e.g., aefc4819) to kill.
328
+
329
+ Returns:
330
+ list[dict[str, Any]]: List of kill operation results.
331
+ Each dictionary contains keys: 'invocation', 'job_id', 'status', and 'data'.
332
+ If a job is not found, status is 'not_found'.
333
+ If an error occurs, status is 'error' and 'data' contains error details.
334
+ """
335
+ db = ExecutionDB()
336
+ results = []
337
+
338
+ def kill_single_job(job_id: str, job_data: JobData) -> dict[str, Any]:
339
+ """Helper function to kill a single job."""
340
+ try:
341
+ executor_cls = get_executor(job_data.executor)
342
+ if hasattr(executor_cls, "kill_job"):
343
+ executor_cls.kill_job(job_id)
344
+ # Success - job was killed
345
+ return {
346
+ "invocation": job_data.invocation_id,
347
+ "job_id": job_id,
348
+ "status": "killed",
349
+ "data": {"result": "Successfully killed job"},
350
+ }
351
+ else:
352
+ return {
353
+ "invocation": job_data.invocation_id,
354
+ "job_id": job_id,
355
+ "status": "error",
356
+ "data": {
357
+ "error": f"Executor {job_data.executor} does not support killing jobs"
358
+ },
359
+ }
360
+ except (ValueError, RuntimeError) as e:
361
+ # Expected errors from kill_job
362
+ return {
363
+ "invocation": job_data.invocation_id,
364
+ "job_id": job_id,
365
+ "status": "error",
366
+ "data": {"error": str(e)},
367
+ }
368
+ except Exception as e:
369
+ # Unexpected errors
370
+ return {
371
+ "invocation": job_data.invocation_id,
372
+ "job_id": job_id,
373
+ "status": "error",
374
+ "data": {"error": f"Unexpected error: {str(e)}"},
375
+ }
376
+
377
+ # Determine if this is a job ID or invocation ID
378
+ if "." in id:
379
+ # This is a job ID - kill single job
380
+ job_data = db.get_job(id)
381
+ if job_data is None:
382
+ return [
383
+ {
384
+ "invocation": None,
385
+ "job_id": id,
386
+ "status": "not_found",
387
+ "data": {},
388
+ }
389
+ ]
390
+ results.append(kill_single_job(id, job_data))
391
+ else:
392
+ # This is an invocation ID - kill all jobs in the invocation
393
+ jobs = db.get_jobs(id)
394
+ if not jobs:
395
+ return [
396
+ {
397
+ "invocation": id,
398
+ "job_id": None,
399
+ "status": "not_found",
400
+ "data": {},
401
+ }
402
+ ]
403
+
404
+ # Kill each job in the invocation
405
+ for job_id, job_data in jobs.items():
406
+ results.append(kill_single_job(job_id, job_data))
407
+
408
+ return results
409
+
410
+
411
+ def export_results(
412
+ invocation_ids: Union[str, List[str]],
413
+ dest: str = "local",
414
+ config: dict[Any, Any] | None = None,
415
+ ) -> dict:
416
+ """Export results for one or more IDs (jobs/invocations/pipeline IDs) to a destination.
417
+
418
+ Args:
419
+ invocation_ids: Single invocation ID or list of invocation/job IDs
420
+ dest: Export destination (local, wandb, jet, mlflow, gsheets)
421
+ config: exporter configuration
422
+
423
+ Returns:
424
+ Export evaluation results dictionary
425
+ """
426
+
427
+ try:
428
+ # Normalize to list
429
+ if isinstance(invocation_ids, str):
430
+ invocation_ids = [invocation_ids]
431
+
432
+ exporter = create_exporter(dest, config or {})
433
+
434
+ if len(invocation_ids) == 1:
435
+ # Single id (job or invocation)
436
+ single_id = invocation_ids[0]
437
+
438
+ if "." in single_id: # job_id
439
+ md_job_data = None
440
+ # Use artifacts/run_config.yml if present
441
+ ypath_artifacts = Path("run_config.yml")
442
+ if ypath_artifacts.exists():
443
+ try:
444
+ cfg_yaml = (
445
+ yaml.safe_load(ypath_artifacts.read_text(encoding="utf-8"))
446
+ or {}
447
+ )
448
+ # merge exporter config if present
449
+ ypath_export = Path("export_config.yml")
450
+ if ypath_export.exists():
451
+ exp_yaml = (
452
+ yaml.safe_load(ypath_export.read_text(encoding="utf-8"))
453
+ or {}
454
+ )
455
+ exec_cfg = cfg_yaml.get("execution") or {}
456
+ auto_exp = (exp_yaml.get("execution") or {}).get(
457
+ "auto_export"
458
+ )
459
+ if auto_exp is not None:
460
+ exec_cfg["auto_export"] = auto_exp
461
+ cfg_yaml["execution"] = exec_cfg
462
+ # metadata
463
+ md_job_data = JobData(
464
+ invocation_id=single_id.split(".")[0],
465
+ job_id=single_id,
466
+ timestamp=0.0,
467
+ executor="local", #
468
+ data={"output_dir": str(Path.cwd().parent)},
469
+ config=cfg_yaml,
470
+ )
471
+ except Exception:
472
+ md_job_data = None
473
+ # fallback to execDB only
474
+ job_data = md_job_data or ExecutionDB().get_job(single_id)
475
+ if job_data is None:
476
+ return {
477
+ "success": False,
478
+ "error": f"Job {single_id} not found in ExecutionDB",
479
+ }
480
+
481
+ # Convert single job result to invocation-like structure
482
+ job_result = exporter.export_job(job_data)
483
+ return {
484
+ "success": job_result.success,
485
+ "invocation_id": job_data.invocation_id,
486
+ "jobs": {
487
+ job_data.job_id: {
488
+ "success": job_result.success,
489
+ "message": job_result.message,
490
+ "metadata": job_result.metadata or {},
491
+ }
492
+ },
493
+ "metadata": job_result.metadata or {},
494
+ }
495
+ elif single_id.isdigit(): # pipeline_id
496
+ # Find job by pipeline_id
497
+ db = ExecutionDB()
498
+ # Search all jobs for matching pipeline_id
499
+ for job_id, job_data in db._jobs.items():
500
+ if job_data.data.get("pipeline_id") == int(single_id):
501
+ job_result = exporter.export_job(job_data)
502
+ return {
503
+ "success": job_result.success,
504
+ "invocation_id": job_data.invocation_id,
505
+ "jobs": {
506
+ job_data.job_id: {
507
+ "success": job_result.success,
508
+ "message": job_result.message,
509
+ "metadata": job_result.metadata or {},
510
+ }
511
+ },
512
+ "metadata": job_result.metadata or {},
513
+ }
514
+ return {"success": False, "error": f"Pipeline {single_id} not found"}
515
+ else: # invocation_id
516
+ result = exporter.export_invocation(single_id)
517
+ # Ensure metadata is present in job results to prevent KeyError
518
+ if "jobs" in result:
519
+ for job_id, job_result in result["jobs"].items():
520
+ if "metadata" not in job_result:
521
+ job_result["metadata"] = {}
522
+ return result # type: ignore[no-any-return]
523
+ else:
524
+ # Multiple IDs - parse and group
525
+ db = ExecutionDB()
526
+ grouped_jobs: dict[
527
+ str, dict[str, Any]
528
+ ] = {} # invocation_id -> {job_id: job_data}
529
+ invocation_only = set() # invocation_ids with no specific jobs
530
+ all_jobs_for_consolidated = {} # job_id -> job_data (for consolidated export)
531
+
532
+ # Parse and group IDs
533
+ for id_str in invocation_ids:
534
+ if "." in id_str: # job_id
535
+ job_data = db.get_job(id_str)
536
+ if job_data:
537
+ inv_id = job_data.invocation_id
538
+ if inv_id not in grouped_jobs:
539
+ grouped_jobs[inv_id] = {}
540
+ grouped_jobs[inv_id][id_str] = job_data
541
+ all_jobs_for_consolidated[id_str] = job_data
542
+ elif id_str.isdigit(): # pipeline_id
543
+ # Find job by pipeline_id and add to group
544
+ for job_id, job_data in db._jobs.items():
545
+ if job_data.data.get("pipeline_id") == int(id_str):
546
+ inv_id = job_data.invocation_id
547
+ if inv_id not in grouped_jobs:
548
+ grouped_jobs[inv_id] = {}
549
+ grouped_jobs[inv_id][job_id] = job_data
550
+ all_jobs_for_consolidated[job_id] = job_data
551
+ break
552
+ else: # invocation_id
553
+ invocation_only.add(id_str)
554
+ # Add all jobs from this invocation for consolidated export
555
+ invocation_jobs = db.get_jobs(id_str)
556
+ all_jobs_for_consolidated.update(invocation_jobs)
557
+
558
+ # Check if we should use consolidated export (local + json/csv format)
559
+ should_consolidate = (
560
+ dest == "local"
561
+ and config
562
+ and config.get("format") in ["json", "csv"]
563
+ and (
564
+ len(invocation_only) > 1
565
+ or (len(invocation_only) == 1 and len(grouped_jobs) > 0)
566
+ )
567
+ )
568
+
569
+ if should_consolidate and hasattr(exporter, "export_multiple_invocations"):
570
+ # Use consolidated export for local exporter with JSON/CSV format
571
+ all_invocation_ids = list(invocation_only)
572
+ # Add invocations from grouped jobs
573
+ all_invocation_ids.extend(
574
+ set(
575
+ job_data.invocation_id
576
+ for jobs in grouped_jobs.values()
577
+ for job_data in jobs.values()
578
+ )
579
+ )
580
+ all_invocation_ids = list(set(all_invocation_ids)) # remove duplicates
581
+
582
+ try:
583
+ consolidated_result = exporter.export_multiple_invocations(
584
+ all_invocation_ids
585
+ )
586
+ return consolidated_result # type: ignore[no-any-return]
587
+ except Exception as e:
588
+ return {
589
+ "success": False,
590
+ "error": f"Consolidated export failed: {str(e)}",
591
+ }
592
+
593
+ # Regular multi-invocation export
594
+ all_results = {}
595
+ overall_success = True
596
+
597
+ # Export grouped jobs (partial invocations)
598
+ for inv_id, jobs in grouped_jobs.items():
599
+ try:
600
+ # Create a custom partial invocation export
601
+ results = {}
602
+ for job_id, job_data in jobs.items():
603
+ job_result = exporter.export_job(job_data)
604
+ results[job_id] = {
605
+ "success": job_result.success,
606
+ "message": job_result.message,
607
+ "metadata": job_result.metadata or {},
608
+ }
609
+ if not job_result.success:
610
+ overall_success = False
611
+
612
+ all_results[inv_id] = {
613
+ "success": all(r["success"] for r in results.values()),
614
+ "invocation_id": inv_id,
615
+ "jobs": results,
616
+ "partial": True, # indicate this was partial invocation
617
+ }
618
+ except Exception as e:
619
+ all_results[inv_id] = {
620
+ "success": False,
621
+ "error": f"Partial invocation export failed: {str(e)}",
622
+ }
623
+ overall_success = False
624
+
625
+ # Export full invocations
626
+ for inv_id in invocation_only:
627
+ result = exporter.export_invocation(inv_id)
628
+ # Ensure metadata is present in job results to prevent KeyError
629
+ if "jobs" in result:
630
+ for job_id, job_result in result["jobs"].items():
631
+ if "metadata" not in job_result:
632
+ job_result["metadata"] = {}
633
+ all_results[inv_id] = result
634
+ if not result.get("success", False):
635
+ overall_success = False
636
+
637
+ return {
638
+ "success": overall_success,
639
+ "invocations": all_results,
640
+ "metadata": {
641
+ "total_invocations": len(all_results),
642
+ "successful_invocations": sum(
643
+ 1 for r in all_results.values() if r.get("success")
644
+ ),
645
+ "mixed_export": len(grouped_jobs)
646
+ > 0, # indicates mixed job/invocation export
647
+ },
648
+ }
649
+
650
+ except Exception as e:
651
+ return {"success": False, "error": f"Export failed: {str(e)}"}
652
+
653
+
654
+ def _check_api_endpoint_when_deployment_is_configured(cfg: RunConfig) -> None:
655
+ """Check API endpoint configuration when deployment is configured.
656
+
657
+ Args:
658
+ cfg: Configuration object.
659
+
660
+ Raises:
661
+ ValueError: If invalid configuration is detected.
662
+ """
663
+ if cfg.deployment.type == "none":
664
+ return
665
+ if "target" not in cfg or not isinstance(cfg.target, DictConfig):
666
+ return
667
+ if "api_endpoint" not in cfg.target or not isinstance(
668
+ cfg.target.api_endpoint, DictConfig
669
+ ):
670
+ return
671
+ if "url" in cfg.target.api_endpoint:
672
+ raise ValueError(
673
+ "when deployment is configured, url field should not exist in target.api_endpoint"
674
+ )
675
+ if "model_id" in cfg.target.api_endpoint:
676
+ raise ValueError(
677
+ "when deployment is configured, model_id field should not exist in target.api_endpoint"
678
+ )