pydantic_ai.models.ollama
Setup
For details on how to set up authentication with this model, see model configuration for Ollama.
Ollama model implementation using OpenAI-compatible API.
OllamaModel
dataclass
Bases: OpenAIChatModel
A model that uses Ollama's OpenAI-compatible Chat Completions API.
Self-hosted Ollama (v0.5.0+) honors response_format with json_schema via
llama.cpp's grammar-constrained decoder, so NativeOutput produces
schema-valid output at generation time.
Ollama Cloud currently accepts response_format with json_schema without
error but does not enforce the schema upstream (see
pydantic-ai#4917 and
ollama/ollama#12362). When
this model detects a Cloud path — either a base_url on ollama.com or a
model name ending in -cloud — it disables supports_json_schema_output
on the resolved profile. With that flag off,
NativeOutput raises a clear
UserError so users pick a mode that
actually works on Cloud (ToolOutput —
the default — and PromptedOutput are
both verified to work).
Apart from __init__, all methods are inherited from the base class.
Source code in pydantic_ai_slim/pydantic_ai/models/ollama.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
__init__
__init__(
model_name: str,
*,
provider: (
Literal["ollama"] | Provider[AsyncOpenAI]
) = "ollama",
profile: ModelProfileSpec | None = None,
settings: ModelSettings | None = None
)
Initialize an Ollama model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
The name of the Ollama model to use (e.g. |
required |
provider
|
Literal['ollama'] | Provider[AsyncOpenAI]
|
The provider to use. Defaults to |
'ollama'
|
profile
|
ModelProfileSpec | None
|
The model profile to use. Defaults to a profile picked by the provider based on the model name,
adjusted to disable |
None
|
settings
|
ModelSettings | None
|
Model-specific settings that will be used as defaults for this model. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/models/ollama.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |