Python APIgoogle-genai

Core

Instrumentation

Python · Instrumentation

openinference-instrumentation-google-genai

OpenInference Google GenAI Instrumentation

Module
openinference.instrumentation.google_genai
Version
1.6.4
Python
>=3.10, <3.16
Instruments
google-genai >= 2.0.0

Install

pip install openinference-instrumentation-google-genai

Usage

Instrument once, at startup, before the library is used. The GoogleGenAIInstrumentor reference below lists the options.

from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.google_genai import GoogleGenAIInstrumentor

tracer_provider = TracerProvider()
# ...add a span processor that exports to Phoenix or any OTLP backend.

GoogleGenAIInstrumentor().instrument(
    tracer_provider=tracer_provider,
    config=TraceConfig(hide_inputs=True),  # optional
)

openinference.instrumentation.google_genai

GoogleGenAIInstrumentor

classSource ↗
class GoogleGenAIInstrumentor(BaseInstrumentor)

An instrumentor for google-genai

instrument

method
def instrument(
    *,
    tracer_provider: Optional[TracerProvider] = None,
    config: Optional[TraceConfig] = None,
    skip_dep_check: bool = False,
) -> None

Start tracing google-genai: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s BaseInstrumentor.

Parameters

NameDescription
tracer_provider

The tracer provider the spans are made with. Defaults to the global one, opentelemetry.trace.get_tracer_provider().

config

What the spans record: hide inputs, outputs or messages, or limit image sizes. Defaults to TraceConfig(), which reads the OPENINFERENCE_* environment variables. See TraceConfig.

skip_dep_check

Instrument even if the installed version of google-genai is outside the supported range.

uninstrument

method
def uninstrument() -> None

Stop tracing: undo the patches instrument() made. Inherited from OpenTelemetry’s BaseInstrumentor.

instrumentation_dependencies

methodSource ↗
def instrumentation_dependencies() -> Collection[str]

openinference.instrumentation.google_genai.cache_attributes

get_attributes_from_request

functionSource ↗
def get_attributes_from_request(
    request_parameters: Mapping[str, Any],
    config: TraceConfig | None,
) -> Iterator[tuple[str, AttributeValue]]

get_attributes_from_response

functionSource ↗
def get_attributes_from_response(
    response: types.CachedContent,
) -> Iterator[tuple[str, AttributeValue]]

openinference.instrumentation.google_genai.interactions_attributes

get_output_messages

functionSource ↗
def get_output_messages(steps: Any) -> list[Message]

get_message_objects

functionSource ↗
def get_message_objects(inputs: Any) -> list[Message]

get_tools

functionSource ↗
def get_tools(request_params: Mapping[str, Any]) -> list[Tool]

get_token_object_from_response

functionSource ↗
def get_token_object_from_response(response: Any) -> TokenCount

is_agent_call

functionSource ↗
def is_agent_call(request_parameters: Mapping[str, Any]) -> bool

is_agent_response

functionSource ↗
def is_agent_response(response: Any) -> bool

get_attributes_from_request_object

functionSource ↗
def get_attributes_from_request_object(
    request_parameters: Mapping[str, Any],
    config: TraceConfig | None,
) -> dict[str, AttributeValue]

get_attributes_from_get_request_object

functionSource ↗
def get_attributes_from_get_request_object(
    request_parameters: Mapping[str, Any],
) -> dict[str, AttributeValue]

get_attributes_from_request

functionSource ↗
def get_attributes_from_request(
    request_parameters: Mapping[str, Any],
    config: TraceConfig | None,
) -> Iterable[tuple[str, AttributeValue]]

get_attributes_from_get_request

functionSource ↗
def get_attributes_from_get_request(
    request_parameters: Mapping[str, Any],
) -> Iterable[tuple[str, AttributeValue]]

get_attributes_from_response

functionSource ↗
def get_attributes_from_response(
    request_parameters: Mapping[str, Any],
    response: Any,
) -> dict[str, AttributeValue]