Skip to main content

Installation

Install both the Netra SDK and Ollama SDK:
pip install netra-sdk ollama

Usage

Initialize the Netra SDK with Ollama instrumentation enabled. The SDK automatically traces all Ollama API calls once initialized.
from netra import Netra
from ollama import Client
import os

# Initialize Netra with Ollama instrumentation
Netra.init(
    app_name="my-ai-app",
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    trace_content=True
)

# Use Ollama client as usual - all calls are automatically traced
client = Client()

response = client.chat(
    model="llama2",
    messages=[
        {"role": "user", "content": "What is observability?"}
    ]
)

print(response['message']['content'])

Streaming Responses

The SDK automatically handles streaming responses and captures the complete output:
stream = client.chat(
    model="llama2",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    print(chunk['message']['content'], end="")

Embeddings

Embedding operations are also automatically instrumented:
embeddings = client.embeddings(
    model="llama2",
    prompt="The quick brown fox jumps over the lazy dog"
)

print(embeddings['embedding'])

Selective Instrumentation

Control which integrations are enabled using the instruments or blockInstruments configuration:
from netra import Netra
from netra.instrumentation.instruments import InstrumentSet

# Only enable Ollama instrumentation
Netra.init(
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    instruments={InstrumentSet.OLLAMA}
)

# Or block specific instrumentations
Netra.init(
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    block_instruments={InstrumentSet.HTTPX}
)

Next Steps

Last modified on February 3, 2026