25
25
Literal ,
26
26
Optional ,
27
27
Sequence ,
28
+ Tuple ,
28
29
Union ,
29
30
)
30
31
import warnings
@@ -859,6 +860,9 @@ class TextGenerationResponse:
859
860
Attributes:
860
861
text: The generated text
861
862
is_blocked: Whether the the request was blocked.
863
+ errors: The error codes indicate why the response was blocked.
864
+ Learn more information about safety errors here:
865
+ this documentation https://cloud.google.com/vertex-ai/docs/generative-ai/learn/responsible-ai#safety_errors
862
866
safety_attributes: Scores for safety attributes.
863
867
Learn more about the safety attributes here:
864
868
https://cloud.google.com/vertex-ai/docs/generative-ai/learn/responsible-ai#safety_attribute_descriptions
@@ -870,6 +874,7 @@ class TextGenerationResponse:
870
874
text : str
871
875
_prediction_response : Any
872
876
is_blocked : bool = False
877
+ errors : Tuple [int ] = tuple ()
873
878
safety_attributes : Dict [str , float ] = dataclasses .field (default_factory = dict )
874
879
grounding_metadata : Optional [GroundingMetadata ] = None
875
880
@@ -882,6 +887,7 @@ def __repr__(self):
882
887
"TextGenerationResponse("
883
888
f"text={ self .text !r} "
884
889
f", is_blocked={ self .is_blocked !r} "
890
+ f", errors={ self .errors !r} "
885
891
f", safety_attributes={ self .safety_attributes !r} "
886
892
f", grounding_metadata={ self .grounding_metadata !r} "
887
893
")"
@@ -891,6 +897,7 @@ def __repr__(self):
891
897
"TextGenerationResponse("
892
898
f"text={ self .text !r} "
893
899
f", is_blocked={ self .is_blocked !r} "
900
+ f", errors={ self .errors !r} "
894
901
f", safety_attributes={ self .safety_attributes !r} "
895
902
")"
896
903
)
@@ -1216,10 +1223,13 @@ def _parse_text_generation_model_response(
1216
1223
prediction = prediction_response .predictions [prediction_idx ]
1217
1224
safety_attributes_dict = prediction .get ("safetyAttributes" , {})
1218
1225
grounding_metadata_dict = prediction .get ("groundingMetadata" , {})
1226
+ errors_list = safety_attributes_dict .get ("errors" , [])
1227
+ errors = tuple (map (int , errors_list ))
1219
1228
return TextGenerationResponse (
1220
1229
text = prediction ["content" ],
1221
1230
_prediction_response = prediction_response ,
1222
1231
is_blocked = safety_attributes_dict .get ("blocked" , False ),
1232
+ errors = errors ,
1223
1233
safety_attributes = dict (
1224
1234
zip (
1225
1235
safety_attributes_dict .get ("categories" ) or [],
@@ -1251,6 +1261,7 @@ def _parse_text_generation_model_multi_candidate_response(
1251
1261
text = candidates [0 ].text ,
1252
1262
_prediction_response = prediction_response ,
1253
1263
is_blocked = candidates [0 ].is_blocked ,
1264
+ errors = candidates [0 ].errors ,
1254
1265
safety_attributes = candidates [0 ].safety_attributes ,
1255
1266
grounding_metadata = candidates [0 ].grounding_metadata ,
1256
1267
candidates = candidates ,
@@ -2090,13 +2101,16 @@ def _parse_chat_prediction_response(
2090
2101
grounding_metadata_list = prediction .get ("groundingMetadata" )
2091
2102
for candidate_idx in range (candidate_count ):
2092
2103
safety_attributes = prediction ["safetyAttributes" ][candidate_idx ]
2104
+ errors_list = safety_attributes .get ("errors" , [])
2105
+ errors = tuple (map (int , errors_list ))
2093
2106
grounding_metadata_dict = {}
2094
2107
if grounding_metadata_list and grounding_metadata_list [candidate_idx ]:
2095
2108
grounding_metadata_dict = grounding_metadata_list [candidate_idx ]
2096
2109
candidate_response = TextGenerationResponse (
2097
2110
text = prediction ["candidates" ][candidate_idx ]["content" ],
2098
2111
_prediction_response = prediction_response ,
2099
2112
is_blocked = safety_attributes .get ("blocked" , False ),
2113
+ errors = errors ,
2100
2114
safety_attributes = dict (
2101
2115
zip (
2102
2116
# Unlike with normal prediction, in streaming prediction
@@ -2112,6 +2126,7 @@ def _parse_chat_prediction_response(
2112
2126
text = candidates [0 ].text ,
2113
2127
_prediction_response = prediction_response ,
2114
2128
is_blocked = candidates [0 ].is_blocked ,
2129
+ errors = candidates [0 ].errors ,
2115
2130
safety_attributes = candidates [0 ].safety_attributes ,
2116
2131
grounding_metadata = candidates [0 ].grounding_metadata ,
2117
2132
candidates = candidates ,
0 commit comments