Skip to main content

OpenInference Ollama Instrumentation

pypi

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 via tools=[...] are converted to their JSON schemas
  • llm.provider (ollama) and llm.model_name (recorded from the request as well, so errored calls still carry the model)
  • Token counts: prompt_eval_countllm.token_count.prompt, eval_countllm.token_count.completion, with the total derived when both are present
  • llm.invocation_parameters (request options other than messages, model, and tools)
  • Errors: exceptions set the span status to ERROR and 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 via import ollama; ollama.chat(...) or a Client/AsyncClient instance. A reference captured before instrumentation (e.g. from ollama import chat at import time) keeps the uninstrumented function and produces no spans. To be captured on the span, tools must 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

PyPI package: 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

openinference_instrumentation_ollama-0.1.2.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file openinference_instrumentation_ollama-0.1.2.tar.gz.

File metadata

File hashes

Hashes for openinference_instrumentation_ollama-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e97282b1b8d965497a6dc84574de0e4654524e3ebf8de0edbabb8b6606c61ecb
MD5 fec4ee87fa4108a070385c4f28992555
BLAKE2b-256 41d99944fb3625d6206e2c57997895ab5131287b4e4ede077ba597cf2a011677

See more details on using hashes here.

Provenance

The following attestation bundles were made for openinference_instrumentation_ollama-0.1.2.tar.gz:

Publisher: publish.yaml on Arize-ai/openinference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openinference_instrumentation_ollama-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for openinference_instrumentation_ollama-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70469d337beee0c3bd511b2d5b8ac335c5dde539fa08de46f072586a4335e3a2
MD5 2fcc8384c5c4dece6e3a77f6f634b9f9
BLAKE2b-256 90dc9457d2ca3c998aa7ea146a19d9e21657cb83537493e1aebfd6d64f6ea7c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for openinference_instrumentation_ollama-0.1.2-py3-none-any.whl:

Publisher: publish.yaml on Arize-ai/openinference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page