Skip to content

Commit 874098f

Browse files
committed
Typing improvements to check that we support both v1 and v1beta1
1 parent c84c532 commit 874098f

File tree

2 files changed

+42
-22
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai

2 files changed

+42
-22
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/patch.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
TYPE_CHECKING,
1919
Any,
2020
Callable,
21-
Iterable,
2221
MutableSequence,
23-
Optional,
24-
Union,
2522
)
2623

2724
from opentelemetry._events import EventLogger
@@ -33,28 +30,35 @@
3330
from opentelemetry.trace import SpanKind, Tracer
3431

3532
if TYPE_CHECKING:
33+
from google.cloud.aiplatform_v1.services.prediction_service import client
3634
from google.cloud.aiplatform_v1.types import (
3735
content,
3836
prediction_service,
3937
)
40-
from vertexai.generative_models import (
41-
GenerationResponse,
38+
from google.cloud.aiplatform_v1beta1.services.prediction_service import (
39+
client as client_v1beta1,
4240
)
43-
from vertexai.generative_models._generative_models import (
44-
_GenerativeModel,
41+
from google.cloud.aiplatform_v1beta1.types import (
42+
content as content_v1beta1,
43+
)
44+
from google.cloud.aiplatform_v1beta1.types import (
45+
prediction_service as prediction_service_v1beta1,
4546
)
4647

4748

4849
# Use parameter signature from
4950
# https://github.com/googleapis/python-aiplatform/blob/v1.76.0/google/cloud/aiplatform_v1/services/prediction_service/client.py#L2088
5051
# to handle named vs positional args robustly
5152
def _extract_params(
52-
request: Optional[
53-
Union[prediction_service.GenerateContentRequest, dict[Any, Any]]
54-
] = None,
53+
request: prediction_service.GenerateContentRequest
54+
| prediction_service_v1beta1.GenerateContentRequest
55+
| dict[Any, Any]
56+
| None = None,
5557
*,
56-
model: Optional[str] = None,
57-
contents: Optional[MutableSequence[content.Content]] = None,
58+
model: str | None = None,
59+
contents: MutableSequence[content.Content]
60+
| MutableSequence[content_v1beta1.Content]
61+
| None = None,
5862
**_kwargs: Any,
5963
) -> GenerateContentParams:
6064
# Request vs the named parameters are mututally exclusive or the RPC will fail
@@ -86,9 +90,12 @@ def generate_content_create(
8690

8791
def traced_method(
8892
wrapped: Callable[
89-
..., GenerationResponse | Iterable[GenerationResponse]
93+
...,
94+
prediction_service.GenerateContentResponse
95+
| prediction_service_v1beta1.GenerateContentResponse,
9096
],
91-
instance: _GenerativeModel,
97+
instance: client.PredictionServiceClient
98+
| client_v1beta1.PredictionServiceClient,
9299
args: Any,
93100
kwargs: Any,
94101
):

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/utils.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from typing import (
2121
TYPE_CHECKING,
2222
Mapping,
23-
Optional,
2423
Sequence,
2524
)
2625

@@ -31,18 +30,32 @@
3130

3231
if TYPE_CHECKING:
3332
from google.cloud.aiplatform_v1.types import content, tool
33+
from google.cloud.aiplatform_v1beta1.types import (
34+
content as content_v1beta1,
35+
)
36+
from google.cloud.aiplatform_v1beta1.types import (
37+
tool as tool_v1beta1,
38+
)
3439

3540

3641
@dataclass(frozen=True)
3742
class GenerateContentParams:
3843
model: str
39-
contents: Optional[Sequence[content.Content]] = None
40-
system_instruction: Optional[content.Content | None] = None
41-
tools: Optional[Sequence[tool.Tool]] = None
42-
tool_config: Optional[tool.ToolConfig] = None
43-
labels: Optional[Mapping[str, str]] = None
44-
safety_settings: Optional[Sequence[content.SafetySetting]] = None
45-
generation_config: Optional[content.GenerationConfig] = None
44+
contents: (
45+
Sequence[content.Content] | Sequence[content_v1beta1.Content] | None
46+
) = None
47+
system_instruction: content.Content | content_v1beta1.Content | None = None
48+
tools: Sequence[tool.Tool] | Sequence[tool_v1beta1.Tool] | None = None
49+
tool_config: tool.ToolConfig | tool_v1beta1.ToolConfig | None = None
50+
labels: Mapping[str, str] | None = None
51+
safety_settings: (
52+
Sequence[content.SafetySetting]
53+
| Sequence[content_v1beta1.SafetySetting]
54+
| None
55+
) = None
56+
generation_config: (
57+
content.GenerationConfig | content_v1beta1.GenerationConfig | None
58+
) = None
4659

4760

4861
def get_genai_request_attributes(

0 commit comments

Comments
 (0)