Skip to content

fix(diarization): use_auth_token->token shim for pyannote on HF Hub 1.x (#167) - #168

Merged
debpalash merged 2 commits into
mainfrom
fix/pyannote-use-auth-token
May 30, 2026
Merged

fix(diarization): use_auth_token->token shim for pyannote on HF Hub 1.x (#167)#168
debpalash merged 2 commits into
mainfrom
fix/pyannote-use-auth-token

Conversation

@debpalash

@debpalash debpalash commented May 30, 2026

Copy link
Copy Markdown
Owner

Fixes #167. pyannote-audio 3.x (core/pipeline.py:102) calls hf_hub_download(..., use_auth_token=...); huggingface_hub 1.x removed that kwarg (only token), so diarization died with unexpected keyword argument 'use_auth_token'. We can't drop to HF Hub <1 (transformers≥5.3 + our token persistence need 1.x) nor jump to pyannote 4.x (whisperx 3.4.2 still passes use_auth_token to pyannote's Inference — already noted in pyproject). So: a small shim wraps hf_hub_download/snapshot_download to translate use_auth_tokentoken, applied before pyannote's from huggingface_hub import … binds it. Tested with the installed pyannote 3.4.0 + HF Hub 1.7.2 — incl. a real check that pyannote's bound reference is the shim. 3 tests.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Prevented diarization pipeline failures by adding a compatibility shim that makes older/newer Hugging Face Hub parameter names work together.
  • Tests

    • Added tests verifying the compatibility shim translates legacy parameters to the newer form, is idempotent, and binds correctly when related modules are present.

Review Change Stack

….x (#167)

pyannote-audio 3.x (pipeline.py:102) calls hf_hub_download(use_auth_token=...),
which huggingface_hub 1.x removed (only 'token' now) -> 'unexpected keyword
argument use_auth_token', breaking speaker diarization. Wrap hf_hub_download/
snapshot_download to translate the dead kwarg, applied before pyannote's
'from huggingface_hub import hf_hub_download' binds it (+ patch already-loaded
pyannote modules). Verified the real pyannote reference binds the shim.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4c50f382-577b-4e85-b768-2f40bb2852dd

📥 Commits

Reviewing files that changed from the base of the PR and between 7617cd3 and 3bba2dd.

📒 Files selected for processing (1)
  • tests/test_pyannote_hf_compat.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_pyannote_hf_compat.py

📝 Walkthrough

Walkthrough

Adds an idempotent compatibility shim that wraps huggingface_hub.hf_hub_download/snapshot_download to translate use_auth_tokentoken, installs the shim before loading the pyannote diarization pipeline, and adds tests verifying translation, idempotence, and pyannote binding.

Changes

Pyannote Hugging Face Hub Compatibility for Pyannote

Layer / File(s) Summary
Compatibility shim function
backend/services/model_manager.py
Implements _ensure_pyannote_hf_token_compat() to wrap huggingface_hub.hf_hub_download and snapshot_download, translate use_auth_tokentoken, patch already-imported pyannote.* modules, and prevent re-wrapping via a marker.
Pipeline integration and compatibility verification
backend/services/model_manager.py, tests/test_pyannote_hf_compat.py
Calls the shim at the start of the diarization pipeline load; adds tests that assert the shim forwards use_auth_token as token, is idempotent, and is applied to pyannote.audio.core.pipeline when present.
sequenceDiagram
  participant Caller as get_diarization_pipeline
  participant Shim as _ensure_pyannote_hf_token_compat
  participant HF as huggingface_hub.hf_hub_download
  participant Pyannote as pyannote.audio.core.pipeline

  Caller->>Shim: install shim before importing Pipeline
  Shim->>HF: wrap hf_hub_download/snapshot_download (translate kwargs)
  Shim->>Pyannote: patch already-imported bindings to use shim
  Caller->>Pyannote: import/bind Pipeline (uses shim-wrapped HF functions)
Loading

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a compatibility shim to translate use_auth_token to token for pyannote on HuggingFace Hub 1.x.
Description check ✅ Passed The description covers the problem, solution, and testing approach comprehensively, but lacks coverage of the template's Type, Checklist, and Screenshots sections.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pyannote-use-auth-token

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread tests/test_pyannote_hf_compat.py Fixed
…d-local)

CodeQL doesn't model pytest.skip() as no-return, so it flagged _pp as a
possibly-uninitialized local (py/uninitialized-local-variable, error). Switch
to pytest.importorskip — cleaner and CodeQL-clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a targeted monkey-patch shim that wraps huggingface_hub.hf_hub_download and snapshot_download to translate the removed use_auth_token kwarg into token, resolving the TypeError that broke diarization when pyannote-audio 3.x (which still emits use_auth_token internally) is paired with huggingface_hub 1.x (which no longer accepts it).

  • _ensure_pyannote_hf_token_compat() is injected in get_diarization_pipeline immediately before pyannote is imported, so pyannote's module-level from huggingface_hub import hf_hub_download binds the wrapped version; a secondary loop also back-patches any already-imported pyannote submodules in sys.modules.
  • Three tests verify kwarg translation, idempotency, and (conditionally, when pyannote is installed) that pyannote's own module reference points to the shim.
  • The setdefault translation has a subtle silent-drop edge case when token=None is already present in kwargs, and the outer Pipeline.from_pretrained call on line 587 still uses the deprecated use_auth_token= spelling.

Confidence Score: 4/5

Safe to merge — the shim correctly patches hf_hub_download before pyannote imports it, and the fix is idempotent and well-scoped to the diarization load path.

The setdefault line has a silent-failure edge case (token=None wins over a valid use_auth_token) with no test coverage, and the outer Pipeline.from_pretrained call retains the deprecated kwarg spelling inconsistently.

The translation logic in backend/services/model_manager.py (the setdefault line) and the corresponding missing test case in tests/test_pyannote_hf_compat.py deserve a second look.

Important Files Changed

Filename Overview
backend/services/model_manager.py Adds _ensure_pyannote_hf_token_compat() shim that wraps hf_hub_download/snapshot_download to translate use_auth_token to token; called in get_diarization_pipeline before pyannote import. The setdefault translation has a silent-drop edge case if token=None is passed explicitly alongside use_auth_token.
tests/test_pyannote_hf_compat.py Three tests covering kwarg translation, idempotency, and a live pyannote-binding check. test_pyannote_binds_the_shim permanently patches the real hf_hub_download without monkeypatch cleanup, and the token=None+use_auth_token=X edge case is not covered.

Sequence Diagram

sequenceDiagram
    participant App as get_diarization_pipeline()
    participant Shim as _ensure_pyannote_hf_token_compat()
    participant HF as huggingface_hub module
    participant PyA as pyannote.audio.Pipeline
    participant DL as hf_hub_download (real)

    App->>Shim: call (before pyannote import)
    Shim->>HF: setattr(hf_hub_download, wrapped_fn)
    Note over HF: wrapped_fn translates use_auth_token to token

    App->>PyA: from pyannote.audio import Pipeline
    Note over PyA: binds wrapped hf_hub_download
    App->>PyA: "Pipeline.from_pretrained(..., use_auth_token=token)"
    PyA->>HF: "hf_hub_download(..., use_auth_token=token)"
    Note over HF: shim intercepts and translates kwarg
    HF->>DL: "hf_hub_download(..., token=token)"
    DL-->>App: model downloaded
Loading

Fix All in Claude Code

Reviews (1): Last reviewed commit: "test(diarization): use pytest.importorsk..." | Re-trigger Greptile

Comment on lines +540 to +541
if "use_auth_token" in kwargs:
kwargs.setdefault("token", kwargs.pop("use_auth_token"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 The setdefault call silently discards use_auth_token when token=None is already present in kwargs. If any caller (or a future pyannote minor that starts passing token=None as an explicit default) issues hf_hub_download(..., token=None, use_auth_token=real_token), the real token is popped and thrown away — the wrapped function receives token=None and authentication fails with no error pointing at the shim.

Suggested change
if "use_auth_token" in kwargs:
kwargs.setdefault("token", kwargs.pop("use_auth_token"))
if "use_auth_token" in kwargs:
uat = kwargs.pop("use_auth_token")
if not kwargs.get("token"):
kwargs["token"] = uat

Fix in Claude Code

_ensure_pyannote_hf_token_compat() # #167: use_auth_token -> token
from pyannote.audio import Pipeline
logger.info("Loading Pyannote Diarization Pipeline...")
_diar_pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", use_auth_token=hf_token)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 The outer Pipeline.from_pretrained call still passes the deprecated use_auth_token kwarg. pyannote 3.x still accepts it, so this doesn't break today, but it's inconsistent with the fix this PR applies — and if a pyannote upgrade is ever attempted, the outer call will be the first place to notice. Switching to token= here is a one-line change and removes the last use_auth_token site from the call path.

Suggested change
_diar_pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", use_auth_token=hf_token)
_diar_pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", token=hf_token)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code

Comment on lines +50 to +61
def test_pyannote_binds_the_shim():
"""The real proof: after the shim, pyannote's own `hf_hub_download`
reference translates `use_auth_token` rather than raising."""
_ensure_pyannote_hf_token_compat()
# importorskip imports the module (or skips) — and since the shim patched
# huggingface_hub first, pyannote's `from huggingface_hub import
# hf_hub_download` (pipeline.py:34) binds the wrapped fn. (Also avoids the
# CodeQL "possibly-uninitialized local" false positive from a try/skip.)
_pp = pytest.importorskip("pyannote.audio.core.pipeline")
assert getattr(_pp.hf_hub_download, "_ov_uat_shim", False), (
"pyannote.audio.core.pipeline.hf_hub_download is not the use_auth_token shim"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 test_pyannote_binds_the_shim calls _ensure_pyannote_hf_token_compat() against the real huggingface_hub without a monkeypatch fixture, so the production hf_hub_download reference is permanently wrapped for the remainder of the test session. The shim is transparent and idempotent so this is benign today, but any later test that explicitly checks huggingface_hub.hf_hub_download is the unwrapped original would spuriously fail. Wrapping the call in a monkeypatch (or simply accepting the permanent patch as intentional with a comment) would make the side-effect explicit.

Fix in Claude Code

@debpalash
debpalash merged commit c993e14 into main May 30, 2026
15 checks passed
@debpalash
debpalash deleted the fix/pyannote-use-auth-token branch June 12, 2026 10:10
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.

[Bug] [2026-05-30][10:49:31] unexpected keyword argument 'use_auth_token'

2 participants