# openinference-instrumentation-ollama

**Python · Instrumentation**

OpenInference Ollama Instrumentation

- **Module**: `openinference.instrumentation.ollama`
- **Version**: 0.1.21
- **Python**: >=3.10, <3.16
- **Instruments**: `ollama >= 0.4.0`

[PyPI](https://pypi.org/project/openinference-instrumentation-ollama/) · [README](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-ollama/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-ollama)

## Install

```sh
pip install openinference-instrumentation-ollama
```

## Usage

Instrument once, at startup, before the library is used. The [`OllamaInstrumentor`](https://openinf.dev/python/openinference-instrumentation-ollama.html.md#openinference.instrumentation.ollama.OllamaInstrumentor) reference below lists the options.

```python
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.ollama import OllamaInstrumentor

tracer_provider = TracerProvider()
# ...add a span processor that exports to Phoenix or any OTLP backend.

OllamaInstrumentor().instrument(
    tracer_provider=tracer_provider,
    config=TraceConfig(hide_inputs=True),  # optional
)
```

## `openinference.instrumentation.ollama`

OpenInference instrumentation for the Ollama Python client.

Usage:

“python from openinference.instrumentation.ollama import OllamaInstrumentor

OllamaInstrumentor().instrument(tracer_provider=tracer_provider) “

Chat calls made through `ollama.chat`, `ollama.Client.chat`, and `ollama.AsyncClient.chat` (including `stream=True`) are exported as OpenInference LLM spans. Call `instrument()` before making chat calls; references captured earlier (e.g. `from ollama import chat` at import time) keep the unwrapped method and are not traced. `uninstrument()` restores the original methods and deactivates any wrapper references that were captured while instrumented.

### `OllamaInstrumentor`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-ollama/src/openinference/instrumentation/ollama/__init__.py#L35-L128)

```python
class OllamaInstrumentor(BaseInstrumentor)
```

An instrumentor for the Ollama Python client.

`instrument()` accepts two optional keyword arguments: `tracer_provider` (an OpenTelemetry `TracerProvider`) and `config` (an OpenInference `TraceConfig` for masking sensitive data). It wraps `Client.chat` and `AsyncClient.chat` (covering the module-level `ollama.chat` helper) so chat calls are exported as OpenInference LLM spans; `uninstrument()` restores the original methods.

#### `instrument`

_method_

```python
def instrument(
    *,
    tracer_provider: Optional[TracerProvider] = None,
    config: Optional[TraceConfig] = None,
    skip_dep_check: bool = False,
) -> None
```

Start tracing `ollama`: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

**Parameters**

| Name | Description |
| - | - |
| `tracer_provider` | The tracer provider the spans are made with. Defaults to the global one, `opentelemetry.trace.get_tracer_provider()`. |
| `config` | What the spans record: hide inputs, outputs or messages, or limit image sizes. Defaults to `TraceConfig()`, which reads the `OPENINFERENCE_*` environment variables. See [`TraceConfig`](https://openinf.dev/python/openinference-instrumentation.html.md#openinference.instrumentation.TraceConfig). |
| `skip_dep_check` | Instrument even if the installed version of `ollama` is outside the supported range. |

#### `uninstrument`

_method_

```python
def uninstrument() -> None
```

Stop tracing: undo the patches `instrument()` made. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

#### `instrumentation_dependencies`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-ollama/src/openinference/instrumentation/ollama/__init__.py#L55-L56)

```python
def instrumentation_dependencies() -> Collection[str]
```
