@@ -2917,6 +2917,35 @@ class GenerateContentResponse(_common.BaseModel):
2917
2917
description = """Parsed response if response_schema is provided. Not available for streaming.""" ,
2918
2918
)
2919
2919
2920
+ @property
2921
+ def _text (self ) -> Optional [str ]:
2922
+ """Returns the concatenation of all text parts in the response.
2923
+
2924
+ This is an internal method that doesn't include logging when called.
2925
+ """
2926
+ if (
2927
+ not self .candidates
2928
+ or not self .candidates [0 ].content
2929
+ or not self .candidates [0 ].content .parts
2930
+ ):
2931
+ return None
2932
+ text = ''
2933
+ any_text_part_text = False
2934
+ non_text_parts = []
2935
+ for part in self .candidates [0 ].content .parts :
2936
+ for field_name , field_value in part .model_dump (
2937
+ exclude = {'text' , 'thought' }
2938
+ ).items ():
2939
+ if field_value is not None :
2940
+ non_text_parts .append (field_name )
2941
+ if isinstance (part .text , str ):
2942
+ if isinstance (part .thought , bool ) and part .thought :
2943
+ continue
2944
+ any_text_part_text = True
2945
+ text += part .text
2946
+ # part.text == '' is different from part.text is None
2947
+ return text if any_text_part_text else None
2948
+
2920
2949
@property
2921
2950
def text (self ) -> Optional [str ]:
2922
2951
"""Returns the concatenation of all text parts in the response."""
@@ -3037,16 +3066,16 @@ def _from_response(
3037
3066
):
3038
3067
# Pydantic schema.
3039
3068
try :
3040
- if result .text is not None :
3041
- result .parsed = response_schema .model_validate_json (result .text )
3069
+ if result ._text is not None :
3070
+ result .parsed = response_schema .model_validate_json (result ._text )
3042
3071
# may not be a valid json per stream response
3043
3072
except pydantic .ValidationError :
3044
3073
pass
3045
3074
except json .decoder .JSONDecodeError :
3046
3075
pass
3047
- elif isinstance (response_schema , EnumMeta ) and result .text is not None :
3076
+ elif isinstance (response_schema , EnumMeta ) and result ._text is not None :
3048
3077
# Enum with "application/json" returns response in double quotes.
3049
- enum_value = result .text .replace ('"' , '' )
3078
+ enum_value = result ._text .replace ('"' , '' )
3050
3079
try :
3051
3080
result .parsed = response_schema (enum_value )
3052
3081
if (
@@ -3064,8 +3093,8 @@ class Placeholder(pydantic.BaseModel):
3064
3093
placeholder : response_schema # type: ignore[valid-type]
3065
3094
3066
3095
try :
3067
- if result .text is not None :
3068
- parsed = {'placeholder' : json .loads (result .text )}
3096
+ if result ._text is not None :
3097
+ parsed = {'placeholder' : json .loads (result ._text )}
3069
3098
placeholder = Placeholder .model_validate (parsed )
3070
3099
result .parsed = placeholder .placeholder
3071
3100
except json .decoder .JSONDecodeError :
@@ -3080,8 +3109,8 @@ class Placeholder(pydantic.BaseModel):
3080
3109
# want the result converted to. So just return json.
3081
3110
# JSON schema.
3082
3111
try :
3083
- if result .text is not None :
3084
- result .parsed = json .loads (result .text )
3112
+ if result ._text is not None :
3113
+ result .parsed = json .loads (result ._text )
3085
3114
# may not be a valid json per stream response
3086
3115
except json .decoder .JSONDecodeError :
3087
3116
pass
@@ -3091,12 +3120,12 @@ class Placeholder(pydantic.BaseModel):
3091
3120
for union_type in union_types :
3092
3121
if issubclass (union_type , pydantic .BaseModel ):
3093
3122
try :
3094
- if result .text is not None :
3123
+ if result ._text is not None :
3095
3124
3096
3125
class Placeholder (pydantic .BaseModel ): # type: ignore[no-redef]
3097
3126
placeholder : response_schema # type: ignore[valid-type]
3098
3127
3099
- parsed = {'placeholder' : json .loads (result .text )}
3128
+ parsed = {'placeholder' : json .loads (result ._text )}
3100
3129
placeholder = Placeholder .model_validate (parsed )
3101
3130
result .parsed = placeholder .placeholder
3102
3131
except json .decoder .JSONDecodeError :
@@ -3105,8 +3134,8 @@ class Placeholder(pydantic.BaseModel): # type: ignore[no-redef]
3105
3134
pass
3106
3135
else :
3107
3136
try :
3108
- if result .text is not None :
3109
- result .parsed = json .loads (result .text )
3137
+ if result ._text is not None :
3138
+ result .parsed = json .loads (result ._text )
3110
3139
# may not be a valid json per stream response
3111
3140
except json .decoder .JSONDecodeError :
3112
3141
pass
0 commit comments