Skip to content

Commit 4a92d48

Browse files
committed
Add documentation for the Outlines model
1 parent 5da96a9 commit 4a92d48

File tree

9 files changed

+183
-2
lines changed

9 files changed

+183
-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).

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
@@ -37,6 +37,7 @@ making it ideal for queries that require up-to-date data.
3737
| Mistral || Not supported |
3838
| Cohere || Not supported |
3939
| HuggingFace || Not supported |
40+
| Outlines || Not supported |
4041

4142
### Usage
4243

@@ -128,6 +129,7 @@ in a secure environment, making it perfect for computational tasks, data analysi
128129
| Mistral || |
129130
| Cohere || |
130131
| HuggingFace || |
132+
| Outlines || |
131133

132134
### Usage
133135

@@ -320,6 +322,7 @@ allowing it to pull up-to-date information from the web.
320322
| Mistral || |
321323
| Cohere || |
322324
| HuggingFace || |
325+
| Outlines || |
323326

324327
### Usage
325328

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 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](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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ 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-transformers` - installs `outlines[transformers]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
55+
* `outlines-llamacpp` - installs `outlines[llamacpp]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
56+
* `outlines-mlxlm` - installs `outlines[mlxlm]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
57+
* `outlines-sglang` - installs `outlines[sglang]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
58+
* `outlines-vllm-offline` - installs `outlines[vllm-offline]` [PyPI ↗](https://pypi.org/project/outlines){:target="_blank"}
5459
* `duckduckgo` - installs `ddgs` [PyPI ↗](https://pypi.org/project/ddgs){:target="_blank"}
5560
* `tavily` - installs `tavily-python` [PyPI ↗](https://pypi.org/project/tavily-python){:target="_blank"}
5661
* `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: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Outlines
2+
3+
## Install
4+
5+
As Outlines is a library allowing you to run models from various different providers, it does not include the necessary dependencies for any provider by default. As a result, to use the [`OutlinesModel`][pydantic_ai.models.OutlinesModel], you must install `pydantic-ai-slim` with an optional group composed of outlines, a dash, and the name of the specific model provider you would use through Outlines. For instance:
6+
7+
```bash
8+
pip/uv-add "pydantic-ai-slim[outlines-transformers]"
9+
```
10+
11+
Or
12+
13+
```bash
14+
pip/uv-add "pydantic-ai-slim[outlines-mlxlm]"
15+
```
16+
17+
There are 5 optional groups for the 5 model providers supported through Outlines:
18+
19+
- `outlines-transformers`
20+
- `outlines-llamacpp`
21+
- `outlines-mlxlm`
22+
- `outlines-sglang`
23+
- `outlines-vllm-offline`
24+
25+
## Model Initialization
26+
27+
As Outlines is not an inference provider, but instead a library allowing you to run both local and API-based models, instantiating the model is a bit different from the other models available on Pydantic AI.
28+
29+
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.
30+
31+
For instance:
32+
33+
```python {test="skip"}
34+
import outlines
35+
from transformers import AutoModelForCausalLM, AutoTokenizer
36+
37+
from pydantic_ai.models.outlines import OutlinesModel
38+
39+
outlines_model = outlines.from_transformers(
40+
AutoModelForCausalLM.from_pretrained('erwanf/gpt2-mini'),
41+
AutoTokenizer.from_pretrained('erwanf/gpt2-mini')
42+
)
43+
model = OutlinesModel(outlines_model)
44+
```
45+
46+
As you already providing an Outlines model instance, there is no need to provide an `OutlinesProvider` yourself.
47+
48+
### Model Loading Methods
49+
50+
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 arguments the same arguments you would have given to the associated Outlines model loading function (except in the case of SGLang).
51+
52+
There are methods for the 5 Outlines models that are officially supported in the integration into Pydantic AI:
53+
54+
- [`from_transformers`][pydantic_ai.models.OutlinesModel.from_transformers]
55+
- [`from_llamacpp`][pydantic_ai.models.OutlinesModel.from_llamacpp]
56+
- [`from_mlxlm`][pydantic_ai.models.OutlinesModel.from_mlxlm]
57+
- [`from_sglang`][pydantic_ai.models.OutlinesModel.from_sglang]
58+
- [`from_vllm_offline`][pydantic_ai.models.OutlinesModel.from_vllm_offline]
59+
60+
#### Transformers
61+
62+
```python {test="skip"}
63+
from transformers import AutoModelForCausalLM, AutoTokenizer
64+
65+
from pydantic_ai.models.outlines import OutlinesModel
66+
67+
model = OutlinesModel.from_transformers(
68+
AutoModelForCausalLM.from_pretrained('microsoft/Phi-3-mini-4k-instruct'),
69+
AutoTokenizer.from_pretrained('microsoft/Phi-3-mini-4k-instruct')
70+
)
71+
```
72+
73+
#### LlamaCpp
74+
75+
```python {test="skip"}
76+
from llama_cpp import Llama
77+
78+
from pydantic_ai.models.outlines import OutlinesModel
79+
80+
model = OutlinesModel.from_llamacpp(
81+
Llama.from_pretrained(
82+
repo_id='TheBloke/Mistral-7B-Instruct-v0.2-GGUF',
83+
filename='mistral-7b-instruct-v0.2.Q5_K_M.gguf',
84+
)
85+
)
86+
```
87+
88+
#### MLXLM
89+
90+
```python {test="skip"}
91+
from mlx_lm import load
92+
93+
from pydantic_ai.models.outlines import OutlinesModel
94+
95+
model = OutlinesModel.from_mlxlm(
96+
*load('mlx-community/TinyLlama-1.1B-Chat-v1.0-4bit')
97+
)
98+
```
99+
100+
#### SGLang
101+
102+
```python {test="skip"}
103+
from pydantic_ai.models.outlines import OutlinesModel
104+
105+
model = OutlinesModel.from_sglang(
106+
'http://localhost:11434',
107+
'api_key',
108+
'meta-llama/Llama-3.1-8B'
109+
)
110+
```
111+
112+
#### vLLM Offline
113+
114+
```python {test="skip"}
115+
from vllm import LLM
116+
117+
from pydantic_ai.models.outlines import OutlinesModel
118+
119+
model = OutlinesModel.from_vllm_offline(
120+
LLM('microsoft/Phi-3-mini-4k-instruct')
121+
)
122+
```
123+
124+
## Running the model
125+
126+
Once you have initialized an `OutlinesModel`, you can use it with an Agent as with all other Pydantic AI models.
127+
128+
As Outlines is focused on structured output, this provider supports the `output_type` component through the `NativeOutput` format.
129+
130+
```python {test="skip"}
131+
from pydantic import BaseModel
132+
from transformers import AutoModelForCausalLM, AutoTokenizer
133+
134+
from pydantic_ai import Agent
135+
from pydantic_ai.models.outlines import OutlinesModel
136+
from pydantic_ai.settings import ModelSettings
137+
138+
class Box(BaseModel):
139+
width: int
140+
height: int
141+
depth: int
142+
units: str
143+
144+
model = OutlinesModel.from_transformers(
145+
AutoModelForCausalLM.from_pretrained('microsoft/Phi-3-mini-4k-instruct'),
146+
AutoTokenizer.from_pretrained('microsoft/Phi-3-mini-4k-instruct')
147+
)
148+
agent = Agent(model, output_type=Box)
149+
150+
result = agent.run_sync(
151+
'Give me the dimensions of a box',
152+
model_settings=ModelSettings(extra_body={'max_new_tokens': 100})
153+
)
154+
print(result.output) # width=20 height=30 depth=40 units='cm'
155+
```
156+
157+
Outlines does not support tools yet, but support for that feature will be added in the near future.

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ Thinking is supported by the `command-a-reasoning-08-2025` model. It does not ne
9898

9999
Text output inside `<think>` tags is automatically converted to [`ThinkingPart`][pydantic_ai.messages.ThinkingPart] objects.
100100
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).
101+
102+
## Outlines
103+
104+
Some local models run through Outlines include in their text output a thinking part delimited by tags. In that case, it will be handled by Pydantic AI that will separate the thinking part from the final answer without the need to specifically enable it. The thinking tags used by default are `"<think>"` and `"</think>"`. If your model uses different tags, you can specify them in the [model profile](models/openai.md#model-profile) using the [`thinking_tags`][pydantic_ai.profiles.ModelProfile.thinking_tags] field.
105+
106+
Outlines currently does not support thinking along with structured output. If you provide an `output_type`, the model text output will not contain a thinking part with the associated tags, and you may experience degraded performance.

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ nav:
3434
- models/groq.md
3535
- models/mistral.md
3636
- models/huggingface.md
37+
- models/outlines.md
3738
- Tools & Toolsets:
3839
- tools.md
3940
- tools-advanced.md
@@ -123,6 +124,7 @@ nav:
123124
- api/models/huggingface.md
124125
- api/models/instrumented.md
125126
- api/models/mistral.md
127+
- api/models/outlines.md
126128
- api/models/test.md
127129
- api/models/function.md
128130
- api/models/fallback.md

0 commit comments

Comments
 (0)