If you want a local model inside your own program, the shortest route is the official Ollama Python library. Its README says it “provides the easiest way to integrate Python 3.8+ projects with Ollama”, and it works with the Ollama server already running on your computer. You install one package, pull a model, and call chat() — no API key, because the request never leaves your computer.
Install it and send a first chat
The library’s prerequisites are that Ollama should be installed and running, and that you have pulled a model to use with the library. Then install it with pip install ollama.
Its README gives this first script, using the model gemma4 and its own sample question:
from ollama import chatresponse = chat(model='gemma4', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])print(response.message.content)
Read the answer from response.message.content. Ollama’s API introduction confirms the same point for local requests: use http://localhost:11434/api/chat with a downloaded model and omit the authorization header, because local requests do not need an API key.
| Library call | What it does | Example from the README |
|---|---|---|
| chat() | Sends a list of messages and returns a reply | chat(model=’gemma4′, messages=[…]) |
| generate() | Sends one prompt string and returns a completion | generate(model=’gemma4′, prompt=’Why is the sky blue?’) |
| list() | Lists the models on your computer | ollama.list() |
| pull() | Downloads a model | ollama.pull(‘gemma4’) |
| embed() | Returns vectors for a piece of text | ollama.embed(model=’gemma4′, input=’The sky is blue because of rayleigh scattering’) |
Try it: a three-line script
Save the chat script above as a fictional practice file, first-chat.py, and run it with python first-chat.py. For a fictional example, the script prints a short answer to “Why is the sky blue?” and then ends — if it prints an error about a connection instead, Ollama is not running, and the server needs starting before the script can work. To see the answer arrive piece by piece, add stream=True to the chat() call and print each chunk’s message.content as it arrives; the README says streaming is enabled by setting stream=True. Expected result. We checked the Ollama Python library README and the API introduction. We did not install the package or run a script on this machine.
Limits
The library is only a way to talk to the Ollama server: if the server is not running, or the model named in the script has not been pulled, the script fails even though the code is correct. The examples in the README use the model gemma4, so pull that model first, or change the model name to one already in your ollama list. Cloud models are a separate case — the README’s cloud section needs a sign-in and, for direct cloud access, an API key — so keep this script local unless you deliberately set up cloud access. We did not test any script ourselves.
