Python Ollama Tutorial: Working API Code Examples
Integrating local artificial intelligence into Python applications has never been easier. With Ollama running on your local machine or server infrastructure, developers can easily generate completions, execute multi-turn conversational chat, and process structured JSON outputs without paying cloud API fees or risking sensitive data exfiltration.
This Python Ollama tutorial provides working, copy-pasteable API code examples for modern software engineers. Whether you need a standard synchronous generation script, streaming execution, or structured JSON schema parsing, these code examples will get you started quickly.
Where to Find Python Examples for the Ollama API?
When searching for reliable code patterns and developer resources for local AI development, standard official and community repositories provide useful reference material:
- Official Ollama GitHub Repository: The primary GitHub repo (
ollama/ollama-python) contains full SDK documentation, client definitions, and integration samples. - Ollama Official Blog & API Docs: Provides endpoint specification references, model parameter options, and environment configuration instructions.
- Community Developer Portals: Open-source repositories showcasing real-world implementations using LangChain, LlamaIndex, and native Python web frameworks like FastAPI and Flask.
For a comprehensive overview of client installation and REST endpoint capabilities, check out our Ollama Python library guide.
Essential Python Ollama Tutorial Steps
To get the most out of every Ollama API example Python script, follow this standard three-stage implementation workflow:
[IMAGE: Flowchart showing the essential Python Ollama tutorial steps]
- Start the Ollama Server Engine: Ensure the local daemon process is active on port
11434. - Download Model Weights: Pull your target model (e.g.,
ollama pull llama3) via CLI or script before triggering generation tasks. - Execute Python Requests: Send structured prompts via the official
ollamaPython library or native HTTP requests, handling response payloads gracefully.
If you haven’t yet set up your underlying local environment, read our step-by-step guide on how to run a local LLM in Python.
Basic Ollama API Example in Python
Let’s inspect a complete, minimal Ollama API example Python snippet. This section covers initializing client requests and handling standard response payloads.
Sending Your First Request
Ensure the ollama library is installed (pip install ollama). Then run the following script:
[IMAGE: Code block demonstrating a basic Ollama API example in Python]
import ollama
# Sending a simple synchronous text completion request
response = ollama.generate(
model='llama3',
prompt='Write a short paragraph explaining what a REST API is.'
)
print("Generated Response:")
print(response['response'])
Parsing the JSON Response
When ollama.generate() completes, it returns a dictionary containing important execution metadata alongside the text response.
import json
import ollama
response = ollama.generate(model='llama3', prompt='What is Python?')
# Accessing key response dictionary attributes
generated_text = response.get('response')
total_duration = response.get('total_duration') # Total evaluation time in nanoseconds
eval_count = response.get('eval_count') # Number of generated tokens
print(f"Response: {generated_text}\n")
print(f"Tokens Generated: {eval_count}")
print(f"Total Duration (ms): {total_duration / 1e6:.2f} ms")
Advanced Ollama API Example in Python
Beyond standard generation, real-world applications frequently require real-time token streaming and structured JSON response formatting. Below are working examples of both advanced features.
1. Real-Time Token Streaming Example
Token streaming provides a responsive user experience by printing or broadcasting words as soon as the model generates them:
import ollama
prompt = "List 5 best practices for writing clean code in Python."
print("Streaming response: ", end="", flush=True)
# Enable token streaming via stream=True
stream = ollama.generate(
model='llama3',
prompt=prompt,
stream=True
)
for chunk in stream:
# Print each generated chunk in real-time
print(chunk['response'], end='', flush=True)
print("\n\n[Stream Finished]")
2. Structured JSON Format Enforcement Example
For automated data extraction or API backends, forcing local LLMs to reply strictly in valid JSON format guarantees seamless programmatic parsing:
import json
import ollama
prompt = """
Return a JSON object containing information about 3 popular Python frameworks.
Do not include conversational text or markdown code blocks.
Format:
{
"frameworks": [
{"name": "FrameworkName", "type": "Web/Data/Async"}
]
}
"""
response = ollama.generate(
model='llama3',
prompt=prompt,
format='json' # Enforces JSON output format
)
try:
data = json.loads(response['response'])
print("Parsed JSON Output Successfully:")
print(json.dumps(data, indent=2))
except json.JSONDecodeError as e:
print(f"Failed to parse JSON response: {e}")
Once you master these code examples, you can move forward to building a chat app with Ollama to implement complete conversational interfaces and high-dimensional vector embeddings.
Frequently Asked Questions (FAQ)
How do I enable streaming in the Ollama Python library?
Set stream=True in your ollama.generate() or ollama.chat() function call. This returns a generator yielding individual response dictionary chunks as tokens are rendered by the underlying model.
Can I enforce strict JSON responses from local models?
Yes. Passing format='json' to ollama.generate() or ollama.chat() instructs the model engine to constrain output generation strictly to valid JSON objects, making output parsing reliable.
Where can I find more advanced Python code snippets for Ollama?
You can reference the official ollama-python GitHub repository examples folder, as well as community cookbooks on GitHub and technical developer guides.