{"id":2649,"library":"opentelemetry-instrumentation-transformers","title":"OpenTelemetry Instrumentation for Hugging Face Transformers","description":"The `opentelemetry-instrumentation-transformers` library provides OpenTelemetry instrumentation for the Hugging Face Transformers library. It automatically captures traces for operations performed using Transformers models, helping to monitor and observe AI/ML application performance and behavior. The current version is 0.58.0, and it follows a rapid release cadence, often aligning with updates to OpenTelemetry's Generative AI semantic conventions.","status":"active","version":"0.58.0","language":"en","source_language":"en","source_url":"https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-transformers","tags":["opentelemetry","observability","apm","llm","transformers","huggingface","instrumentation","ai","ml"],"install":[{"cmd":"pip install opentelemetry-instrumentation-transformers transformers","lang":"bash","label":"Install with transformers"}],"dependencies":[{"reason":"The library being instrumented by this package.","package":"transformers","optional":false},{"reason":"Core OpenTelemetry API for tracing.","package":"opentelemetry-api","optional":false},{"reason":"OpenTelemetry SDK for provider and exporter setup.","package":"opentelemetry-sdk","optional":false}],"imports":[{"symbol":"TransformersInstrumentor","correct":"from opentelemetry.instrumentation.transformers import TransformersInstrumentor"}],"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 (\n    ConsoleSpanExporter,\n    SimpleSpanProcessor,\n)\nfrom opentelemetry.instrumentation.transformers import TransformersInstrumentor\nfrom transformers import pipeline\n\n# Configure OpenTelemetry SDK\n# In production, use OTLPExporter to send traces to a collector.\nresource = Resource.create({\"service.name\": \"transformers-app\"})\nprovider = TracerProvider(resource=resource)\nprocessor = SimpleSpanProcessor(ConsoleSpanExporter())\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# Instrument the transformers library\nTransformersInstrumentor().instrument()\n\n# Use a transformers pipeline\nprint(\"Running transformers pipeline...\")\npipe = pipeline(\"sentiment-analysis\")\nresult = pipe(\"I love OpenTelemetry!\")\nprint(\"Pipeline result:\", result)\n\n# Ensure traces are flushed before exit\nprovider.shutdown()\n","lang":"python","description":"This example sets up a basic OpenTelemetry SDK with a console exporter, then instruments the Hugging Face Transformers library. It then runs a sentiment analysis pipeline, generating traces that will be printed to the console."},"warnings":[{"fix":"Consult the OpenTelemetry GenAI Semantic Conventions documentation and the `opentelemetry-instrumentation-transformers` changelog to adapt to updated attribute names and structures.","message":"Frequent updates to OpenTelemetry Generative AI semantic conventions (semconv) may introduce breaking changes in the names and structure of span attributes. Applications consuming these traces should be prepared for attribute schema changes.","severity":"breaking","affected_versions":"0.53.0 and newer"},{"fix":"Ensure `pip install transformers` is executed and `import transformers` can successfully run in your environment before initializing the instrumentor.","message":"The `transformers` library must be installed and importable *before* `TransformersInstrumentor().instrument()` is called. If `transformers` is not present, instrumentation will fail silently or not apply.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the OpenTelemetry Python SDK documentation for proper setup of `TracerProvider`, `Resource`, and a suitable `SpanExporter` (e.g., `OTLPSpanExporter` for sending to an OTel collector).","message":"To observe traces, a complete OpenTelemetry SDK setup (including a `TracerProvider`, `Resource`, and `SpanProcessor` with an `Exporter`) is required. Without a configured exporter, traces will be generated but not sent anywhere.","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"}