Skip to main content

Installation

Install both the Netra SDK and OpenAI SDK:
pip install netra-sdk openai

Usage

Initialize the Netra SDK with OpenAI instrumentation enabled. The SDK automatically traces all OpenAI API calls once initialized.
from netra import Netra
from openai import OpenAI

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

# Use OpenAI client as usual - all calls are automatically traced
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

completion = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "user", "content": "What is observability?"}
    ]
)

print(completion.choices[0].message.content)

Streaming Responses

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

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Embeddings

Embedding operations are also automatically instrumented:
embedding = client.embeddings.create(
    model="text-embedding-ada-002",
    input="The quick brown fox jumps over the lazy dog"
)

print(embedding.data[0].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 OpenAI instrumentation
Netra.init(
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    instruments={InstrumentSet.OPENAI}
)

# 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