Commit b7ed360
[Spaces] Add fetch_space_logs + hf spaces logs command (#4091)
* [Spaces] Add fetch_space_logs + hf spaces logs command
Agents and scripts currently have no way to read Space build/run logs
programmatically — the endpoint is only reachable via raw curl. This
adds a public API to close that gap.
- HfApi.fetch_space_logs(repo_id, *, build=False, follow=False) yields
log lines as Iterable[str]. build=True switches to container build
logs; default is the running app's stdout/stderr.
- `hf spaces logs <repo_id> [--build] [-f] [-n N]` mirrors the Python
API at the CLI level, with 404/403 mapped to clean CLIError messages.
The helper trusts the "stream close = done" server contract (confirmed
against moon-landing's SpaceLogs.svelte onClose handler) and does not
poll SpaceStage; read timeout + bounded retries handle the
misbehaving-upstream case. Structure mirrors _fetch_running_job_sse but
without the status-check backstop. Tests use the mock-based pattern
from hf jobs logs (no new VCR cassettes).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Update generated CLI reference for hf spaces logs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Fix: catch HfHubHTTPError instead of httpx.HTTPStatusError
hf_raise_for_status() raises HfHubHTTPError (inherits HTTPError), not
httpx.HTTPStatusError. The previous handler was dead code, causing
404/403 errors to fall through to the retry loop instead of raising
immediately. Spotted by cursor bugbot on PR review.
Note: the same bug exists in _fetch_running_job_sse — not fixed here
to keep the diff focused, but worth a follow-up.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Update docs/source/en/guides/manage-spaces.md
Co-authored-by: Lucain <lucainp@gmail.com>
* Refactor: share SSE streaming loop between Spaces and Jobs
Extracts `HfApi._stream_sse_events` to unify the retry/backoff/dedup
loop previously duplicated across `_fetch_space_logs_sse` and
`_fetch_running_job_sse`. Addresses Wauplin and Cursor Bugbot review
comments on PR #4091.
Also fixes a dead `except httpx.HTTPStatusError` handler that affected
both Spaces and Jobs: `hf_raise_for_status` raises `HfHubHTTPError`
(subclass of `httpx.HTTPError`, not `HTTPStatusError`), so 404/403 in
follow mode used to fall through to the broad retry arm and stall for
~25s. The new helper catches `HfHubHTTPError` before the broad arm, so
permanent errors fail fast. Live-verified: `hf spaces logs missing/x -f`
now errors in <1s instead of ~25s.
CLI cleanups on `hf spaces logs` per Wauplin:
- Switch from `print()` to `out.text(line.strip())` (new mode-aware
printer from #3979).
- Drop the redundant local `HfHubHTTPError` block — it's already
handled by the global CLI error mapper.
Also tightens `_fetch_running_job_sse` typing by splitting the legacy
`double_check_job_has_finished_on_status_code_or_error` mixed tuple
into `tolerated_status_codes: tuple[int, ...]` and
`tolerated_exception_types: tuple[type[Exception], ...]`, eliminating a
runtime type-discrimination step.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Drop dead `httpx.ReadTimeout` entry from fetch_job_metrics
The entry in `tolerated_exception_types` was never consulted: the SSE
helper's `is_no_new_line_timeout` check short-circuits the tolerated
tuple lookup for any ReadTimeout, so the tuple entry was dead code
(pre-existing on `main` before #4091, preserved faithfully through the
refactor). ReadTimeout tolerance continues to work via the
`is_no_new_line_timeout` path.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Update src/huggingface_hub/cli/spaces.py
Co-authored-by: Lucain <lucainp@gmail.com>
* Update tests/test_cli.py
Co-authored-by: Lucain <lucainp@gmail.com>
* Update tests/test_cli.py
Co-authored-by: Lucain <lucainp@gmail.com>
* Address review feedback on fetch_space_logs
- Promote "Debug a failing Space" heading to ### (matches PR #4108 style)
- Add hint when run logs are empty, suggesting --build as alternative
- Add tests covering the empty-logs hint for both run and build modes
- Regenerate CLI reference docs to include hf spaces logs command
* Update tests/test_cli.py
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Lucain <lucainp@gmail.com>1 parent 6cace2b commit b7ed360
7 files changed
Lines changed: 358 additions & 60 deletions
File tree
- docs/source/en
- guides
- package_reference
- src/huggingface_hub
- cli
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 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 | + | |
198 | 225 | | |
199 | 226 | | |
200 | 227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3403 | 3403 | | |
3404 | 3404 | | |
3405 | 3405 | | |
| 3406 | + | |
3406 | 3407 | | |
3407 | 3408 | | |
3408 | 3409 | | |
| |||
3548 | 3549 | | |
3549 | 3550 | | |
3550 | 3551 | | |
| 3552 | + | |
| 3553 | + | |
| 3554 | + | |
| 3555 | + | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
| 3560 | + | |
| 3561 | + | |
| 3562 | + | |
| 3563 | + | |
| 3564 | + | |
| 3565 | + | |
| 3566 | + | |
| 3567 | + | |
| 3568 | + | |
| 3569 | + | |
| 3570 | + | |
| 3571 | + | |
| 3572 | + | |
| 3573 | + | |
| 3574 | + | |
| 3575 | + | |
| 3576 | + | |
| 3577 | + | |
| 3578 | + | |
| 3579 | + | |
| 3580 | + | |
| 3581 | + | |
| 3582 | + | |
| 3583 | + | |
| 3584 | + | |
| 3585 | + | |
| 3586 | + | |
| 3587 | + | |
| 3588 | + | |
| 3589 | + | |
3551 | 3590 | | |
3552 | 3591 | | |
3553 | 3592 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| 248 | + | |
248 | 249 | | |
249 | 250 | | |
250 | 251 | | |
| |||
954 | 955 | | |
955 | 956 | | |
956 | 957 | | |
| 958 | + | |
957 | 959 | | |
958 | 960 | | |
959 | 961 | | |
| |||
1379 | 1381 | | |
1380 | 1382 | | |
1381 | 1383 | | |
| 1384 | + | |
1382 | 1385 | | |
1383 | 1386 | | |
1384 | 1387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
264 | 265 | | |
265 | 266 | | |
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 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
267 | 330 | | |
268 | 331 | | |
269 | 332 | | |
| |||
0 commit comments