{"id":2632,"library":"opentelemetry-instrumentation-google-generativeai","title":"OpenTelemetry Google Generative AI Instrumentation","description":"This library provides OpenTelemetry automatic instrumentation for the `google-generativeai` Python client, enabling distributed tracing of calls to Google's Generative AI models. It is part of the broader OpenLLMetry project, which focuses on LLM observability, and has a frequent release cadence, often aligning with updates to OpenTelemetry GenAI semantic conventions. The current version is 0.58.0.","status":"active","version":"0.58.0","language":"en","source_language":"en","source_url":"https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-google-generativeai","tags":["opentelemetry","observability","tracing","google","generative-ai","llm","gemini"],"install":[{"cmd":"pip install opentelemetry-instrumentation-google-generativeai google-generativeai opentelemetry-sdk","lang":"bash","label":"Install core packages"}],"dependencies":[{"reason":"This is the core library being instrumented; it must be installed for the instrumentation to function.","package":"google-generativeai","optional":false},{"reason":"Provides the OpenTelemetry API for creating traces.","package":"opentelemetry-api","optional":false},{"reason":"Required for a full OpenTelemetry setup, including `TracerProvider`, `SpanProcessor`, and `Exporter`, to make traces visible.","package":"opentelemetry-sdk","optional":false}],"imports":[{"note":"Note the underscore vs. hyphen in the package name and the module path.","wrong":"from opentelemetry.instrumentation.google_generative_ai import GoogleGenerativeAIInstrumentor","symbol":"GoogleGenerativeAIInstrumentor","correct":"from opentelemetry.instrumentation.google_generativeai import GoogleGenerativeAIInstrumentor"}],"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, SimpleSpanProcessor\nfrom opentelemetry.instrumentation.google_generativeai import GoogleGenerativeAIInstrumentor\nimport google.generativeai as genai\n\n# Configure OpenTelemetry SDK for console output\nresource = Resource.create({\"service.name\": \"google-generativeai-example\"})\nprovider = TracerProvider(resource=resource)\nprocessor = SimpleSpanProcessor(ConsoleSpanExporter())\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# Initialize the Google Generative AI instrumentation\nGoogleGenerativeAIInstrumentor().instrument()\n\n# Configure Google Generative AI with API key\ngenai.configure(api_key=os.environ.get(\"GOOGLE_API_KEY\", \"\"))\n\n# Make a call to the Generative AI model\nif genai.get_default_base_url(): # Check if API key might be configured\n    try:\n        model = genai.GenerativeModel('gemini-pro')\n        print(\"Calling Gemini-Pro...\")\n        response = model.generate_content(\"What is OpenTelemetry?\", stream=False)\n        print(f\"Response: {response.text[:100]}...\")\n    except Exception as e:\n        print(f\"Error calling Google Generative AI: {e}\")\n        print(\"Ensure GOOGLE_API_KEY environment variable is set.\")\nelse:\n    print(\"Google Generative AI API key not configured. Set GOOGLE_API_KEY environment variable.\")","lang":"python","description":"This quickstart demonstrates how to set up the OpenTelemetry SDK with a console exporter, initialize the `GoogleGenerativeAIInstrumentor`, and then make a request using the `google-generativeai` client. Traces for the `generate_content` call will be printed to the console."},"warnings":[{"fix":"Review the release notes for changes related to 'GenAI semantic conventions'. Update your OpenTelemetry collector processors, analysis dashboards, or alerting rules to match the latest attribute names and values. Consider pinning to specific minor versions if stability is critical.","message":"The OpenTelemetry Generative AI Semantic Conventions are actively evolving. This instrumentation frequently updates to conform to new versions of these conventions, which can result in changes to span names, attribute keys, and attribute values between minor versions. If you rely on specific trace data for analysis or alerting, you may need to update your configurations.","severity":"breaking","affected_versions":"0.53.0 and newer (e.g., 0.53.4, 0.54.0, 0.55.0, 0.58.0)"},{"fix":"Ensure your application code includes the full OpenTelemetry SDK setup as shown in the quickstart example, or that it is handled by an OpenTelemetry auto-instrumentation agent if applicable.","message":"This package only provides the instrumentation. To actually see traces, you must install and configure the `opentelemetry-sdk` with a `TracerProvider`, `SpanProcessor`, and an `Exporter` (e.g., `ConsoleSpanExporter`, `OTLPSpanExporter`). Without this setup, instrumentation will run but no traces will be emitted.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always install `google-generativeai` alongside `opentelemetry-instrumentation-google-generativeai` using `pip install opentelemetry-instrumentation-google-generativeai google-generativeai`.","message":"The `google-generativeai` library itself is a required peer dependency. This instrumentation package does not install it automatically.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Set the `GOOGLE_API_KEY` environment variable or ensure `genai.configure(api_key=...)` is called before making model requests.","message":"The Google Generative AI client requires an API key, which must be configured either via the `GOOGLE_API_KEY` environment variable or explicitly through `genai.configure(api_key='YOUR_API_KEY')`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}