{"id":2162,"library":"opentelemetry-instrumentation-ollama","title":"OpenTelemetry Ollama Instrumentation","description":"This library provides OpenTelemetry instrumentation for tracing calls to Ollama's endpoints made with the official Ollama Python Library. It is part of the OpenLLMetry project and sees frequent releases, often multiple times a month, reflecting active development and continuous alignment with evolving OpenTelemetry semantic conventions for AI applications.","status":"active","version":"0.58.0","language":"en","source_language":"en","source_url":"https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-ollama","tags":["opentelemetry","ollama","observability","instrumentation","ai","llm","tracing","genai"],"install":[{"cmd":"pip install opentelemetry-instrumentation-ollama ollama opentelemetry-sdk","lang":"bash","label":"Core Installation"},{"cmd":"pip install opentelemetry-distro","lang":"bash","label":"Recommended for Auto-Instrumentation"}],"dependencies":[{"reason":"The instrumentation traces calls made using the official Ollama Python client library. This package is not a direct dependency of the instrumentation itself but is required for its functionality.","package":"ollama"},{"reason":"Core OpenTelemetry API for defining telemetry. Implicit dependency for any OpenTelemetry instrumentation.","package":"opentelemetry-api"},{"reason":"Core OpenTelemetry SDK for processing and exporting telemetry. Implicit dependency for any OpenTelemetry instrumentation.","package":"opentelemetry-sdk"},{"reason":"Provides default OpenTelemetry API/SDK setup and helpful auto-instrumentation tools (like `opentelemetry-bootstrap` and `opentelemetry-instrument`) for easier setup.","package":"opentelemetry-distro","optional":true}],"imports":[{"symbol":"OllamaInstrumentor","correct":"from opentelemetry.instrumentation.ollama import OllamaInstrumentor"}],"quickstart":{"code":"import os\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import Resource\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor\nfrom opentelemetry.instrumentation.ollama import OllamaInstrumentor\nimport ollama\n\n# 1. Set up OpenTelemetry Tracer Provider\n# This simple setup exports traces to the console.\nresource = Resource.create(attributes={\"service.name\": \"ollama-app\"})\nprovider = TracerProvider(resource=resource)\nprocessor = BatchSpanProcessor(ConsoleSpanExporter())\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# 2. Instrument Ollama\nOllamaInstrumentor().instrument()\n\n# 3. Use Ollama client (ensure ollama server is running and model pulled)\ntry:\n    print(\"\\n--- Making an Ollama chat request ---\")\n    chat_response = ollama.chat(\n        model='llama2',\n        messages=[{'role': 'user', 'content': 'Why is the sky blue?'}]\n    )\n    print(\"Ollama Chat Response:\")\n    print(chat_response['message']['content'])\n    print(\"\\n--- Trace for chat request should be visible above ---\")\n\n    print(\"\\n--- Making an Ollama generate request (streaming) ---\")\n    stream = ollama.generate(\n        model='llama2',\n        prompt='Tell me a short story about a space-faring cat.',\n        stream=True\n    )\n    full_response = \"\"\n    print(\"Streaming Ollama Response:\")\n    for chunk in stream:\n        if 'response' in chunk:\n            full_response += chunk['response']\n            print(chunk['response'], end='', flush=True)\n    print(\"\\n\\n--- Trace for streaming request should be visible above ---\")\n\nexcept Exception as e:\n    print(f\"\\nError interacting with Ollama: {e}\")\n    print(\"Please ensure the Ollama server is running (e.g., `ollama serve`) \")\n    print(\"and the 'llama2' model is pulled (e.g., `ollama pull llama2`).\")\n\n","lang":"python","description":"This quickstart demonstrates how to set up a basic OpenTelemetry `TracerProvider` that exports traces to the console, and then instruments the `ollama` client to automatically capture traces for chat and generate operations. Ensure you have the `ollama` client library installed and an Ollama server running locally with the `llama2` model pulled."},"warnings":[{"fix":"Review the OpenTelemetry GenAI Semantic Conventions documentation for updated attribute names. Update your trace processors, dashboards, and any custom logic to reflect the new conventions. Consider using environment variables like `OTEL_SEMCONV_STABILITY_OPT_IN` for a phased migration if supported by your OpenTelemetry SDK.","message":"Recent versions (e.g., v0.53.0 onwards) have migrated to align with the OpenTelemetry GenAI Semantic Conventions (0.5.0). This change introduces new attribute names and structures for LLM-related spans, potentially breaking existing dashboards, alerts, or custom processors relying on older conventions.","severity":"breaking","affected_versions":">=0.53.0"},{"fix":"To disable logging of sensitive content for privacy reasons or to reduce trace size, set the `TRACELOOP_TRACE_CONTENT` environment variable to `false` (e.g., `export TRACELOOP_TRACE_CONTENT=false`).","message":"By default, this instrumentation captures and logs prompts, completions, and embeddings to span attributes. This data may contain highly sensitive user information.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure you have installed `ollama` via `pip install ollama`. Start your Ollama server (e.g., `ollama serve`) and pull any necessary models (e.g., `ollama pull llama2`) before running your instrumented application.","message":"This library instruments the `ollama` Python client. For the instrumentation to be effective, the `ollama` client library must be installed separately, and an Ollama server instance must be running and accessible with the required models pulled.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}