# openinference-instrumentation-google-genai

**Python · Instrumentation**

OpenInference Google GenAI Instrumentation

- **Module**: `openinference.instrumentation.google_genai`
- **Version**: 1.6.5
- **Python**: >=3.10, <3.16
- **Instruments**: `google-genai >= 2.0.0`

[PyPI](https://pypi.org/project/openinference-instrumentation-google-genai/) · [README](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-google-genai)

## Install

```sh
pip install openinference-instrumentation-google-genai
```

## Usage

Instrument once, at startup, before the library is used. The [`GoogleGenAIInstrumentor`](https://openinf.dev/python/openinference-instrumentation-google-genai.html.md#openinference.instrumentation.google_genai.GoogleGenAIInstrumentor) reference below lists the options.

```python
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`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/__init__.py#L55-L357)

```python
class GoogleGenAIInstrumentor(BaseInstrumentor)
```

An instrumentor for `google-genai`

#### `instrument`

_method_

```python
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`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

**Parameters**

| Name | Description |
| - | - |
| `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`](https://openinf.dev/python/openinference-instrumentation.html.md#openinference.instrumentation.TraceConfig). |
| `skip_dep_check` | Instrument even if the installed version of `google-genai` is outside the supported range. |

#### `uninstrument`

_method_

```python
def uninstrument() -> None
```

Stop tracing: undo the patches `instrument()` made. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

#### `instrumentation_dependencies`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/__init__.py#L81-L82)

```python
def instrumentation_dependencies() -> Collection[str]
```

## `openinference.instrumentation.google_genai.cache_attributes`

### `get_attributes_from_request`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/cache_attributes.py#L28-L42)

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

### `get_attributes_from_response`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/cache_attributes.py#L45-L54)

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

## `openinference.instrumentation.google_genai.interactions_attributes`

### `get_output_messages`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L149-L167)

```python
def get_output_messages(steps: Any) -> list[Message]
```

### `get_message_objects`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L170-L221)

```python
def get_message_objects(inputs: Any) -> list[Message]
```

### `get_tools`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L224-L226)

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

### `get_token_object_from_response`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L245-L273)

```python
def get_token_object_from_response(response: Any) -> TokenCount
```

### `is_agent_call`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L276-L279)

```python
def is_agent_call(request_parameters: Mapping[str, Any]) -> bool
```

### `is_agent_response`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L282-L285)

```python
def is_agent_response(response: Any) -> bool
```

### `get_attributes_from_request_object`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L288-L335)

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

### `get_attributes_from_get_request_object`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L338-L344)

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

### `get_attributes_from_request`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L347-L354)

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

### `get_attributes_from_get_request`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L357-L363)

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

### `get_attributes_from_response`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-google-genai/src/openinference/instrumentation/google_genai/interactions_attributes.py#L366-L390)

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