Skip to content

Fix SpaceRuntime parsing after volumes mutation API calls#4061

Open
abidlabs wants to merge 5 commits intomainfrom
fix-space-volumes-runtime-response
Open

Fix SpaceRuntime parsing after volumes mutation API calls#4061
abidlabs wants to merge 5 commits intomainfrom
fix-space-volumes-runtime-response

Conversation

@abidlabs
Copy link
Copy Markdown
Member

@abidlabs abidlabs commented Apr 7, 2026

Not sure if this is a problem with Hub server or library, but basically I've noticed that PUT / DELETE /api/spaces/{repo_id}/volumes can return a JSON body that is not a full Space runtime document (e.g. only volume-related fields, no top-level stage). The client always did return SpaceRuntime(r.json()), which raises when stage is missing:

KeyError: 'stage'

Example of what goes wrong with the previous behavior:

from huggingface_hub import HfApi, Volume

api = HfApi()
# After a successful HTTP 200, the Hub may return e.g. {"volumes": [...]} without "stage".
# SpaceRuntime(data) expects data["stage"] → KeyError.
api.set_space_volumes(
    "org/space",
    [Volume(type="bucket", source="org/bucket", mount_path="/data")],
)

This PR checks more resiliently if the response is empty or does not contain "stage", in which case it falls back to get_space_runtime(repo_id) so callers still get a valid SpaceRuntime.

Noticed while testing the latest version of huggingface_hub (1.9.1) with trackio


Note

Low Risk
Low risk: change is localized to Space volume mutation return handling and adds a unit test; main behavioral difference is an extra get_space_runtime call when the Hub response is empty or not parseable as a full SpaceRuntime.

Overview
Fixes set_space_volumes and delete_space_volumes to be resilient to Hub responses that don’t include a full SpaceRuntime payload. A new helper (_space_runtime_after_mutation) now attempts to parse the response when possible, but falls back to get_space_runtime when the body is empty or missing required fields.

Adds a regression test ensuring that a non-runtime JSON response (e.g. only {"volumes": [...]}) triggers the fallback fetch instead of raising during SpaceRuntime construction.

Reviewed by Cursor Bugbot for commit 15ce06a. Bugbot is set up for automated code reviews on this repo. Configure here.

abidlabs added 4 commits April 7, 2026 09:44
PUT/DELETE /spaces/.../volumes may return JSON without a top-level stage.
HfApi.set_space_volumes and delete_space_volumes now fall back to
get_space_runtime when the body is empty or not a full runtime payload.

Made-with: Cursor
@abidlabs abidlabs marked this pull request as ready for review April 7, 2026 16:52
@bot-ci-comment
Copy link
Copy Markdown

bot-ci-comment bot commented Apr 7, 2026

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@davanstrien
Copy link
Copy Markdown
Member

Thanks for the fix! IIUC the response parsing issue has been there since the volumes API was first added in #4018, but it wasn't reachable until #4054 fixed the request payload format.

@abidlabs
Copy link
Copy Markdown
Member Author

abidlabs commented Apr 7, 2026

yup @davanstrien exactly, thanks for that PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants