Skip to content

Commit fb85af6

Browse files
authored
fix(vertex): ToolMessage is translated to function role which does not exist . (#1238)
1 parent 33f015b commit fb85af6

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

libs/vertexai/langchain_google_vertexai/chat_models.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def _convert_to_parts(message: BaseMessage) -> List[Part]:
443443
vertex_messages.append(Content(role=role, parts=parts))
444444
elif isinstance(message, FunctionMessage):
445445
prev_ai_message = None
446-
role = "function"
446+
role = "user"
447447

448448
part = Part(
449449
function_response=FunctionResponse(
@@ -453,9 +453,7 @@ def _convert_to_parts(message: BaseMessage) -> List[Part]:
453453
parts = [part]
454454
if vertex_messages:
455455
prev_content = vertex_messages[-1]
456-
prev_content_is_function = (
457-
prev_content and prev_content.role == "function"
458-
)
456+
prev_content_is_function = prev_content and prev_content.role == "user"
459457
if prev_content_is_function:
460458
prev_parts = list(prev_content.parts)
461459
prev_parts.extend(parts)
@@ -465,7 +463,7 @@ def _convert_to_parts(message: BaseMessage) -> List[Part]:
465463

466464
vertex_messages.append(Content(role=role, parts=parts))
467465
elif isinstance(message, ToolMessage):
468-
role = "function"
466+
role = "user"
469467

470468
# message.name can be null for ToolMessage
471469
name = message.name
@@ -527,9 +525,9 @@ def _parse_content(raw_content: str | Dict[Any, Any]) -> Dict[Any, Any]:
527525
parts = [part]
528526

529527
prev_content = vertex_messages[-1]
530-
prev_content_is_function = prev_content and prev_content.role == "function"
528+
prev_content_is_tool_response = prev_content and prev_content.role == "user"
531529

532-
if prev_content_is_function:
530+
if prev_content_is_tool_response:
533531
prev_parts = list(prev_content.parts)
534532
prev_parts.extend(parts)
535533
# replacing last message

libs/vertexai/tests/unit_tests/test_chat_models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def test_parse_history_gemini_function_empty_list() -> None:
333333
name=tool_call_1["name"], args=tool_call_1["args"]
334334
)
335335

336-
assert history[2].role == "function"
336+
assert history[2].role == "user"
337337
assert history[2].parts[0].function_response == FunctionResponse(
338338
name=fn_name_1,
339339
response={"content": ""},
@@ -414,7 +414,7 @@ def test_parse_history_gemini_function() -> None:
414414
name=tool_call_2["name"], args=tool_call_2["args"]
415415
)
416416

417-
assert history[2].role == "function"
417+
assert history[2].role == "user"
418418
assert history[2].parts[0].function_response == FunctionResponse(
419419
name=fn_name_1,
420420
response={"content": message3.content},
@@ -429,7 +429,7 @@ def test_parse_history_gemini_function() -> None:
429429
name=tool_call_3["name"], args=tool_call_3["args"]
430430
)
431431

432-
assert history[4].role == "function"
432+
assert history[4].role == "user"
433433
assert history[4].parts[0].function_response == FunctionResponse(
434434
name=fn_name_3,
435435
response={"content": message6.content},
@@ -1246,7 +1246,7 @@ def test_multiple_fc() -> None:
12461246
)
12471247
),
12481248
],
1249-
role="function",
1249+
role="user",
12501250
),
12511251
]
12521252
assert history == expected
@@ -1549,7 +1549,7 @@ def test_thought_signature() -> None:
15491549
],
15501550
),
15511551
Content(
1552-
role="function",
1552+
role="user",
15531553
parts=[
15541554
Part(
15551555
function_response=FunctionResponse(

0 commit comments

Comments
 (0)