HuggingFace

HuggingFace is the leading platform for machine learning models, datasets, and tools. The Transformers library provides access to thousands of pretrained models for text generation, classification, translation, and more. Respan gives you full observability over every pipeline run, model inference, and token output — and gateway routing through the OpenAI-compatible Respan endpoint when you want to mix HuggingFace with other providers.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai opentelemetry-instrumentation-transformers transformers
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1from transformers import pipeline
2from respan import Respan
3from opentelemetry.instrumentation.transformers import TransformersInstrumentor
4
5respan = Respan(instrumentations=[TransformersInstrumentor()])
6
7generator = pipeline("text-generation", model="gpt2")
8
9output = generator("Say hello in three languages:", max_new_tokens=100)
10print(output[0]["generated_text"])
11respan.flush()
4

View your trace

Open the Traces page to see your auto-instrumented inference spans with model names, tokens, and latency.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. TransformersInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

1from respan import Respan
2from opentelemetry.instrumentation.transformers import TransformersInstrumentor
3
4respan = Respan(
5 instrumentations=[TransformersInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "hf-api", "version": "1.0.0"},
8)

With propagate_attributes

1from respan import Respan, propagate_attributes
2from opentelemetry.instrumentation.transformers import TransformersInstrumentor
3
4respan = Respan(instrumentations=[TransformersInstrumentor()])
5
6def handle_request(user_id: str, prompt: str):
7 with propagate_attributes(
8 customer_identifier=user_id,
9 thread_identifier="conv_abc_123",
10 metadata={"plan": "pro"},
11 ):
12 output = generator(prompt, max_new_tokens=100)
13 print(output[0]["generated_text"])
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.