Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/ollama/llms.txt

Use this file to discover all available pages before exploring further.

OllamaTextModel is the internal model class created by OllamaProvider when a text generation request is made. It implements TextModelInterface and handles both non-streaming and streaming generation, routing requests through either Ollama’s OpenAI-compatible Chat Completions endpoint or the Responses API depending on configuration.

Namespace

use AiSdk\Ollama\Models\OllamaTextModel;
This class is not instantiated directly. Use Ollama::model() to obtain a model instance and the Generate facade to dispatch requests.

Adapter Capabilities

The following capabilities are declared by OllamaTextModel via its ADAPTER_CAPABILITIES constant. These capabilities reflect what the adapter supports at the protocol level and are always declared regardless of which specific model is installed on the Ollama server:
CapabilityDescription
TextGenerationGenerates text completions from prompts and message history
StreamingSupports server-sent event streaming via stream()
ToolCallingForwards tool/function definitions and parses tool calls
StructuredOutputEnforces structured JSON output via the Chat Completions API
ReasoningPasses reasoning effort controls to thinking-capable models
TextInputAccepts plain text as input
ImageInputAccepts image URLs or base64 data as vision input

Methods

generate(TextModelRequest $request): TextModelResponse

Sends a synchronous generation request and returns the full response once complete. Internally, generate():
  1. Resolves which API to use (Chat Completions or Responses) via resolveApi() — see API Resolution below.
  2. Validates the request/API combination via ensureApiRequestSupported() — see Validation below.
  3. Builds the request body using ChatRequestBuilder::build() or ResponsesRequestBuilder::build() depending on the resolved API.
  4. Strips the internal api key from the built body before sending.
  5. POSTs to the appropriate URL and parses the response using ChatResponseParser or ResponsesResponseParser.
$result = Generate::text('Explain black holes in one sentence.')
    ->model(Ollama::model('llama3.2'))
    ->run();

echo $result->text;

stream(TextModelRequest $request): Generator

Sends a streaming request and returns a Generator that yields string chunks as they arrive from the server. Chunks are parsed from server-sent events using ChatStreamParser or ResponsesStreamParser, depending on the resolved API.
$stream = Generate::text('Write a short poem about the sea.')
    ->model(Ollama::model('llama3.2'))
    ->stream();

foreach ($stream as $chunk) {
    echo $chunk;
}

provider(): string

Returns the provider identifier string 'ollama'.

modelId(): string

Returns the model ID string that was passed at construction — for example 'llama3.2' or 'acme/private-model:latest'.

API Resolution

Each request is routed to one of two Ollama API backends. The API is resolved in the following order:
  1. Per-request override — if $request->providerOptionsFor('ollama')['api'] is set, that string is normalised to an OllamaApi enum case via OllamaApi::resolve() and used for the request.
  2. Global default — if no per-request override is present, the already-resolved $this->options->api enum (set at provider creation time via Ollama::create(['api' => ...])) is used directly.
// Set globally at provider creation
Ollama::create(['api' => 'responses']);

// Or override on a per-request basis
Generate::text('Hi')
    ->model(Ollama::model('llama3.2'))
    ->providerOptions('ollama', ['api' => 'responses'])
    ->run();

URL Routing

The request URL is assembled from the configured baseUrl (default: http://localhost:11434/v1):
APIEndpoint
Chat Completions{baseUrl}/chat/completions
Responses{baseUrl}/responses

Validation Constraints

Before every request ensureApiRequestSupported() enforces the following rules. Violations throw InvalidArgumentException:
Responses API + reasoning
InvalidArgumentException
Thrown when the resolved API is Responses and $request->reasoning is not null.
Ollama Responses does not expose reasoning controls. Use chat_completions or let the selected thinking model reason automatically.
Use the Chat Completions API (the default) if you need to pass reasoning effort controls explicitly.
Responses API + structured output
InvalidArgumentException
Thrown when the resolved API is Responses and $request->output is not null.
Ollama Responses does not expose structured-output controls. Use chat_completions for portable structured output.
reasoning effort = minimal
InvalidArgumentException
Thrown when $request->reasoning->effort === 'minimal', regardless of API.
Ollama Chat Completions reasoning effort must be low, medium, or high.
Ollama’s thinking models accept low, medium, or high reasoning effort only.

Usage Examples

Basic text generation

use AiSdk\Generate;
use AiSdk\Ollama;

Ollama::create();

$result = Generate::text('What is the capital of France?')
    ->model(Ollama::model('llama3.2'))
    ->run();

echo $result->text;
// → Paris

Streaming

Ollama::create();

$stream = Generate::text('Count from one to five.')
    ->model(Ollama::model('llama3.2'))
    ->stream();

foreach ($stream as $chunk) {
    echo $chunk;
}

Vision input

Ollama::create();

$result = Generate::text()
    ->model(Ollama::model('llava'))
    ->messages([
        AiSdk\Message::user([
            AiSdk\Content::text('What is in this image?'),
            AiSdk\Content::image('https://example.com/photo.jpg'),
        ]),
    ])
    ->run();

echo $result->text;

Reasoning with thinking models

Ollama::create();

$result = Generate::text('Solve: if x + 3 = 10, what is x?')
    ->model(Ollama::model('deepseek-r1:8b'))
    ->reasoning(AiSdk\Reasoning::effort('low'))
    ->run();

echo $result->text;

Switching to the Responses API globally

Ollama::create(['api' => 'responses']);

$result = Generate::text('Summarise the water cycle.')
    ->model(Ollama::model('llama3.2'))
    ->run();

echo $result->text;

Build docs developers (and LLMs) love