Skip to content

Commit 38789cf

Browse files
committed
fix(vertexai): normalize content message roles
1 parent 20ea579 commit 38789cf

4 files changed

Lines changed: 28 additions & 20 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ def convert_content_to_message_parts(
221221
return parts
222222

223223

224-
<<<<<<< HEAD
225224
def convert_content_to_input_message(
226225
content: content.Content | content_v1beta1.Content,
227226
) -> InputMessage:
@@ -269,8 +268,6 @@ def _normalize_content_role(
269268
return role or "user"
270269

271270

272-
=======
273-
>>>>>>> e7af21e (HYBIM-623: Migrate Vertex AI instrumentation to TelemetryHandler/LLMInvocation)
274271
def _map_finish_reason(
275272
finish_reason: content.Candidate.FinishReason
276273
| content_v1beta1.Candidate.FinishReason

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ def test_generate_content(
8686
output_msgs = json.loads(attrs["gen_ai.output.messages"])
8787
assert output_msgs == [
8888
{
89-
<<<<<<< HEAD
9089
"role": "assistant",
91-
=======
92-
"role": "model",
93-
>>>>>>> e7af21e (HYBIM-623: Migrate Vertex AI instrumentation to TelemetryHandler/LLMInvocation)
9490
"parts": [{"type": "text", "content": "This is a test."}],
9591
"finish_reason": "stop",
9692
}
@@ -108,11 +104,7 @@ def test_generate_content(
108104
]
109105
assert body["gen_ai.output.messages"] == [
110106
{
111-
<<<<<<< HEAD
112107
"role": "assistant",
113-
=======
114-
"role": "model",
115-
>>>>>>> e7af21e (HYBIM-623: Migrate Vertex AI instrumentation to TelemetryHandler/LLMInvocation)
116108
"parts": [{"type": "text", "content": "This is a test."}],
117109
"finish_reason": "stop",
118110
}
@@ -392,11 +384,7 @@ def generate_content_all_input_messages(
392384
assert input_msgs[0]["parts"] == [
393385
{"type": "text", "content": "My name is OpenTelemetry"}
394386
]
395-
<<<<<<< HEAD
396387
assert input_msgs[1]["role"] == "assistant"
397-
=======
398-
assert input_msgs[1]["role"] == "model"
399-
>>>>>>> e7af21e (HYBIM-623: Migrate Vertex AI instrumentation to TelemetryHandler/LLMInvocation)
400388
assert input_msgs[1]["parts"] == [
401389
{"type": "text", "content": "Hello OpenTelemetry!"}
402390
]
@@ -412,11 +400,7 @@ def generate_content_all_input_messages(
412400
assert "gen_ai.output.messages" in attrs
413401
output_msgs = json.loads(attrs["gen_ai.output.messages"])
414402
assert len(output_msgs) == 1
415-
<<<<<<< HEAD
416403
assert output_msgs[0]["role"] == "assistant"
417-
=======
418-
assert output_msgs[0]["role"] == "model"
419-
>>>>>>> e7af21e (HYBIM-623: Migrate Vertex AI instrumentation to TelemetryHandler/LLMInvocation)
420404
assert output_msgs[0]["parts"] == [
421405
{"type": "text", "content": "OpenTelemetry, this is a test."}
422406
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_tool_events(
163163
)
164164
assert "gen_ai.request.function.0.parameters" in attrs
165165

166-
# Content on span: user text, model function_call, user tool responses, model text response
166+
# Content on span: user text, assistant function_call, tool responses, assistant text response
167167
assert "gen_ai.input.messages" in attrs
168168
input_msgs = json.loads(attrs["gen_ai.input.messages"])
169169
assert len(input_msgs) == 3

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ def test_convert_content_mixed_parts():
154154
assert parts[2].response == {"answer": "world"}
155155

156156

157+
def test_convert_content_to_input_message_normalizes_roles():
158+
model_content = content.Content(
159+
{
160+
"role": "model",
161+
"parts": [{"text": "hello"}],
162+
}
163+
)
164+
model_message = convert_content_to_input_message(model_content)
165+
assert model_message.role == "assistant"
166+
167+
tool_content = content.Content(
168+
{
169+
"role": "user",
170+
"parts": [
171+
{
172+
"function_response": {
173+
"name": "search",
174+
"response": {"answer": "world"},
175+
}
176+
}
177+
],
178+
}
179+
)
180+
tool_message = convert_content_to_input_message(tool_content)
181+
assert tool_message.role == "tool"
182+
183+
157184
def test_extract_tool_definitions():
158185
"""extract_tool_definitions converts Tool protos to dicts."""
159186
t = tool.Tool(

0 commit comments

Comments
 (0)