Skip to content

Commit 6e61ff0

Browse files
authored
VertexAI stop serializing unset fields into event (#3236)
1 parent 231d26c commit 6e61ff0

File tree

6 files changed

+151
-6
lines changed

6 files changed

+151
-6
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#3203](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3203))
1818
- Add Vertex gen AI response span attributes
1919
([#3227](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3227))
20+
- VertexAI stop serializing unset fields into event
21+
([#3236](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3236))

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ def _parts_to_any_value(
257257
return None
258258

259259
return [
260-
cast("dict[str, AnyValue]", type(part).to_dict(part)) # type: ignore[reportUnknownMemberType]
260+
cast(
261+
"dict[str, AnyValue]",
262+
type(part).to_dict(part, including_default_value_fields=False), # type: ignore[reportUnknownMemberType]
263+
)
261264
for part in parts
262265
]
263266

instrumentation-genai/opentelemetry-instrumentation-vertexai/test-requirements-0.txt

Lines changed: 1 addition & 1 deletion
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

instrumentation-genai/opentelemetry-instrumentation-vertexai/test-requirements-1.txt

Lines changed: 1 addition & 1 deletion
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
Lines changed: 108 additions & 0 deletions
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

instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/test_chat_completions.py

Lines changed: 35 additions & 3 deletions
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 (
@@ -321,10 +326,37 @@ def test_generate_content_all_events(
321326
log_exporter: InMemoryLogExporter,
322327
instrument_with_content: VertexAIInstrumentor,
323328
):
324-
model = GenerativeModel(
325-
"gemini-1.5-flash-002",
326-
system_instruction=Part.from_text("You are a clever language model"),
329+
generate_content_all_input_events(
330+
GenerativeModel(
331+
"gemini-1.5-flash-002",
332+
system_instruction=Part.from_text(
333+
"You are a clever language model"
334+
),
335+
),
336+
log_exporter,
327337
)
338+
339+
340+
@pytest.mark.vcr
341+
def test_preview_generate_content_all_input_events(
342+
log_exporter: InMemoryLogExporter,
343+
instrument_with_content: VertexAIInstrumentor,
344+
):
345+
generate_content_all_input_events(
346+
PreviewGenerativeModel(
347+
"gemini-1.5-flash-002",
348+
system_instruction=Part.from_text(
349+
"You are a clever language model"
350+
),
351+
),
352+
log_exporter,
353+
)
354+
355+
356+
def generate_content_all_input_events(
357+
model: GenerativeModel | PreviewGenerativeModel,
358+
log_exporter: InMemoryLogExporter,
359+
):
328360
model.generate_content(
329361
[
330362
Content(

0 commit comments

Comments
 (0)