Skip to content

Commit c881998

Browse files
Ark-kuncopybara-github
authored andcommitted
fix: GenAI - Fixed response validation error during streaming
Previously, when the response was blocked by safety, the exception was non-informative. PiperOrigin-RevId: 622291778
1 parent 9bb687c commit c881998

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

vertexai/generative_models/_generative_models.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1022,17 +1022,17 @@ def _send_message_streaming(
10221022
full_response = None
10231023
for chunk in stream:
10241024
chunks.append(chunk)
1025-
if full_response:
1026-
_append_response(full_response, chunk)
1027-
else:
1028-
full_response = chunk
10291025
# By default we're not adding incomplete interactions to history.
10301026
if self._response_validator is not None:
10311027
self._response_validator(
10321028
response=chunk,
10331029
request_contents=request_history,
10341030
response_chunks=chunks,
10351031
)
1032+
if full_response:
1033+
_append_response(full_response, chunk)
1034+
else:
1035+
full_response = chunk
10361036
yield chunk
10371037
if not full_response:
10381038
return
@@ -1089,18 +1089,17 @@ async def async_generator():
10891089
full_response = None
10901090
async for chunk in stream:
10911091
chunks.append(chunk)
1092-
if full_response:
1093-
_append_response(full_response, chunk)
1094-
else:
1095-
full_response = chunk
10961092
# By default we're not adding incomplete interactions to history.
10971093
if self._response_validator is not None:
10981094
self._response_validator(
10991095
response=chunk,
11001096
request_contents=request_history,
11011097
response_chunks=chunks,
11021098
)
1103-
1099+
if full_response:
1100+
_append_response(full_response, chunk)
1101+
else:
1102+
full_response = chunk
11041103
yield chunk
11051104
if not full_response:
11061105
return

0 commit comments

Comments
 (0)