OpenInference Portkey AI Instrumentation

raw JSON →
0.1.8 verified Fri May 01 auth: no python

OpenInference instrumentation for Portkey AI gateway, enabling tracing of LLM calls made via Portkey. Version 0.1.8 supports Python 3.10-3.14. Part of the Arize AI OpenInference ecosystem for observability of AI applications.

pip install openinference-instrumentation-portkey
error ImportError: cannot import name 'PortkeyInstrumentor' from 'openinference_instrumentation_portkey'
cause The package may not be installed correctly, or you are using an older version that had a different export.
fix
Verify installation: pip install openinference-instrumentation-portkey. Check your Python environment and reinstall if needed.
error AttributeError: module 'openinference_instrumentation_portkey' has no attribute 'PortkeyInstrumentor'
cause Similar to above; the module exists but the instrumentor class is not exported (possible version mismatch).
fix
Ensure you have the latest version: pip install --upgrade openinference-instrumentation-portkey
gotcha Instrumentation must be called before any Portkey client is created. If you import or instantiate Portkey before calling .instrument(), traces will not be captured.
fix Ensure the .instrument() call happens at module-level before any Portkey usage, ideally right after setting up OpenTelemetry.
gotcha If no TracerProvider is set, instrumentation silently fails (no spans emitted). Ensure opentelemetry-api and a TracerProvider (e.g., from opentelemetry-sdk) are configured.
fix Set a global tracer provider via trace_api.set_tracer_provider(TracerProvider()) and add a span processor with an exporter.
gotcha This package requires wrapt. If you have an older version of wrapt or wrapt is missing, you may see import errors. Versions >=1.14 are recommended.
fix Install or upgrade wrapt: pip install 'wrapt>=1.14'

Initialize tracing by setting up an OpenTelemetry TracerProvider and calling PortkeyInstrumentor().instrument() before any Portkey usage.

import os
from openinference_instrumentation_portkey import PortkeyInstrumentor
from opentelemetry import trace as trace_api

# Set up a TracerProvider (e.g., ConsoleSpanExporter for debugging)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

trace_provider = TracerProvider()
trace_provider.add_span_processor(
    SimpleSpanProcessor(OTLPSpanExporter(endpoint=os.environ.get("OTEL_ENDPOINT", "http://localhost:4318/v1/traces")))
)
trace_api.set_tracer_provider(trace_provider)

# Instrument Portkey
PortkeyInstrumentor().instrument()

# Now use Portkey normally - calls will be traced
from portkey_ai import Portkey

client = Portkey(api_key=os.environ.get("PORTKEY_API_KEY", ""))
response = client.completions.create(prompt="Hello", model="gpt-3.5-turbo")
print(response.choices[0].text)