Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if candidates is not empty before accessing content #3703

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 29 additions & 23 deletions vertexai/generative_models/_generative_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,11 +916,12 @@ def _send_message(
response_chunks=[response],
)

# Adding the request and the first response candidate to history
response_message = response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
history_delta.append(response_message)
if len(response.candidates) > 0:
# Adding the request and the first response candidate to history
response_message = response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
history_delta.append(response_message)

auto_responder_content = (
message_responder.respond_to_model_response(response=response)
Expand Down Expand Up @@ -984,12 +985,14 @@ async def _send_message_async(
response_chunks=[response],
)

# Adding the request and the first response candidate to history
response_message = response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
self._history.append(request_message)
self._history.append(response_message)
if len(response.candidates) > 0:
# Adding the request and the first response candidate to history
response_message = response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
self._history.append(request_message)
self._history.append(response_message)

return response

def _send_message_streaming(
Expand Down Expand Up @@ -1051,12 +1054,13 @@ def _send_message_streaming(
if not full_response:
return

# Adding the request and the first response candidate to history
response_message = full_response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
self._history.append(request_message)
self._history.append(response_message)
if len(full_response.candidates) > 0:
# Adding the request and the first response candidate to history
response_message = full_response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
self._history.append(request_message)
self._history.append(response_message)

async def _send_message_streaming_async(
self,
Expand Down Expand Up @@ -1117,12 +1121,14 @@ async def async_generator():
yield chunk
if not full_response:
return
# Adding the request and the first response candidate to history
response_message = full_response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
self._history.append(request_message)
self._history.append(response_message)

if len(full_response.candidates) > 0:
# Adding the request and the first response candidate to history
response_message = full_response.candidates[0].content
# Response role is NOT set by the model.
response_message.role = self._MODEL_ROLE
self._history.append(request_message)
self._history.append(response_message)

return async_generator()

Expand Down