Skip to content

Commit c66f1e8

Browse files
committed
VertexAI stop serializing unset fields into event
1 parent d18c5fe commit c66f1e8

File tree

6 files changed

+151
-6
lines changed

6 files changed

+151
-6
lines changed

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
([#3208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3208))
1616
- VertexAI emit user, system, and assistant events
1717
([#3203](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3203))
18+
- VertexAI stop serializing unset fields into event
19+
([#3236](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3236))

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ def _parts_to_any_value(
205205
return None
206206

207207
return [
208-
cast("dict[str, AnyValue]", type(part).to_dict(part)) # type: ignore[reportUnknownMemberType]
208+
cast(
209+
"dict[str, AnyValue]",
210+
type(part).to_dict(part, including_default_value_fields=False), # type: ignore[reportUnknownMemberType]
211+
)
209212
for part in parts
210213
]

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/test-requirements-0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ docstring_parser==0.16
77
exceptiongroup==1.2.2
88
google-api-core==2.23.0
99
google-auth==2.36.0
10-
google-cloud-aiplatform==1.74.0
10+
google-cloud-aiplatform==1.79.0
1111
google-cloud-bigquery==3.27.0
1212
google-cloud-core==2.4.1
1313
google-cloud-resource-manager==1.13.1

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/test-requirements-1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ docstring_parser==0.16
88
exceptiongroup==1.2.2
99
google-api-core==2.23.0
1010
google-auth==2.36.0
11-
google-cloud-aiplatform==1.74.0
11+
google-cloud-aiplatform==1.79.0
1212
google-cloud-bigquery==3.27.0
1313
google-cloud-core==2.4.1
1414
google-cloud-resource-manager==1.13.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"contents": [
6+
{
7+
"role": "user",
8+
"parts": [
9+
{
10+
"text": "My name is OpenTelemetry"
11+
}
12+
]
13+
},
14+
{
15+
"role": "model",
16+
"parts": [
17+
{
18+
"text": "Hello OpenTelemetry!"
19+
}
20+
]
21+
},
22+
{
23+
"role": "user",
24+
"parts": [
25+
{
26+
"text": "Address me by name and say this is a test"
27+
}
28+
]
29+
}
30+
],
31+
"systemInstruction": {
32+
"role": "user",
33+
"parts": [
34+
{
35+
"text": "You are a clever language model"
36+
}
37+
]
38+
}
39+
}
40+
headers:
41+
Accept:
42+
- '*/*'
43+
Accept-Encoding:
44+
- gzip, deflate
45+
Connection:
46+
- keep-alive
47+
Content-Length:
48+
- '548'
49+
Content-Type:
50+
- application/json
51+
User-Agent:
52+
- python-requests/2.32.3
53+
method: POST
54+
uri: https://us-central1-aiplatform.googleapis.com/v1beta1/projects/fake-project/locations/us-central1/publishers/google/models/gemini-1.5-flash-002:generateContent?%24alt=json%3Benum-encoding%3Dint
55+
response:
56+
body:
57+
string: |-
58+
{
59+
"candidates": [
60+
{
61+
"content": {
62+
"role": "model",
63+
"parts": [
64+
{
65+
"text": "OpenTelemetry, this is a test.\n"
66+
}
67+
]
68+
},
69+
"finishReason": 1,
70+
"avgLogprobs": -1.1655389850299496e-06
71+
}
72+
],
73+
"usageMetadata": {
74+
"promptTokenCount": 25,
75+
"candidatesTokenCount": 9,
76+
"totalTokenCount": 34,
77+
"promptTokensDetails": [
78+
{
79+
"modality": 1,
80+
"tokenCount": 25
81+
}
82+
],
83+
"candidatesTokensDetails": [
84+
{
85+
"modality": 1,
86+
"tokenCount": 9
87+
}
88+
]
89+
},
90+
"modelVersion": "gemini-1.5-flash-002",
91+
"createTime": "2025-02-03T22:15:35.089616Z",
92+
"responseId": "B0ChZ5C8BbXInvgP19PTyA4"
93+
}
94+
headers:
95+
Content-Type:
96+
- application/json; charset=UTF-8
97+
Transfer-Encoding:
98+
- chunked
99+
Vary:
100+
- Origin
101+
- X-Origin
102+
- Referer
103+
content-length:
104+
- '715'
105+
status:
106+
code: 200
107+
message: OK
108+
version: 1

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/test_chat_completions.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24
from google.api_core.exceptions import BadRequest, NotFound
35
from vertexai.generative_models import (
@@ -6,6 +8,9 @@
68
GenerativeModel,
79
Part,
810
)
11+
from vertexai.preview.generative_models import (
12+
GenerativeModel as PreviewGenerativeModel,
13+
)
914

1015
from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
1116
from opentelemetry.sdk._logs._internal.export.in_memory_log_exporter import (
@@ -278,10 +283,37 @@ def test_generate_content_all_input_events(
278283
log_exporter: InMemoryLogExporter,
279284
instrument_with_content: VertexAIInstrumentor,
280285
):
281-
model = GenerativeModel(
282-
"gemini-1.5-flash-002",
283-
system_instruction=Part.from_text("You are a clever language model"),
286+
generate_content_all_input_events(
287+
GenerativeModel(
288+
"gemini-1.5-flash-002",
289+
system_instruction=Part.from_text(
290+
"You are a clever language model"
291+
),
292+
),
293+
log_exporter,
284294
)
295+
296+
297+
@pytest.mark.vcr
298+
def test_preview_generate_content_all_input_events(
299+
log_exporter: InMemoryLogExporter,
300+
instrument_with_content: VertexAIInstrumentor,
301+
):
302+
generate_content_all_input_events(
303+
PreviewGenerativeModel(
304+
"gemini-1.5-flash-002",
305+
system_instruction=Part.from_text(
306+
"You are a clever language model"
307+
),
308+
),
309+
log_exporter,
310+
)
311+
312+
313+
def generate_content_all_input_events(
314+
model: GenerativeModel | PreviewGenerativeModel,
315+
log_exporter: InMemoryLogExporter,
316+
):
285317
model.generate_content(
286318
[
287319
Content(

0 commit comments

Comments
 (0)