OpenInference Ollama Instrumentation
Python auto-instrumentation library for the Ollama Python client.
The traces emitted by this instrumentation are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as Arize Phoenix or Arize AX.
What is instrumented
chat calls made through ollama.chat, ollama.Client.chat, and ollama.AsyncClient.chat are exported as OpenInference LLM spans (named Chat and AsyncChat respectively), capturing:
- Input and output messages (
llm.input_messages.*,llm.output_messages.*), including tool calls - Streaming (
stream=True): the span finishes when the stream is exhausted, fails, or is abandoned, with the output message and token counts reconstructed from the accumulated chunks - Tool definitions as
llm.tools.N.tool.json_schema— plain Python functions passed viatools=[...]are converted to their JSON schemas llm.provider(ollama) andllm.model_name(recorded from the request as well, so errored calls still carry the model)- Token counts:
prompt_eval_count→llm.token_count.prompt,eval_count→llm.token_count.completion, with the total derived when both are present llm.invocation_parameters(request options other thanmessages,model, andtools)- Errors: exceptions set the span status to
ERRORand are recorded as span events
Not currently instrumented: generate, embed/embeddings, and other client methods.
[!NOTE] Call
OllamaInstrumentor().instrument()before making chat calls, and invoke chat viaimport ollama; ollama.chat(...)or aClient/AsyncClientinstance. A reference captured before instrumentation (e.g.from ollama import chatat import time) keeps the uninstrumented function and produces no spans. To be captured on the span,toolsmust be a list or tuple (not a generator).
Context attributes (session, user, metadata, tags via using_attributes) propagate onto spans, and sensitive data can be masked with a TraceConfig, e.g. OllamaInstrumentor().instrument(tracer_provider=tracer_provider, config=TraceConfig(hide_inputs=True)). Calls made inside with suppress_tracing(): are not traced.
Installation
pip install openinference-instrumentation-ollama
Requires ollama >= 0.4.0.
Quickstart
Install packages needed for this demonstration.
pip install openinference-instrumentation-ollama ollama arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp
Install and start the Ollama server (pip install ollama installs only the client), then pull a model. The server listens on http://localhost:11434 by default.
ollama pull llama3.2
Start Phoenix in the background as a collector. By default, it listens on http://localhost:6006. (Phoenix does not send data over the internet. It only operates locally on your machine.)
phoenix serve
Set up OllamaInstrumentor to trace your application and send the traces to Phoenix.
from openinference.instrumentation.ollama import OllamaInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
OllamaInstrumentor().instrument(tracer_provider=tracer_provider)
Run a chat completion against the locally running Ollama server.
import ollama
response = ollama.chat(
model="llama3.2",
messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
print(response.message.content)
Now view your traces in the Phoenix UI at http://localhost:6006.
Examples
The examples/ directory contains runnable scripts. They require a running Phoenix and Ollama server, read the model from the OLLAMA_MODEL environment variable (default llama3.2), and send traces to a Phoenix project named ollama-examples.
pip install -r examples/requirements.txt
OLLAMA_MODEL=llama3.2 python examples/chat.py
| Example | Description |
|---|---|
chat.py |
A basic chat completion |
streaming_and_tools.py |
Streaming with session attributes, and tool calling with a plain Python function |
Development
From the python/ directory: tox run -e test-ollama runs the tests, and tox run -e ruff-mypy-test-ollama runs all checks.
More Info
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openinference_instrumentation_ollama-0.1.1.tar.gz.
File metadata
- Download URL: openinference_instrumentation_ollama-0.1.1.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a2036c6b26f01c9a8d4a079b3e97b32b94f145011e461ffc5e6aed42d89ff43
|
|
| MD5 |
daadc83de1af1acaed7adea54d314bb7
|
|
| BLAKE2b-256 |
2e2736acb6f4a3130fc54491df85373700ca6b85bef205ab0998e2d18f61350b
|
Provenance
The following attestation bundles were made for openinference_instrumentation_ollama-0.1.1.tar.gz:
Publisher:
publish.yaml on Arize-ai/openinference
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openinference_instrumentation_ollama-0.1.1.tar.gz -
Subject digest:
5a2036c6b26f01c9a8d4a079b3e97b32b94f145011e461ffc5e6aed42d89ff43 - Sigstore transparency entry: 2371117138
- Sigstore integration time:
-
Permalink:
Arize-ai/openinference@5b9114cdca81af4250531d4e4d8b8eecf7907192 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Arize-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@5b9114cdca81af4250531d4e4d8b8eecf7907192 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file openinference_instrumentation_ollama-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openinference_instrumentation_ollama-0.1.1-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bde1f9ee041975b353e1787e09ba718f486fe45d42ea07a5848ccb267398ca9
|
|
| MD5 |
8d460f71c5fb27fa8ce1c646d52b9374
|
|
| BLAKE2b-256 |
4e7a6b6fe153b0c9f7a0a56b2e9a510735b6d48e43bc24a6881b198eab71cc11
|
Provenance
The following attestation bundles were made for openinference_instrumentation_ollama-0.1.1-py3-none-any.whl:
Publisher:
publish.yaml on Arize-ai/openinference
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openinference_instrumentation_ollama-0.1.1-py3-none-any.whl -
Subject digest:
8bde1f9ee041975b353e1787e09ba718f486fe45d42ea07a5848ccb267398ca9 - Sigstore transparency entry: 2371117151
- Sigstore integration time:
-
Permalink:
Arize-ai/openinference@5b9114cdca81af4250531d4e4d8b8eecf7907192 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Arize-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@5b9114cdca81af4250531d4e4d8b8eecf7907192 -
Trigger Event:
workflow_dispatch
-
Statement type: