ollama.chat() sends messages to a model through a locally running Ollama server and returns the response as a Python dictionary. Pass a model name and a list of conversation messages; read the assistant’s reply from the nested message and content keys. section 7.1.2
response = ollama.chat(
model="gemma3:4b",
messages=[
{"role": "user", "content": "What is the capital of France?"}
],
)
See a complete call that asks gemma3:4b a question; the returned text can be printed with the nested dictionary lookup. section 7.1.2
A chat request can also support an ongoing conversation:
- Messages use system, user, and assistant roles; a system message can set the model’s personality or rules. section 7.6
- With stream=True, responses appear token by token. To preserve conversation history, append each user message and assistant response, then send the full list with each request. section 7.6
