Skip to content

Commit 55146ac

Browse files
committed
Document the use of the Outlines model
1 parent 7582e1e commit 55146ac

File tree

9 files changed

+137
-2
lines changed

9 files changed

+137
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ We built Pydantic AI with one simple aim: to bring that FastAPI feeling to GenAI
3939
[Pydantic Validation](https://docs.pydantic.dev/latest/) is the validation layer of the OpenAI SDK, the Google ADK, the Anthropic SDK, LangChain, LlamaIndex, AutoGPT, Transformers, CrewAI, Instructor and many more. _Why use the derivative when you can go straight to the source?_ :smiley:
4040

4141
2. **Model-agnostic**:
42-
Supports virtually every [model](https://ai.pydantic.dev/models/overview) and provider: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Cohere, Mistral, and Perplexity; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Ollama, LiteLLM, Groq, OpenRouter, Together AI, Fireworks AI, Cerebras, Hugging Face, GitHub, Heroku, Vercel. If your favorite model or provider is not listed, you can easily implement a [custom model](https://ai.pydantic.dev/models/overview#custom-models).
42+
Supports virtually every [model](https://ai.pydantic.dev/models/overview) and provider: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Cohere, Mistral, and Perplexity; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Ollama, LiteLLM, Groq, OpenRouter, Together AI, Fireworks AI, Cerebras, Hugging Face, GitHub, Heroku, Vercel, and Outlines. If your favorite model or provider is not listed, you can easily implement a [custom model](https://ai.pydantic.dev/models/overview#custom-models).
4343

4444
3. **Seamless Observability**:
4545
Tightly [integrates](https://ai.pydantic.dev/logfire) with [Pydantic Logfire](https://pydantic.dev/logfire), our general-purpose OpenTelemetry observability platform, for real-time debugging, evals-based performance monitoring, and behavior, tracing, and cost tracking. If you already have an observability platform that supports OTel, you can [use that too](https://ai.pydantic.dev/logfire#alternative-observability-backends).

a.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import outlines
2+
from llama_cpp import Llama
3+
4+
from pydantic_ai.models.outlines import OutlinesModel
5+
import os
6+
7+
8+
os.environ['OPENAI_API_KEY'] = 'testing'
9+
os.environ['GEMINI_API_KEY'] = 'testing'
10+
os.environ['GOOGLE_API_KEY'] = 'testing'
11+
os.environ['GROQ_API_KEY'] = 'testing'
12+
os.environ['CO_API_KEY'] = 'testing'
13+
os.environ['MISTRAL_API_KEY'] = 'testing'
14+
os.environ['ANTHROPIC_API_KEY'] = 'testing'
15+
os.environ['HF_TOKEN'] = 'hf_testing'
16+
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
17+
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
18+
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
19+
os.environ['VERCEL_AI_GATEWAY_API_KEY'] = 'testing'
20+
os.environ['CEREBRAS_API_KEY'] = 'testing'
21+
22+
23+
outlines_model = outlines.from_llamacpp(
24+
Llama.from_pretrained(
25+
repo_id='M4-ai/TinyMistral-248M-v2-Instruct-GGUF',
26+
filename='TinyMistral-248M-v2-Instruct.Q4_K_M.gguf',
27+
)
28+
)
29+
model = OutlinesModel(outlines_model)

docs/api/models/outlines.md

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

docs/builtin-tools.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ making it ideal for queries that require up-to-date data.
3535
| Mistral || Not supported |
3636
| Cohere || Not supported |
3737
| HuggingFace || Not supported |
38+
| Outlines || Not supported |
3839

3940
### Usage
4041

@@ -119,6 +120,7 @@ in a secure environment, making it perfect for computational tasks, data analysi
119120
| Mistral || |
120121
| Cohere || |
121122
| HuggingFace || |
123+
| Outlines || |
122124

123125
### Usage
124126

@@ -148,6 +150,7 @@ allowing it to pull up-to-date information from the web.
148150
| Mistral || |
149151
| Cohere || |
150152
| HuggingFace || |
153+
| Outlines || |
151154

152155
### Usage
153156

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ We built Pydantic AI with one simple aim: to bring that FastAPI feeling to GenAI
1414
[Pydantic Validation](https://docs.pydantic.dev/latest/) is the validation layer of the OpenAI SDK, the Google ADK, the Anthropic SDK, LangChain, LlamaIndex, AutoGPT, Transformers, CrewAI, Instructor and many more. _Why use the derivative when you can go straight to the source?_ :smiley:
1515

1616
2. **Model-agnostic**:
17-
Supports virtually every [model](models/overview.md) and provider: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Cohere, Mistral, and Perplexity; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Ollama, LiteLLM, Groq, OpenRouter, Together AI, Fireworks AI, Cerebras, Hugging Face, GitHub, Heroku, Vercel. If your favorite model or provider is not listed, you can easily implement a [custom model](models/overview.md#custom-models).
17+
Supports virtually every [model](models/overview.md) and provider: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Cohere, Mistral, Outlines and Perplexity; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Ollama, LiteLLM, Groq, OpenRouter, Together AI, Fireworks AI, Cerebras, Hugging Face, GitHub, Heroku, Vercel. If your favorite model or provider is not listed, you can easily implement a [custom model](models/overview.md#custom-models).
1818

1919
3. **Seamless Observability**:
2020
Tightly [integrates](logfire.md) with [Pydantic Logfire](https://pydantic.dev/logfire), our general-purpose OpenTelemetry observability platform, for real-time debugging, evals-based performance monitoring, and behavior, tracing, and cost tracking. If you already have an observability platform that supports OTel, you can [use that too](logfire.md#alternative-observability-backends).

docs/install.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ pip/uv-add "pydantic-ai-slim[openai]"
5151
* `cohere` - installs `cohere` [PyPI ↗](https://pypi.org/project/cohere){:target="_blank"}
5252
* `bedrock` - installs `boto3` [PyPI ↗](https://pypi.org/project/boto3){:target="_blank"}
5353
* `huggingface` - installs `huggingface-hub[inference]` [PyPI ↗](https://pypi.org/project/huggingface-hub){:target="_blank"}
54+
* `outlines` - installs `outlines` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
55+
* `outlines-transformers` - installs `outlines[transformers]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
56+
* `outlines-llamacpp` - installs `outlines[llamacpp]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
57+
* `outlines-mlxlm` - installs `outlines[mlxlm]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
58+
* `outlines-sglang` - installs `outlines[sglang]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
59+
* `outlines-vllm-offline` - installs `outlines[vllm-offline]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
5460
* `duckduckgo` - installs `ddgs` [PyPI ↗](https://pypi.org/project/ddgs){:target="_blank"}
5561
* `tavily` - installs `tavily-python` [PyPI ↗](https://pypi.org/project/tavily-python){:target="_blank"}
5662
* `cli` - installs `rich` [PyPI ↗](https://pypi.org/project/rich){:target="_blank"}, `prompt-toolkit` [PyPI ↗](https://pypi.org/project/prompt-toolkit){:target="_blank"}, and `argcomplete` [PyPI ↗](https://pypi.org/project/argcomplete){:target="_blank"}

docs/models/outlines.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Outlines
2+
3+
## Install
4+
5+
To use [`OutlinesModel`][pydantic_ai.models.OutlinesModel], you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `outlines` optional group:
6+
```bash
7+
pip/uv-add "pydantic-ai-slim[outlines]"
8+
```
9+
10+
As Outlines is a library allowing you to run model from various different providers, it does not include the necessary dependencies for any model, but instead requires you to install the optional group for the provider you want to use. To do so, you can install `pydantic-ai-slim` with an optional group composed of outlines, a dash, and the name of the model. For instance:
11+
12+
```bash
13+
pip/uv-add "pydantic-ai-slim[outlines-transformers]"
14+
```
15+
16+
Or
17+
18+
```bash
19+
pip/uv-add "pydantic-ai-slim[outlines-mlxlm]"
20+
```
21+
22+
There are 5 optional groups for the 5 models supported through Outlines:
23+
24+
- `outlines-transformers`
25+
- `outlines-llamacpp`
26+
- `outlines-mlxlm`
27+
- `outlines-sglang`
28+
- `outlines-vllm-offline`
29+
30+
## Model Initialization
31+
32+
As Outlines is not an inference provider, but instead a library allowing you to run bith local and API-based models, instantiating the model is a bit different from the other models available on Pydantic AI.
33+
34+
To initialize the `OutlinesModel` through the `__init__` method, the first argument you must provide has to be an `outlines.Model` or an `outlines.AsyncModel` instance.
35+
36+
For instance:
37+
38+
```python {test="skip_ci"}
39+
import outlines
40+
from transformers import AutoModelForCausalLM, AutoTokenizer
41+
42+
from pydantic_ai.models.outlines import OutlinesModel
43+
44+
outlines_model = outlines.from_transformers(
45+
AutoModelForCausalLM.from_pretrained('erwanf/gpt2-mini'),
46+
AutoTokenizer.from_pretrained('erwanf/gpt2-mini')
47+
)
48+
model = OutlinesModel(outlines_model)
49+
```
50+
51+
Alternatively, you can use some `OutlinesModel` class methods made to load a specific type of Outlines model directly. To do so, you must provide as argument the same arguments you would have given to the associated Outlines model loading function.
52+
53+
For instance:
54+
55+
```python {test="skip_ci"}
56+
from transformers import AutoModelForCausalLM, AutoTokenizer
57+
58+
from pydantic_ai.models.outlines import OutlinesModel
59+
60+
model = OutlinesModel.from_transformers(
61+
AutoModelForCausalLM.from_pretrained('erwanf/gpt2-mini'),
62+
AutoTokenizer.from_pretrained('erwanf/gpt2-mini')
63+
)
64+
```
65+
66+
There are methods for the 5 Outlines models that are officially supported in the integration into Pydantic AI:
67+
- [`from_transformers`][pydantic_ai.models.OutlinesModel.from_transformers]
68+
- [`from_llamacpp`][pydantic_ai.models.OutlinesModel.from_llamacpp]
69+
- [`from_mlxlm`][pydantic_ai.models.OutlinesModel.from_mlxlm]
70+
- [`from_sglang`][pydantic_ai.models.OutlinesModel.from_sglang]
71+
- [`from_vllm_offline`][pydantic_ai.models.OutlinesModel.from_vllm_offline]
72+
73+
As you already providing an Outlines model instance, there is no need to provide an `OutlinesProvider` yourself.
74+
75+
## Running the model
76+
77+
Once you have initialized an `OutlinesModel`, you can use it with an Agent as with all other Pydantic AI models.
78+
79+
Outlines does not support tools yet, but support for that feature will be added in the near future.
80+
81+
## Model Settings
82+
83+
As Outlines can be used with various models that do not share the same model settings, the [OutlinesModelSettings][pydantic_ai.models.OutlinesModel.OutlinesModelSettings] class is a bit different from that of the other Pydantic AI models.
84+
85+
There are no Outlines-specific settings added to the class, you are instead in charge of providing the settings needed for the model of your choice. The standard settings inherited from [ModelSettings][pydantic_ai.settings.ModelSettings] are mapped to the corresponding argument for your model when possible, otherwise they are ignored with a warning.

docs/models/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Pydantic AI is model-agnostic and has built-in support for multiple model provid
1010
* [Cohere](cohere.md)
1111
* [Bedrock](bedrock.md)
1212
* [Hugging Face](huggingface.md)
13+
* [Outlines](outlines.md)
1314

1415
## OpenAI-compatible Providers
1516

docs/thinking.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ Thinking is supported by the `command-a-reasoning-08-2025` model. It does not ne
100100

101101
Text output inside `<think>` tags is automatically converted to [`ThinkingPart`][pydantic_ai.messages.ThinkingPart] objects.
102102
You can customize the tags using the [`thinking_tags`][pydantic_ai.profiles.ModelProfile.thinking_tags] field on the [model profile](models/openai.md#model-profile).
103+
104+
## Outlines
105+
106+
Thinking is not supported by Outlines.

0 commit comments

Comments
 (0)