Skip to content

Commit 4d755d2

Browse files
hanouticelinaKludexburtenshaw
authored
Add Hugging Face as a provider (#1911)
Co-authored-by: Marcelo Trylesinski <[email protected]> Co-authored-by: burtenshaw <[email protected]>
1 parent f9f1c03 commit 4d755d2

28 files changed

+3583
-20
lines changed

docs/api/models/huggingface.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `pydantic_ai.models.huggingface`
2+
3+
## Setup
4+
5+
For details on how to set up authentication with this model, see [model configuration for Hugging Face](../../models/huggingface.md).
6+
7+
::: pydantic_ai.models.huggingface

docs/api/providers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@
3131
::: pydantic_ai.providers.github.GitHubProvider
3232

3333
::: pydantic_ai.providers.openrouter.OpenRouterProvider
34+
35+
::: pydantic_ai.providers.huggingface.HuggingFaceProvider

docs/models/huggingface.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Hugging Face
2+
3+
[Hugging Face](https://huggingface.co/) is an AI platform with all major open source models, datasets, MCPs, and demos. You can use [Inference Providers](https://huggingface.co/docs/inference-providers) to run open source models like DeepSeek R1 on scalable serverless infrastructure.
4+
5+
## Install
6+
7+
To use `HuggingFaceModel`, you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `huggingface` optional group:
8+
9+
```bash
10+
pip/uv-add "pydantic-ai-slim[huggingface]"
11+
```
12+
13+
## Configuration
14+
15+
To use [Hugging Face](https://huggingface.co/) inference, you'll need to set up an account which will give you [free tier](https://huggingface.co/docs/inference-providers/pricing) allowance on [Inference Providers](https://huggingface.co/docs/inference-providers). To setup inference, follow these steps:
16+
17+
1. Go to [Hugging Face](https://huggingface.co/join) and sign up for an account.
18+
2. Create a new access token in [Hugging Face](https://huggingface.co/settings/tokens).
19+
3. Set the `HF_TOKEN` environment variable to the token you just created.
20+
21+
Once you have a Hugging Face access token, you can set it as an environment variable:
22+
23+
```bash
24+
export HF_TOKEN='hf_token'
25+
```
26+
27+
## Usage
28+
29+
You can then use [`HuggingFaceModel`][pydantic_ai.models.huggingface.HuggingFaceModel] by name:
30+
31+
```python
32+
from pydantic_ai import Agent
33+
34+
agent = Agent('huggingface:Qwen/Qwen3-235B-A22B')
35+
...
36+
```
37+
38+
Or initialise the model directly with just the model name:
39+
40+
```python
41+
from pydantic_ai import Agent
42+
from pydantic_ai.models.huggingface import HuggingFaceModel
43+
44+
model = HuggingFaceModel('Qwen/Qwen3-235B-A22B')
45+
agent = Agent(model)
46+
...
47+
```
48+
49+
By default, the [`HuggingFaceModel`][pydantic_ai.models.huggingface.HuggingFaceModel] uses the
50+
[`HuggingFaceProvider`][pydantic_ai.providers.huggingface.HuggingFaceProvider] that will select automatically
51+
the first of the inference providers (Cerebras, Together AI, Cohere..etc) available for the model, sorted by your
52+
preferred order in https://hf.co/settings/inference-providers.
53+
54+
## Configure the provider
55+
56+
If you want to pass parameters in code to the provider, you can programmatically instantiate the
57+
[`HuggingFaceProvider`][pydantic_ai.providers.huggingface.HuggingFaceProvider] and pass it to the model:
58+
59+
```python
60+
from pydantic_ai import Agent
61+
from pydantic_ai.models.huggingface import HuggingFaceModel
62+
from pydantic_ai.providers.huggingface import HuggingFaceProvider
63+
64+
model = HuggingFaceModel('Qwen/Qwen3-235B-A22B', provider=HuggingFaceProvider(api_key='hf_token', provider_name='nebius'))
65+
agent = Agent(model)
66+
...
67+
```
68+
69+
## Custom Hugging Face client
70+
71+
[`HuggingFaceProvider`][pydantic_ai.providers.huggingface.HuggingFaceProvider] also accepts a custom
72+
[`AsyncInferenceClient`][huggingface_hub.AsyncInferenceClient] client via the `hf_client` parameter, so you can customise
73+
the `headers`, `bill_to` (billing to an HF organization you're a member of), `base_url` etc. as defined in the
74+
[Hugging Face Hub python library docs](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client).
75+
76+
```python
77+
from huggingface_hub import AsyncInferenceClient
78+
79+
from pydantic_ai import Agent
80+
from pydantic_ai.models.huggingface import HuggingFaceModel
81+
from pydantic_ai.providers.huggingface import HuggingFaceProvider
82+
83+
client = AsyncInferenceClient(
84+
bill_to='openai',
85+
api_key='hf_token',
86+
provider='fireworks-ai',
87+
)
88+
89+
model = HuggingFaceModel(
90+
'Qwen/Qwen3-235B-A22B',
91+
provider=HuggingFaceProvider(hf_client=client),
92+
)
93+
agent = Agent(model)
94+
...
95+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ nav:
8383
- api/models/gemini.md
8484
- api/models/google.md
8585
- api/models/groq.md
86+
- api/models/huggingface.md
8687
- api/models/instrumented.md
8788
- api/models/mistral.md
8889
- api/models/test.md

pydantic_ai_slim/pydantic_ai/models/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@
227227
'heroku:claude-3-7-sonnet',
228228
'heroku:claude-4-sonnet',
229229
'heroku:claude-3-haiku',
230+
'huggingface:Qwen/QwQ-32B',
231+
'huggingface:Qwen/Qwen2.5-72B-Instruct',
232+
'huggingface:Qwen/Qwen3-235B-A22B',
233+
'huggingface:Qwen/Qwen3-32B',
234+
'huggingface:deepseek-ai/DeepSeek-R1',
235+
'huggingface:meta-llama/Llama-3.3-70B-Instruct',
236+
'huggingface:meta-llama/Llama-4-Maverick-17B-128E-Instruct',
237+
'huggingface:meta-llama/Llama-4-Scout-17B-16E-Instruct',
230238
'mistral:codestral-latest',
231239
'mistral:mistral-large-latest',
232240
'mistral:mistral-moderation-latest',
@@ -560,7 +568,7 @@ def override_allow_model_requests(allow_model_requests: bool) -> Iterator[None]:
560568
ALLOW_MODEL_REQUESTS = old_value # pyright: ignore[reportConstantRedefinition]
561569

562570

563-
def infer_model(model: Model | KnownModelName | str) -> Model:
571+
def infer_model(model: Model | KnownModelName | str) -> Model: # noqa: C901
564572
"""Infer the model from the name."""
565573
if isinstance(model, Model):
566574
return model
@@ -624,6 +632,10 @@ def infer_model(model: Model | KnownModelName | str) -> Model:
624632
from .bedrock import BedrockConverseModel
625633

626634
return BedrockConverseModel(model_name, provider=provider)
635+
elif provider == 'huggingface':
636+
from .huggingface import HuggingFaceModel
637+
638+
return HuggingFaceModel(model_name, provider=provider)
627639
else:
628640
raise UserError(f'Unknown model: {model}') # pragma: no cover
629641

0 commit comments

Comments
 (0)