Skip to main content

Installation

Install both the Netra SDK and Hugging Face Transformers:
pip install netra-sdk transformers torch

Usage

Initialize the Netra SDK with Hugging Face instrumentation enabled. The SDK automatically traces all Transformers operations once initialized.
from netra import Netra
from transformers import pipeline
import os

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

# Use Transformers pipeline - all calls are automatically traced
generator = pipeline("text-generation", model="gpt2")

response = generator("What is observability?", max_length=50)
print(response[0]['generated_text'])

Streaming Responses

The SDK automatically handles streaming responses from Hugging Face models:
from transformers import TextStreamer

generator = pipeline(
    "text-generation",
    model="gpt2",
    streamer=TextStreamer(skip_prompt=True)
)

# Streaming output is automatically captured
generator("Tell me a story", max_length=100)

Different Task Types

Various Transformers tasks are automatically instrumented:
# Text Classification
classifier = pipeline("sentiment-analysis")
result = classifier("I love this product!")

# Summarization
summarizer = pipeline("summarization")
summary = summarizer("Long text to summarize...", max_length=50)

# Question Answering
qa = pipeline("question-answering")
answer = qa({
    "question": "What is AI?",
    "context": "Artificial Intelligence is..."
})

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

# 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