99from concurrent .futures import ThreadPoolExecutor , as_completed
1010from datetime import datetime
1111from pathlib import Path
12+ from collections .abc import Iterable
1213from typing import Any , cast
1314
1415import httpx
@@ -1346,14 +1347,20 @@ def watch_task(
13461347 api_url : str ,
13471348 task_id : str ,
13481349 experiment_id : str | None = None ,
1350+ trial_ids : Iterable [str ] | None = None ,
13491351) -> dict | None :
13501352 """Watch a task until completion. Returns the final result.
13511353
13521354 When *experiment_id* is given, only trials belonging to that experiment
13531355 are displayed (others are hidden from the table and summary counts).
1356+
1357+ When *trial_ids* is given, only trials whose ``id`` is in that set are
1358+ shown. This is useful when appending trials to an existing task and the
1359+ caller only wants to monitor the freshly-submitted trials.
13541360 """
13551361 final_result = None
13561362 headers = get_auth_headers ()
1363+ trial_id_filter = set (trial_ids ) if trial_ids is not None else None
13571364 with Live (console = console , refresh_per_second = 2 ) as live :
13581365 while True :
13591366 try :
@@ -1368,7 +1375,11 @@ def watch_task(
13681375 final_result = result
13691376
13701377 all_trials = result .get ("trials" , [])
1371- if experiment_id :
1378+ if trial_id_filter is not None :
1379+ all_trials = [
1380+ t for t in all_trials if t .get ("id" ) in trial_id_filter
1381+ ]
1382+ elif experiment_id :
13721383 all_trials = [
13731384 t for t in all_trials if t .get ("experiment_id" ) == experiment_id
13741385 ]
@@ -1450,7 +1461,7 @@ def watch_task(
14501461 live .update (table )
14511462
14521463 # Check if done
1453- if experiment_id :
1464+ if trial_id_filter is not None or experiment_id :
14541465 terminal = {"success" , "failed" , "cancelled" }
14551466 if all_trials and all (
14561467 t .get ("status" ) in terminal for t in all_trials
0 commit comments