Skip to main content

Installation

Install both the Netra SDK and IBM Watsonx SDK:
pip install netra-sdk ibm-watsonx-ai

Usage

Initialize the Netra SDK with Watsonx instrumentation enabled. The SDK automatically traces all Watsonx API calls once initialized.
from netra import Netra
from ibm_watsonx_ai.foundation_models import Model
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
import os

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

# Use Watsonx client as usual - all calls are automatically traced
model = Model(
    model_id="ibm/granite-13b-chat-v2",
    credentials={
        "url": "https://us-south.ml.cloud.ibm.com",
        "apikey": os.environ.get("WATSONX_API_KEY")
    },
    project_id=os.environ.get("WATSONX_PROJECT_ID")
)

response = model.generate_text(prompt="What is observability?")
print(response)

Streaming Responses

The SDK automatically handles streaming responses and captures the complete output:
model = Model(
    model_id="ibm/granite-13b-chat-v2",
    credentials={
        "url": "https://us-south.ml.cloud.ibm.com",
        "apikey": os.environ.get("WATSONX_API_KEY")
    },
    project_id=os.environ.get("WATSONX_PROJECT_ID")
)

for chunk in model.generate_text_stream(prompt="Tell me a story"):
    print(chunk, end="")

Advanced Parameters

Configure generation parameters for more control:
generate_params = {
    GenParams.MAX_NEW_TOKENS: 100,
    GenParams.TEMPERATURE: 0.7,
    GenParams.TOP_P: 0.9
}

model = Model(
    model_id="ibm/granite-13b-chat-v2",
    credentials={
        "url": "https://us-south.ml.cloud.ibm.com",
        "apikey": os.environ.get("WATSONX_API_KEY")
    },
    project_id=os.environ.get("WATSONX_PROJECT_ID"),
    params=generate_params
)

response = model.generate_text(prompt="Explain AI")
print(response)

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 Watsonx instrumentation
Netra.init(
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    instruments={InstrumentSet.WATSONX}
)

# 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