Python and JavaScript
Official libraries: ollama/ollama-python and ollama/ollama-js. They speak the native API on http://localhost:11434, so you do not assemble NDJSON yourself. If the project already depends on the openai package, you can change only the base URL (OpenAI-compatible API).
Install
pip install -U ollama
# or
uv add ollama
The Ollama service must already be running, and you should ollama pull the model you call (examples use gemma4).
Python: smallest chat
from ollama import chat
response = chat(
model="gemma4",
messages=[{"role": "user", "content": "Explain Ollama in one sentence."}],
)
print(response.message.content)
chat() returns a structured object. For multi-turn, append response.message back onto messages. Use Client for a custom host, timeouts, or cloud headers:
from ollama import Client
client = Client(host="http://localhost:11434")
resp = client.chat(
model="gemma4",
messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
print(resp.message.content)
Python: streaming
from ollama import chat
for part in chat(
model="gemma4",
messages=[{"role": "user", "content": "Describe a local LLM in three short paragraphs."}],
stream=True,
):
print(part.message.content, end="", flush=True)
Cloud models use the same calls after ollama pull gemma4:cloud (or the docs’ gpt-oss:120b-cloud). Set model= to the :cloud name.
Direct https://ollama.com:
import os
from ollama import Client
client = Client(
host="https://ollama.com",
headers={"Authorization": "Bearer " + os.environ["OLLAMA_API_KEY"]},
)
Python: generate and JSON
Use generate for single-prompt completion. For JSON, pass format="json" or a JSON Schema / Pydantic model_json_schema() (see official Structured Outputs):
from ollama import generate, chat
print(generate(model="gemma4", prompt="Why is the sky blue?").response)
resp = chat(
model="gemma4",
messages=[{"role": "user", "content": "Return JSON with country and capital for Canada."}],
format="json",
)
print(resp.message.content)
Tool calling: pass Python functions in tools=[...], inspect response.message.tool_calls, run them, then continue with role: tool. Full loop: tool calling.
Embeddings: ollama.embed(model="embeddinggemma", input="...")—see Embeddings.
JavaScript / TypeScript: smallest chat
import ollama from "ollama";
const response = await ollama.chat({
model: "gemma4",
messages: [{ role: "user", content: "Say this is a test" }],
});
console.log(response.message.content);
Or construct a client:
import { Ollama } from "ollama";
const client = new Ollama({ host: "http://localhost:11434" });
const response = await client.chat({
model: "gemma4",
messages: [{ role: "user", content: "Why is the sky blue?" }],
});
console.log(response.message.content);
JavaScript: streaming
import ollama from "ollama";
const stream = await ollama.chat({
model: "gemma4",
messages: [{ role: "user", content: "Explain Ollama in three sentences." }],
stream: true,
});
for await (const part of stream) {
process.stdout.write(part.message.content);
}
Direct cloud:
import { Ollama } from "ollama";
const ollama = new Ollama({
host: "https://ollama.com",
headers: { Authorization: "Bearer " + process.env.OLLAMA_API_KEY },
});
The npm package also exposes embed, generate, and format: "json". In the browser, remember Ollama binds to localhost and may block CORS; production UIs should proxy through your backend.
Official package vs OpenAI SDK
Both clients require a pulled model. The name must match ollama ls, including the tag.
Next steps