Skip to content

Restore HEAD validation before model downloads to detect 401 errors early#50

Merged
suzukimain merged 9 commits intoupdate-v9from
copilot/sub-pr-49
Dec 16, 2025
Merged

Restore HEAD validation before model downloads to detect 401 errors early#50
suzukimain merged 9 commits intoupdate-v9from
copilot/sub-pr-49

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 16, 2025

The previous implementation removed HEAD request validation before downloading models from Hugging Face, resulting in expensive failed downloads instead of lightweight pre-flight checks, particularly for 401 Unauthorized errors.

Changes

  • Added validate_url_with_head() function - Performs HEAD requests before downloads, raises HTTPError for 4xx/5xx responses to enable candidate retry
  • Refactored file_downloader() - Uses shared validation function to eliminate duplication
  • Validates URLs before both download paths:
    • DiffusionPipeline.download() - checks model_index.json accessibility
    • hf_hub_download() - checks file URL accessibility
  • Uses hf_hub_url() - Ensures consistent URL construction and respects revision parameter
  • Case-insensitive header handling - Prevents duplicate Authorization headers across different casings

Implementation

def validate_url_with_head(
    url: str, 
    token: Optional[str] = None, 
    headers: Optional[Dict] = None,
    timeout: int = 2
) -> None:
    """Validates URL accessibility via HEAD request. Raises HTTPError for 4xx/5xx."""
    if headers is None:
        headers = {}
    else:
        headers = dict(headers)  # Avoid mutating caller's dict
    
    if token and not any(k.lower() == "authorization" for k in headers):
        headers["Authorization"] = f"Bearer {token}"
    
    try:
        response = requests.head(url, headers=headers, allow_redirects=True, timeout=timeout)
        response.raise_for_status()  # Raises for 401 and other errors
    except requests.HTTPError:
        raise
    except requests.exceptions.RequestException as e:
        logger.info(f"HEAD check failed (continuing to download): {url} -> {e}")

Non-HTTPError request failures (timeouts, network errors) are logged but don't block downloads, as the full download may still succeed.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@suzukimain suzukimain marked this pull request as ready for review December 16, 2025 09:25
Copilot AI review requested due to automatic review settings December 16, 2025 09:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI and others added 7 commits December 16, 2025 09:28
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Co-authored-by: suzukimain <131413573+suzukimain@users.noreply.github.com>
Copilot AI changed the title [WIP] Update model loading enhancements and .gitignore updates Restore HEAD validation before model downloads to detect 401 errors early Dec 16, 2025
Copilot AI requested a review from suzukimain December 16, 2025 09:41
@suzukimain suzukimain merged commit b88f3af into update-v9 Dec 16, 2025
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.

3 participants