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.

OllamaOptions is the configuration value object for the Ollama provider. All properties are declared readonly, making every instance fully immutable after construction. You can create an instance directly via OllamaOptions::fromArray(), or let Ollama::create() build one internally from the config array you pass to it.

Namespace

use AiSdk\Ollama\OllamaOptions;

Constants

ConstantValueDescription
DEFAULT_BASE_URL'http://localhost:11434/v1'The default OpenAI-compatible API base URL used when neither the config array nor the OLLAMA_BASE_URL environment variable supplies a value.
PROVIDER_NAME'ollama'The canonical provider name string used throughout the SDK to identify this provider.

Constructor

All six properties are readonly — they can only be set at construction time:
public function __construct(
    public readonly ?string $apiKey,
    public readonly OllamaApi $api = OllamaApi::ChatCompletions,
    public readonly string $baseUrl = self::DEFAULT_BASE_URL,
    public readonly string $nativeBaseUrl = 'http://localhost:11434',
    public readonly array $headers = [],
    public readonly ?Sdk $sdk = null,
) {}

Properties

apiKey
?string
Optional bearer token used to authenticate requests. When set, every outgoing request receives an Authorization: Bearer {apiKey} header. Pass null (or omit it) to make unauthenticated requests, which is the typical setup for a local Ollama instance.
api
OllamaApi
default:"OllamaApi::ChatCompletions"
Selects the default API surface used for text generation requests. Accepts either OllamaApi::ChatCompletions (OpenAI-compatible /chat/completions) or OllamaApi::Responses (/responses). See OllamaApi for a full feature comparison.
baseUrl
string
default:"'http://localhost:11434/v1'"
Base URL for OpenAI-compatible endpoints such as /chat/completions and /responses. Trailing slashes are stripped automatically. Override this when Ollama is running on a non-default host or port, or when routing through a reverse proxy.
nativeBaseUrl
string
default:"'http://localhost:11434'"
Base URL for Ollama-native endpoints: /api/tags, /api/show, and /api/embed. Defaults to baseUrl with the /v1 suffix stripped. Override this independently when your native API is served from a different origin than the OpenAI-compatible API.
headers
array<string, string>
default:"[]"
Additional HTTP headers merged into every outgoing request. Useful for passing custom authentication tokens, tracing headers, or any other provider-specific metadata. When apiKey is also set, the Authorization header is prepended to — not replaced by — this array.
sdk
?Sdk
default:"null"
A custom AiSdk\Support\Sdk instance that wraps the underlying HTTP client and request/stream factories. When null, the provider falls back to Generate::sdk(). Supply your own instance when you need custom HTTP middleware, timeouts, or a test-doubles setup.

Static Factory Method

public static function fromArray(array $config = []): self
The recommended way to construct an OllamaOptions instance. Resolves each setting from the supplied $config array, falling back to environment variables and then to built-in defaults, in that priority order.

Resolution rules

PropertyConfig keyEnvironment variableDefault
apiKeyapiKeyOLLAMA_API_KEYnull
apiapiOllamaApi::ChatCompletions
baseUrlbaseUrlOLLAMA_BASE_URLDEFAULT_BASE_URL
nativeBaseUrlnativeBaseUrlOLLAMA_NATIVE_BASE_URLbaseUrl with /v1 stripped
headersheaders[]
sdksdknull
apiKey — reads $config['apiKey'] first; if absent or empty, checks the OLLAMA_API_KEY environment variable. Stored as null when both sources are empty. api — delegated to OllamaApi::resolve($config['api'] ?? null). Accepts a string ('chat_completions', 'responses'), an OllamaApi enum case, or null (defaults to ChatCompletions). baseUrl — reads $config['baseUrl'] first; if absent, checks OLLAMA_BASE_URL; if still absent, falls back to DEFAULT_BASE_URL. Trailing slashes are stripped. nativeBaseUrl — reads $config['nativeBaseUrl'] first; if absent, checks OLLAMA_NATIVE_BASE_URL; if still absent, derives the value by removing the trailing /v1 segment from the resolved baseUrl. Trailing slashes are stripped. headers — passed through directly from $config['headers'] when it is an array; otherwise defaults to []. No environment variable fallback. sdk — passed through from $config['sdk'] when it is an instance of AiSdk\Support\Sdk; otherwise stored as null. No environment variable fallback.

Example

$options = OllamaOptions::fromArray([
    'baseUrl'       => 'http://localhost:11434/v1',
    'nativeBaseUrl' => 'http://localhost:11434',
    'apiKey'        => 'my-token',
    'api'           => 'chat_completions',
    'headers'       => ['X-Custom' => 'value'],
]);

Instance Method

authHeaders()

public function authHeaders(): array<string, string>
Returns the complete set of authentication and custom headers to be sent with every request. When apiKey is non-null, Authorization: Bearer {apiKey} is prepended to the headers array using array_merge, ensuring it appears first. Because array_merge lets later string keys overwrite earlier ones, any Authorization key present in headers will take precedence over the prepended value — avoid setting Authorization in headers when apiKey is also set.
$headers = $options->authHeaders();
// ['Authorization' => 'Bearer my-token', 'X-Custom' => 'value']
When apiKey is null, the method returns the headers array unchanged:
$options  = OllamaOptions::fromArray(['headers' => ['X-Trace-Id' => 'abc123']]);
$headers  = $options->authHeaders();
// ['X-Trace-Id' => 'abc123']

Build docs developers (and LLMs) love