Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Move follow parameter into _fetch_running_job_sse
Instead of a separate `_fetch_job_logs_no_follow` helper, integrate the
follow/no-follow behavior directly into `_fetch_running_job_sse`. When
`follow=False`, disables retries and breaks on timeout after collecting
buffered events.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
  • Loading branch information
davanstrien and claude committed Feb 9, 2026
commit 89f6e1e32fd2b121c1a224932f4e2845ed87a454
66 changes: 13 additions & 53 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10263,14 +10263,15 @@ def _fetch_running_job_sse(
timeout: int,
skip_previous_events_on_retry: bool,
double_check_job_has_finished_on_status_code_or_error: tuple[Union[int, Type[Exception]], ...],
follow: bool = True,
namespace: Optional[str] = None,
token: Union[bool, str, None] = None,
) -> Iterable[dict[str, Any]]:
if namespace is None:
namespace = self.whoami(token=token)["name"]
# We don't use http_backoff since we need to check ourselves if the job is still running
nb_tries = 0
max_retries = 5
max_retries = 5 if follow else 0
min_wait_time = 1
max_wait_time = 10
sleep_time = 0
Expand Down Expand Up @@ -10315,7 +10316,9 @@ def _fetch_running_job_sse(
and isinstance(getattr(err.__context__, "__cause__", None), TimeoutError)
)
if is_no_new_line_timeout:
# job is likely finished
if not follow:
break # no-follow mode: got all buffered events
# follow mode: job is likely finished
pass
elif type(err) in double_check_job_has_finished_on_status_code_or_error:
pass
Expand Down Expand Up @@ -10353,9 +10356,8 @@ def fetch_job_logs(
The namespace where the Job is running. Defaults to the current user's namespace.

follow (`bool`, *optional*):
If `True` (default), stream logs in real-time until the job completes (blocking).
If `False`, fetch only the currently available logs and return immediately (non-blocking).
Use `follow=False` when calling from scripts, agents, or CI pipelines that should not block.
If `True`, stream logs in real-time until the job completes (blocking).
If `False` (default), fetch only the currently available logs and return immediately (non-blocking).

token `(Union[bool, str, None]`, *optional*):
A valid user access token. If not provided, the locally saved token will be used, which is the
Expand All @@ -10376,10 +10378,6 @@ def fetch_job_logs(
... print(log)
```
"""
if not follow:
yield from self._fetch_job_logs_no_follow(job_id=job_id, namespace=namespace, token=token)
return

# - We need to retry because sometimes the /logs doesn't return logs when the job just started.
# (for example it can return only two lines: one for "Job started" and one empty line)
# - Timeouts can happen in case of build errors
Expand All @@ -10389,12 +10387,17 @@ def fetch_job_logs(
# - there is a ": keep-alive" every 30 seconds

seconds_between_keep_alive = 30
# When not following, use a short timeout: the server replays historical logs
# quickly, then pauses waiting for new events (~30s keep-alive). 5 seconds is
# enough to receive all buffered logs.
timeout = 4 * seconds_between_keep_alive if follow else 5
for event in self._fetch_running_job_sse(
job_id=job_id,
route="logs",
timeout=4 * seconds_between_keep_alive,
timeout=timeout,
skip_previous_events_on_retry=True,
double_check_job_has_finished_on_status_code_or_error=tuple(),
follow=follow,
namespace=namespace,
token=token,
):
Expand All @@ -10403,49 +10406,6 @@ def fetch_job_logs(
log = event["data"]
yield log

def _fetch_job_logs_no_follow(
self,
*,
job_id: str,
namespace: Optional[str] = None,
token: Union[bool, str, None] = None,
) -> Iterable[str]:
"""Fetch only currently available logs and return immediately (non-blocking).

Connects to the SSE log stream with a short read timeout. The server replays
all historical log lines quickly, then pauses waiting for new events (with
keep-alive every ~30s). When the short timeout fires without new data, we
treat that as "all buffered logs received" and return gracefully.
"""
if namespace is None:
namespace = self.whoami(token=token)["name"]

# Short timeout: the server replays historical logs quickly, then pauses.
# 5 seconds is enough to receive all buffered logs.
read_timeout = 5
try:
with get_session().stream(
"GET",
f"{self.endpoint}/api/jobs/{namespace}/{job_id}/logs",
headers=self._build_hf_headers(token=token),
timeout=read_timeout,
) as response:
hf_raise_for_status(response)
for line in response.iter_lines():
if line and line.startswith("data: {"):
event = json.loads(line[len("data: ") :])
if not event["data"].startswith("===== Job started"):
yield event["data"]
except (httpx.ReadTimeout, httpcore.ReadTimeout):
# ReadTimeout means we received all buffered logs and the server is waiting
# for new events - this is the expected exit path for no-follow mode.
pass
except httpx.NetworkError as err:
# NetworkError can wrap a TimeoutError from the underlying socket.
is_timeout = err.__context__ and isinstance(getattr(err.__context__, "__cause__", None), TimeoutError)
if not is_timeout:
raise

def fetch_job_metrics(
self,
*,
Expand Down
Loading