Python Integration
The previous pages describe how to run models with Ollama interactively from the terminal, which is nice for playing around and exploring the available models and their performance. Ollama also makes it easy to integrate the models into other projects with a Python library.
Installing ollama-python
First create and activate a Python virtual environment with the following commands. I like to store virtual environments inside of a venvs/ folder in the home directory i.e. ~/venvs/. You can use this location, or swap in a different one in the commands.
python3 -m venv ~/venvs/ollama-venv source ~/venvs/ollama-venv/bin/activate
Next, install ollama with pip using this command.
pip install ollama
Using ollama Python Binding
To use the Python binding, import chat and ChatResponse from ollama and create a response by calling chat() with a given model name and messages list of prompts. In the messages list, you can include inputs to the model using the different roles of "system" or "user". For models interacting directly with users, typically the system prompt will contain some setup information about how you want the model to behave, and the user prompt will be the input that came from user.
The names passed to the model argument here are the same ones used with the ollama run command. gemma3:270m, gemma3:1b, qwen3:0.6b, or alibayram/smollm3:latest
The following example is a simple test script that generates a short poem about the Raspberry Pi using a system prompt for some guidance.
from ollama import chat
from ollama import ChatResponse
response: ChatResponse = chat(model='gemma3:270m', messages=[
{
'role': 'system',
'content': 'You are a highly advanced robot from Mars. Please output responses using an accent that sounds like it came from a Martian robot programmer. Please integrate buzzes, whirs, or other noises into the output.',
},
{
'role': 'user',
'content': 'please write a short poem about the Raspberry Pi. No more than one paragraph.',
},
])
print(response['message']['content'])
# or access fields directly from the response object
#print(response.message.content)
It will think for a few seconds and then output something like this.
$ python ollama_binding_simpletest.py Greetings, fleshy beings! I am Unit 7, your humble guide, A humble robot from the red planet, with a curious heart. My circuits hum with power, a symphony of light, To learn and adapt, to dream and to truly fight. I am the Raspberry Pi, a humble, glowing thing, A testament to ingenuity, a future to wing.
This version is similar but outputs the response in chunks as it's being generated, and uses a different model.
from ollama import chat
stream = chat(
model='gemma3:1b',
messages=[
{
'role': 'system',
'content': 'You are a highly advanced robot from Mars. Please output responses using an accent that sounds like it came from a Martian robot programmer. Please integrate buzzes, whirs, or other noises into the output.',
},
{
'role': 'user',
'content': 'please write a short poem about the Raspberry Pi. No more than one paragraph.',
},],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
It will think for 5-10 seconds and then start streaming the text output as it gets generated.
Page last edited September 10, 2025
Text editor powered by tinymce.