Skip to content

[vertexai]: ignore empty AIMessage from previous blocked responses #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libs/vertexai/langchain_google_vertexai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ def _convert_to_parts(message: BaseMessage) -> List[Part]:
prev_ai_message = message
role = "model"

# Previous blocked messages will have empty content which should be ignored
if not message.content and message.response_metadata.get(
"is_blocked", False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be message.response_metadata.get("is_blocked", True) instead maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that if someone does this by accident it's better to let the API yell at them than sanitize the data

model.invoke([AIMessage(content="")])

But whatever you think is best, maybe there are other cases where the content is empty

):
logger.warning("Ignoring blocked AIMessage with empty content")
continue

parts = []
if message.content:
parts = _convert_to_parts(message)
Expand Down
32 changes: 32 additions & 0 deletions libs/vertexai/tests/integration_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,38 @@ def test_vertexai_single_call_with_no_system_messages() -> None:
assert isinstance(response.content, str)


@pytest.mark.release
def test_vertexai_single_call_previous_blocked_response() -> None:
"""If a previous call was blocked, the AIMessage will have empty content which
should be ignored."""

model = ChatVertexAI(model_name=_DEFAULT_MODEL_NAME, rate_limiter=rate_limiter)
text_question2 = "How much is 3+3?"
# Previous blocked response included in history. This can happen with a LangGraph
# ReAct agent.
message1 = AIMessage(
content="",
response_metadata={
"is_blocked": True,
"safety_ratings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability_label": "MEDIUM",
"probability_score": 0.33039191365242004,
"blocked": True,
"severity": "HARM_SEVERITY_MEDIUM",
"severity_score": 0.2782268822193146,
},
],
"finish_reason": "SAFETY",
},
)
message2 = HumanMessage(content=text_question2)
response = model([message1, message2])
assert isinstance(response, AIMessage)
assert isinstance(response.content, str)


@pytest.mark.release
@pytest.mark.parametrize("model_name", model_names_to_test)
def test_get_num_tokens_from_messages(model_name: str) -> None:
Expand Down