Skip to content

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
@dataclass(init=False)
class OllamaModel(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](https://github.com/pydantic/pydantic-ai/issues/4917) and
    [ollama/ollama#12362](https://github.com/ollama/ollama/issues/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`][pydantic_ai.output.NativeOutput] raises a clear
    [`UserError`][pydantic_ai.exceptions.UserError] so users pick a mode that
    actually works on Cloud ([`ToolOutput`][pydantic_ai.output.ToolOutput] —
    the default — and [`PromptedOutput`][pydantic_ai.output.PromptedOutput] are
    both verified to work).

    Apart from `__init__`, all methods are inherited from the base class.
    """

    def __init__(
        self,
        model_name: str,
        *,
        provider: Literal['ollama'] | Provider[AsyncOpenAI] = 'ollama',
        profile: ModelProfileSpec | None = None,
        settings: ModelSettings | None = None,
    ):
        """Initialize an Ollama model.

        Args:
            model_name: The name of the Ollama model to use (e.g. `'qwen3'`, `'llama3.2'`).
            provider: The provider to use. Defaults to `'ollama'`.
            profile: The model profile to use. Defaults to a profile picked by the provider based on the model name,
                adjusted to disable `supports_json_schema_output` when the request routes through Ollama Cloud.
            settings: Model-specific settings that will be used as defaults for this model.
        """
        if isinstance(provider, str):
            provider = infer_provider(provider)

        if profile is None and _routes_to_ollama_cloud(provider, model_name):
            base_profile = provider.model_profile(model_name)
            assert base_profile is not None  # OllamaProvider always returns a profile
            profile = merge_profile(base_profile, ModelProfile(supports_json_schema_output=False))

        super().__init__(model_name, provider=provider, profile=profile, settings=settings)

__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. 'qwen3', 'llama3.2').

required
provider Literal['ollama'] | Provider[AsyncOpenAI]

The provider to use. Defaults to 'ollama'.

'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 supports_json_schema_output when the request routes through Ollama Cloud.

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
def __init__(
    self,
    model_name: str,
    *,
    provider: Literal['ollama'] | Provider[AsyncOpenAI] = 'ollama',
    profile: ModelProfileSpec | None = None,
    settings: ModelSettings | None = None,
):
    """Initialize an Ollama model.

    Args:
        model_name: The name of the Ollama model to use (e.g. `'qwen3'`, `'llama3.2'`).
        provider: The provider to use. Defaults to `'ollama'`.
        profile: The model profile to use. Defaults to a profile picked by the provider based on the model name,
            adjusted to disable `supports_json_schema_output` when the request routes through Ollama Cloud.
        settings: Model-specific settings that will be used as defaults for this model.
    """
    if isinstance(provider, str):
        provider = infer_provider(provider)

    if profile is None and _routes_to_ollama_cloud(provider, model_name):
        base_profile = provider.model_profile(model_name)
        assert base_profile is not None  # OllamaProvider always returns a profile
        profile = merge_profile(base_profile, ModelProfile(supports_json_schema_output=False))

    super().__init__(model_name, provider=provider, profile=profile, settings=settings)