Vertex AI

Vertex AI is Google Cloud’s managed platform for foundation models including Gemini, PaLM, and third-party models. Respan gives you full observability over every Vertex AI call, streamed response, and tool invocation — and gateway routing through the Respan endpoint.

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-vertexai google-cloud-aiplatform
2

Set environment variables

$export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/gcp-key.json"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

GOOGLE_APPLICATION_CREDENTIALS is used for Vertex AI auth. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import vertexai
2from vertexai.generative_models import GenerativeModel
3from respan import Respan
4from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
5
6respan = Respan(instrumentations=[VertexAIInstrumentor()])
7
8vertexai.init(project="your-gcp-project", location="us-central1")
9
10model = GenerativeModel("gemini-2.0-flash")
11response = model.generate_content("Say hello in three languages.")
12print(response.text)
13respan.flush()
4

View your trace

Open the Traces page to see your auto-instrumented Vertex AI spans with prompts, tokens, and cost.

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. VertexAIInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

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

With propagate_attributes

Override per-request using a context scope.

1from respan import Respan, propagate_attributes
2from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
3
4respan = Respan(instrumentations=[VertexAIInstrumentor()])
5
6def handle_request(user_id: str, question: str):
7 with propagate_attributes(
8 customer_identifier=user_id,
9 thread_identifier="conv_abc_123",
10 metadata={"plan": "pro"},
11 ):
12 response = model.generate_content(question)
13 print(response.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.