google-genai 1.50.1__py3-none-any.whl → 1.51.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
google/genai/types.py CHANGED
@@ -116,6 +116,15 @@ else:
116
116
  logger = logging.getLogger('google_genai.types')
117
117
  _from_json_schema_warning_logged = False
118
118
  _json_schema_warning_logged = False
119
+ _response_text_warning_logged = False
120
+ _response_text_non_text_warning_logged = False
121
+ _response_parts_warning_logged = False
122
+ _response_function_calls_warning_logged = False
123
+ _response_executable_code_warning_logged = False
124
+ _response_code_execution_warning_logged = False
125
+ _live_server_text_warning_logged = False
126
+ _live_server_data_warning_logged = False
127
+
119
128
 
120
129
  T = typing.TypeVar('T', bound='GenerateContentResponse')
121
130
 
@@ -257,6 +266,17 @@ class PhishBlockThreshold(_common.CaseInSensitiveEnum):
257
266
  """Blocks Extremely high confidence URL that is risky."""
258
267
 
259
268
 
269
+ class ThinkingLevel(_common.CaseInSensitiveEnum):
270
+ """The level of thoughts tokens that the model should generate."""
271
+
272
+ THINKING_LEVEL_UNSPECIFIED = 'THINKING_LEVEL_UNSPECIFIED'
273
+ """Default value."""
274
+ LOW = 'LOW'
275
+ """Low thinking level."""
276
+ HIGH = 'HIGH'
277
+ """High thinking level."""
278
+
279
+
260
280
  class HarmCategory(_common.CaseInSensitiveEnum):
261
281
  """Harm category."""
262
282
 
@@ -542,6 +562,19 @@ class TuningTask(_common.CaseInSensitiveEnum):
542
562
  """Tuning task for reference to video."""
543
563
 
544
564
 
565
+ class PartMediaResolutionLevel(_common.CaseInSensitiveEnum):
566
+ """The tokenization quality used for given media."""
567
+
568
+ MEDIA_RESOLUTION_UNSPECIFIED = 'MEDIA_RESOLUTION_UNSPECIFIED'
569
+ """Media resolution has not been set."""
570
+ MEDIA_RESOLUTION_LOW = 'MEDIA_RESOLUTION_LOW'
571
+ """Media resolution set to low."""
572
+ MEDIA_RESOLUTION_MEDIUM = 'MEDIA_RESOLUTION_MEDIUM'
573
+ """Media resolution set to medium."""
574
+ MEDIA_RESOLUTION_HIGH = 'MEDIA_RESOLUTION_HIGH'
575
+ """Media resolution set to high."""
576
+
577
+
545
578
  class JSONSchemaType(Enum):
546
579
  """The type of the data supported by JSON Schema.
547
580
 
@@ -921,39 +954,34 @@ class LiveMusicPlaybackControl(_common.CaseInSensitiveEnum):
921
954
  Retains the current prompts and config."""
922
955
 
923
956
 
924
- class FunctionCall(_common.BaseModel):
925
- """A function call."""
957
+ class PartMediaResolution(_common.BaseModel):
958
+ """Media resolution for the input media."""
926
959
 
927
- id: Optional[str] = Field(
960
+ level: Optional[PartMediaResolutionLevel] = Field(
928
961
  default=None,
929
- description="""The unique id of the function call. If populated, the client to execute the
930
- `function_call` and return the response with the matching `id`.""",
962
+ description="""The tokenization quality used for given media.
963
+ """,
931
964
  )
932
- args: Optional[dict[str, Any]] = Field(
965
+ num_tokens: Optional[int] = Field(
933
966
  default=None,
934
- description="""Optional. The function parameters and values in JSON object format. See [FunctionDeclaration.parameters] for parameter details.""",
935
- )
936
- name: Optional[str] = Field(
937
- default=None,
938
- description="""Optional. The name of the function to call. Matches [FunctionDeclaration.name].""",
967
+ description="""Specifies the required sequence length for media tokenization.
968
+ """,
939
969
  )
940
970
 
941
971
 
942
- class FunctionCallDict(TypedDict, total=False):
943
- """A function call."""
972
+ class PartMediaResolutionDict(TypedDict, total=False):
973
+ """Media resolution for the input media."""
944
974
 
945
- id: Optional[str]
946
- """The unique id of the function call. If populated, the client to execute the
947
- `function_call` and return the response with the matching `id`."""
948
-
949
- args: Optional[dict[str, Any]]
950
- """Optional. The function parameters and values in JSON object format. See [FunctionDeclaration.parameters] for parameter details."""
975
+ level: Optional[PartMediaResolutionLevel]
976
+ """The tokenization quality used for given media.
977
+ """
951
978
 
952
- name: Optional[str]
953
- """Optional. The name of the function to call. Matches [FunctionDeclaration.name]."""
979
+ num_tokens: Optional[int]
980
+ """Specifies the required sequence length for media tokenization.
981
+ """
954
982
 
955
983
 
956
- FunctionCallOrDict = Union[FunctionCall, FunctionCallDict]
984
+ PartMediaResolutionOrDict = Union[PartMediaResolution, PartMediaResolutionDict]
957
985
 
958
986
 
959
987
  class CodeExecutionResult(_common.BaseModel):
@@ -1056,6 +1084,111 @@ class FileDataDict(TypedDict, total=False):
1056
1084
  FileDataOrDict = Union[FileData, FileDataDict]
1057
1085
 
1058
1086
 
1087
+ class PartialArg(_common.BaseModel):
1088
+ """Partial argument value of the function call.
1089
+
1090
+ This data type is not supported in Gemini API.
1091
+ """
1092
+
1093
+ null_value: Optional[Literal['NULL_VALUE']] = Field(
1094
+ default=None, description="""Optional. Represents a null value."""
1095
+ )
1096
+ number_value: Optional[float] = Field(
1097
+ default=None, description="""Optional. Represents a double value."""
1098
+ )
1099
+ string_value: Optional[str] = Field(
1100
+ default=None, description="""Optional. Represents a string value."""
1101
+ )
1102
+ bool_value: Optional[bool] = Field(
1103
+ default=None, description="""Optional. Represents a boolean value."""
1104
+ )
1105
+ json_path: Optional[str] = Field(
1106
+ default=None,
1107
+ description="""Required. A JSON Path (RFC 9535) to the argument being streamed. https://datatracker.ietf.org/doc/html/rfc9535. e.g. "$.foo.bar[0].data".""",
1108
+ )
1109
+ will_continue: Optional[bool] = Field(
1110
+ default=None,
1111
+ description="""Optional. Whether this is not the last part of the same json_path. If true, another PartialArg message for the current json_path is expected to follow.""",
1112
+ )
1113
+
1114
+
1115
+ class PartialArgDict(TypedDict, total=False):
1116
+ """Partial argument value of the function call.
1117
+
1118
+ This data type is not supported in Gemini API.
1119
+ """
1120
+
1121
+ null_value: Optional[Literal['NULL_VALUE']]
1122
+ """Optional. Represents a null value."""
1123
+
1124
+ number_value: Optional[float]
1125
+ """Optional. Represents a double value."""
1126
+
1127
+ string_value: Optional[str]
1128
+ """Optional. Represents a string value."""
1129
+
1130
+ bool_value: Optional[bool]
1131
+ """Optional. Represents a boolean value."""
1132
+
1133
+ json_path: Optional[str]
1134
+ """Required. A JSON Path (RFC 9535) to the argument being streamed. https://datatracker.ietf.org/doc/html/rfc9535. e.g. "$.foo.bar[0].data"."""
1135
+
1136
+ will_continue: Optional[bool]
1137
+ """Optional. Whether this is not the last part of the same json_path. If true, another PartialArg message for the current json_path is expected to follow."""
1138
+
1139
+
1140
+ PartialArgOrDict = Union[PartialArg, PartialArgDict]
1141
+
1142
+
1143
+ class FunctionCall(_common.BaseModel):
1144
+ """A function call."""
1145
+
1146
+ id: Optional[str] = Field(
1147
+ default=None,
1148
+ description="""The unique id of the function call. If populated, the client to execute the
1149
+ `function_call` and return the response with the matching `id`.""",
1150
+ )
1151
+ args: Optional[dict[str, Any]] = Field(
1152
+ default=None,
1153
+ description="""Optional. The function parameters and values in JSON object format. See [FunctionDeclaration.parameters] for parameter details.""",
1154
+ )
1155
+ name: Optional[str] = Field(
1156
+ default=None,
1157
+ description="""Optional. The name of the function to call. Matches [FunctionDeclaration.name].""",
1158
+ )
1159
+ partial_args: Optional[list[PartialArg]] = Field(
1160
+ default=None,
1161
+ description="""Optional. The partial argument value of the function call. If provided, represents the arguments/fields that are streamed incrementally. This field is not supported in Gemini API.""",
1162
+ )
1163
+ will_continue: Optional[bool] = Field(
1164
+ default=None,
1165
+ description="""Optional. Whether this is the last part of the FunctionCall. If true, another partial message for the current FunctionCall is expected to follow. This field is not supported in Gemini API.""",
1166
+ )
1167
+
1168
+
1169
+ class FunctionCallDict(TypedDict, total=False):
1170
+ """A function call."""
1171
+
1172
+ id: Optional[str]
1173
+ """The unique id of the function call. If populated, the client to execute the
1174
+ `function_call` and return the response with the matching `id`."""
1175
+
1176
+ args: Optional[dict[str, Any]]
1177
+ """Optional. The function parameters and values in JSON object format. See [FunctionDeclaration.parameters] for parameter details."""
1178
+
1179
+ name: Optional[str]
1180
+ """Optional. The name of the function to call. Matches [FunctionDeclaration.name]."""
1181
+
1182
+ partial_args: Optional[list[PartialArgDict]]
1183
+ """Optional. The partial argument value of the function call. If provided, represents the arguments/fields that are streamed incrementally. This field is not supported in Gemini API."""
1184
+
1185
+ will_continue: Optional[bool]
1186
+ """Optional. Whether this is the last part of the FunctionCall. If true, another partial message for the current FunctionCall is expected to follow. This field is not supported in Gemini API."""
1187
+
1188
+
1189
+ FunctionCallOrDict = Union[FunctionCall, FunctionCallDict]
1190
+
1191
+
1059
1192
  class FunctionResponseBlob(_common.BaseModel):
1060
1193
  """Raw media bytes for function response.
1061
1194
 
@@ -1070,6 +1203,11 @@ class FunctionResponseBlob(_common.BaseModel):
1070
1203
  data: Optional[bytes] = Field(
1071
1204
  default=None, description="""Required. Inline media bytes."""
1072
1205
  )
1206
+ display_name: Optional[str] = Field(
1207
+ default=None,
1208
+ description="""Optional. Display name of the blob.
1209
+ Used to provide a label or filename to distinguish blobs.""",
1210
+ )
1073
1211
 
1074
1212
 
1075
1213
  class FunctionResponseBlobDict(TypedDict, total=False):
@@ -1085,6 +1223,10 @@ class FunctionResponseBlobDict(TypedDict, total=False):
1085
1223
  data: Optional[bytes]
1086
1224
  """Required. Inline media bytes."""
1087
1225
 
1226
+ display_name: Optional[str]
1227
+ """Optional. Display name of the blob.
1228
+ Used to provide a label or filename to distinguish blobs."""
1229
+
1088
1230
 
1089
1231
  FunctionResponseBlobOrDict = Union[
1090
1232
  FunctionResponseBlob, FunctionResponseBlobDict
@@ -1101,6 +1243,11 @@ class FunctionResponseFileData(_common.BaseModel):
1101
1243
  default=None,
1102
1244
  description="""Required. The IANA standard MIME type of the source data.""",
1103
1245
  )
1246
+ display_name: Optional[str] = Field(
1247
+ default=None,
1248
+ description="""Optional. Display name of the file.
1249
+ Used to provide a label or filename to distinguish files.""",
1250
+ )
1104
1251
 
1105
1252
 
1106
1253
  class FunctionResponseFileDataDict(TypedDict, total=False):
@@ -1112,6 +1259,10 @@ class FunctionResponseFileDataDict(TypedDict, total=False):
1112
1259
  mime_type: Optional[str]
1113
1260
  """Required. The IANA standard MIME type of the source data."""
1114
1261
 
1262
+ display_name: Optional[str]
1263
+ """Optional. Display name of the file.
1264
+ Used to provide a label or filename to distinguish files."""
1265
+
1115
1266
 
1116
1267
  FunctionResponseFileDataOrDict = Union[
1117
1268
  FunctionResponseFileData, FunctionResponseFileDataDict
@@ -1351,11 +1502,10 @@ class Part(_common.BaseModel):
1351
1502
  instance is considered invalid.
1352
1503
  """
1353
1504
 
1354
- function_call: Optional[FunctionCall] = Field(
1505
+ media_resolution: Optional[PartMediaResolution] = Field(
1355
1506
  default=None,
1356
- description="""A predicted [FunctionCall] returned from the model that contains a string
1357
- representing the [FunctionDeclaration.name] and a structured JSON object
1358
- containing the parameters and their values.""",
1507
+ description="""Media resolution for the input media.
1508
+ """,
1359
1509
  )
1360
1510
  code_execution_result: Optional[CodeExecutionResult] = Field(
1361
1511
  default=None,
@@ -1368,6 +1518,10 @@ class Part(_common.BaseModel):
1368
1518
  file_data: Optional[FileData] = Field(
1369
1519
  default=None, description="""Optional. URI based data."""
1370
1520
  )
1521
+ function_call: Optional[FunctionCall] = Field(
1522
+ default=None,
1523
+ description="""Optional. A predicted [FunctionCall] returned from the model that contains a string representing the [FunctionDeclaration.name] with the parameters and their values.""",
1524
+ )
1371
1525
  function_response: Optional[FunctionResponse] = Field(
1372
1526
  default=None,
1373
1527
  description="""Optional. The result output of a [FunctionCall] that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing any output from the function call. It is used as context to the model.""",
@@ -1472,9 +1626,31 @@ class Part(_common.BaseModel):
1472
1626
  return None
1473
1627
  return self.inline_data.as_image()
1474
1628
 
1629
+ @classmethod
1630
+ def _t_part_media_resolution(
1631
+ cls,
1632
+ part_media_resolution: Union[
1633
+ 'PartMediaResolutionOrDict', 'PartMediaResolutionLevel', str
1634
+ ],
1635
+ ) -> PartMediaResolution:
1636
+ if isinstance(part_media_resolution, str):
1637
+ part_media_resolution = PartMediaResolution(level=part_media_resolution)
1638
+ elif isinstance(part_media_resolution, PartMediaResolutionLevel):
1639
+ part_media_resolution = PartMediaResolution(level=part_media_resolution)
1640
+ elif isinstance(part_media_resolution, dict):
1641
+ part_media_resolution = PartMediaResolution(**part_media_resolution)
1642
+
1643
+ return part_media_resolution
1644
+
1475
1645
  @classmethod
1476
1646
  def from_uri(
1477
- cls, *, file_uri: str, mime_type: Optional[str] = None
1647
+ cls,
1648
+ *,
1649
+ file_uri: str,
1650
+ mime_type: Optional[str] = None,
1651
+ media_resolution: Optional[
1652
+ Union['PartMediaResolutionOrDict', 'PartMediaResolutionLevel', str]
1653
+ ] = None,
1478
1654
  ) -> 'Part':
1479
1655
  """Creates a Part from a file uri.
1480
1656
 
@@ -1489,20 +1665,33 @@ class Part(_common.BaseModel):
1489
1665
  mime_type, _ = mimetypes.guess_type(file_uri)
1490
1666
  if not mime_type:
1491
1667
  raise ValueError(f'Failed to determine mime type for file: {file_uri}')
1668
+ if media_resolution is not None:
1669
+ media_resolution = cls._t_part_media_resolution(media_resolution)
1492
1670
  file_data = FileData(file_uri=file_uri, mime_type=mime_type)
1493
- return cls(file_data=file_data)
1671
+ return cls(file_data=file_data, media_resolution=media_resolution)
1494
1672
 
1495
1673
  @classmethod
1496
1674
  def from_text(cls, *, text: str) -> 'Part':
1497
1675
  return cls(text=text)
1498
1676
 
1499
1677
  @classmethod
1500
- def from_bytes(cls, *, data: bytes, mime_type: str) -> 'Part':
1678
+ def from_bytes(
1679
+ cls,
1680
+ *,
1681
+ data: bytes,
1682
+ mime_type: str,
1683
+ media_resolution: Optional[
1684
+ Union['PartMediaResolutionOrDict', 'PartMediaResolutionLevel', str]
1685
+ ] = None,
1686
+ ) -> 'Part':
1501
1687
  inline_data = Blob(
1502
1688
  data=data,
1503
1689
  mime_type=mime_type,
1504
1690
  )
1505
- return cls(inline_data=inline_data)
1691
+ if media_resolution is not None:
1692
+ media_resolution = cls._t_part_media_resolution(media_resolution)
1693
+
1694
+ return cls(inline_data=inline_data, media_resolution=media_resolution)
1506
1695
 
1507
1696
  @classmethod
1508
1697
  def from_function_call(cls, *, name: str, args: dict[str, Any]) -> 'Part':
@@ -1543,10 +1732,9 @@ class PartDict(TypedDict, total=False):
1543
1732
  instance is considered invalid.
1544
1733
  """
1545
1734
 
1546
- function_call: Optional[FunctionCallDict]
1547
- """A predicted [FunctionCall] returned from the model that contains a string
1548
- representing the [FunctionDeclaration.name] and a structured JSON object
1549
- containing the parameters and their values."""
1735
+ media_resolution: Optional[PartMediaResolutionDict]
1736
+ """Media resolution for the input media.
1737
+ """
1550
1738
 
1551
1739
  code_execution_result: Optional[CodeExecutionResultDict]
1552
1740
  """Optional. Result of executing the [ExecutableCode]."""
@@ -1557,6 +1745,9 @@ class PartDict(TypedDict, total=False):
1557
1745
  file_data: Optional[FileDataDict]
1558
1746
  """Optional. URI based data."""
1559
1747
 
1748
+ function_call: Optional[FunctionCallDict]
1749
+ """Optional. A predicted [FunctionCall] returned from the model that contains a string representing the [FunctionDeclaration.name] with the parameters and their values."""
1750
+
1560
1751
  function_response: Optional[FunctionResponseDict]
1561
1752
  """Optional. The result output of a [FunctionCall] that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing any output from the function call. It is used as context to the model."""
1562
1753
 
@@ -4042,6 +4233,10 @@ class FunctionCallingConfig(_common.BaseModel):
4042
4233
  default=None,
4043
4234
  description="""Optional. Function names to call. Only set when the Mode is ANY. Function names should match [FunctionDeclaration.name]. With mode set to ANY, model will predict a function call from the set of function names provided.""",
4044
4235
  )
4236
+ stream_function_call_arguments: Optional[bool] = Field(
4237
+ default=None,
4238
+ description="""Optional. When set to true, arguments of a single function call will be streamed out in multiple parts/contents/responses. Partial parameter results will be returned in the [FunctionCall.partial_args] field. This field is not supported in Gemini API.""",
4239
+ )
4045
4240
 
4046
4241
 
4047
4242
  class FunctionCallingConfigDict(TypedDict, total=False):
@@ -4053,6 +4248,9 @@ class FunctionCallingConfigDict(TypedDict, total=False):
4053
4248
  allowed_function_names: Optional[list[str]]
4054
4249
  """Optional. Function names to call. Only set when the Mode is ANY. Function names should match [FunctionDeclaration.name]. With mode set to ANY, model will predict a function call from the set of function names provided."""
4055
4250
 
4251
+ stream_function_call_arguments: Optional[bool]
4252
+ """Optional. When set to true, arguments of a single function call will be streamed out in multiple parts/contents/responses. Partial parameter results will be returned in the [FunctionCall.partial_args] field. This field is not supported in Gemini API."""
4253
+
4056
4254
 
4057
4255
  FunctionCallingConfigOrDict = Union[
4058
4256
  FunctionCallingConfig, FunctionCallingConfigDict
@@ -4223,6 +4421,10 @@ class ThinkingConfig(_common.BaseModel):
4223
4421
  description="""Indicates the thinking budget in tokens. 0 is DISABLED. -1 is AUTOMATIC. The default values and allowed ranges are model dependent.
4224
4422
  """,
4225
4423
  )
4424
+ thinking_level: Optional[ThinkingLevel] = Field(
4425
+ default=None,
4426
+ description="""Optional. The level of thoughts tokens that the model should generate.""",
4427
+ )
4226
4428
 
4227
4429
 
4228
4430
  class ThinkingConfigDict(TypedDict, total=False):
@@ -4236,6 +4438,9 @@ class ThinkingConfigDict(TypedDict, total=False):
4236
4438
  """Indicates the thinking budget in tokens. 0 is DISABLED. -1 is AUTOMATIC. The default values and allowed ranges are model dependent.
4237
4439
  """
4238
4440
 
4441
+ thinking_level: Optional[ThinkingLevel]
4442
+ """Optional. The level of thoughts tokens that the model should generate."""
4443
+
4239
4444
 
4240
4445
  ThinkingConfigOrDict = Union[ThinkingConfig, ThinkingConfigDict]
4241
4446
 
@@ -4254,6 +4459,16 @@ class ImageConfig(_common.BaseModel):
4254
4459
  values are `1K`, `2K`, `4K`. If not specified, the model will use default
4255
4460
  value `1K`.""",
4256
4461
  )
4462
+ output_mime_type: Optional[str] = Field(
4463
+ default=None,
4464
+ description="""MIME type of the generated image. This field is not
4465
+ supported in Gemini API.""",
4466
+ )
4467
+ output_compression_quality: Optional[int] = Field(
4468
+ default=None,
4469
+ description="""Compression quality of the generated image (for
4470
+ ``image/jpeg`` only). This field is not supported in Gemini API.""",
4471
+ )
4257
4472
 
4258
4473
 
4259
4474
  class ImageConfigDict(TypedDict, total=False):
@@ -4268,6 +4483,14 @@ class ImageConfigDict(TypedDict, total=False):
4268
4483
  values are `1K`, `2K`, `4K`. If not specified, the model will use default
4269
4484
  value `1K`."""
4270
4485
 
4486
+ output_mime_type: Optional[str]
4487
+ """MIME type of the generated image. This field is not
4488
+ supported in Gemini API."""
4489
+
4490
+ output_compression_quality: Optional[int]
4491
+ """Compression quality of the generated image (for
4492
+ ``image/jpeg`` only). This field is not supported in Gemini API."""
4493
+
4271
4494
 
4272
4495
  ImageConfigOrDict = Union[ImageConfig, ImageConfigDict]
4273
4496
 
@@ -6290,16 +6513,12 @@ class GenerateContentResponse(_common.BaseModel):
6290
6513
  description="""First candidate from the parsed response if response_schema is provided. Not available for streaming.""",
6291
6514
  )
6292
6515
 
6293
- def _get_text(self, warn_property: Optional[str] = None) -> Optional[str]:
6516
+ def _get_text(self) -> Optional[str]:
6294
6517
  """Returns the concatenation of all text parts in the response.
6295
6518
 
6296
6519
  This is an internal method that allows customizing or disabling the warning
6297
6520
  message.
6298
6521
 
6299
- Args:
6300
- warn_property: The property name that is being accessed. This is used to
6301
- customize the warning message. If None, no warning will be logged.
6302
-
6303
6522
  Returns:
6304
6523
  The concatenation of all text parts in the response.
6305
6524
  """
@@ -6309,13 +6528,14 @@ class GenerateContentResponse(_common.BaseModel):
6309
6528
  or not self.candidates[0].content.parts
6310
6529
  ):
6311
6530
  return None
6312
- if len(self.candidates) > 1 and warn_property:
6531
+ global _response_text_warning_logged
6532
+ if len(self.candidates) > 1 and not _response_text_warning_logged:
6313
6533
  logger.warning(
6314
- f'there are {len(self.candidates)} candidates, returning'
6315
- f' {warn_property} result from the first candidate. Access'
6316
- ' response.candidates directly to get the result from other'
6317
- ' candidates.'
6534
+ f'there are {len(self.candidates)} candidates, returning text result'
6535
+ ' from the first candidate. Access response.candidates directly to'
6536
+ ' get the result from other candidates.'
6318
6537
  )
6538
+ _response_text_warning_logged = True
6319
6539
  text = ''
6320
6540
  any_text_part_text = False
6321
6541
  non_text_parts = []
@@ -6330,30 +6550,37 @@ class GenerateContentResponse(_common.BaseModel):
6330
6550
  continue
6331
6551
  any_text_part_text = True
6332
6552
  text += part.text
6333
- if non_text_parts and warn_property:
6553
+ global _response_text_non_text_warning_logged
6554
+ if non_text_parts and not _response_text_non_text_warning_logged:
6334
6555
  logger.warning(
6335
6556
  'Warning: there are non-text parts in the response:'
6336
- f' {non_text_parts}, returning concatenated {warn_property} result'
6557
+ f' {non_text_parts}, returning concatenated text result'
6337
6558
  ' from text parts. Check the full candidates.content.parts accessor'
6338
6559
  ' to get the full model response.'
6339
6560
  )
6561
+ _response_text_non_text_warning_logged = True
6340
6562
  # part.text == '' is different from part.text is None
6341
6563
  return text if any_text_part_text else None
6342
6564
 
6343
6565
  @property
6344
6566
  def parts(self) -> Optional[list[Part]]:
6345
- """Returns the content-parts in the response."""
6567
+ """Returns the content-parts in the response.
6568
+
6569
+ If there are multiple candidates, returns the parts from only the first one.
6570
+ """
6346
6571
  if (
6347
6572
  not self.candidates
6348
6573
  or self.candidates[0].content is None
6349
6574
  or self.candidates[0].content.parts is None
6350
6575
  ):
6351
6576
  return None
6352
- if len(self.candidates) > 1:
6577
+ global _response_parts_warning_logged
6578
+ if len(self.candidates) > 1 and not _response_parts_warning_logged:
6353
6579
  logger.warning(
6354
6580
  'Warning: there are multiple candidates in the response, returning'
6355
6581
  ' parts from the first one.'
6356
6582
  )
6583
+ _response_parts_warning_logged = True
6357
6584
 
6358
6585
  return self.candidates[0].content.parts
6359
6586
 
@@ -6362,23 +6589,32 @@ class GenerateContentResponse(_common.BaseModel):
6362
6589
  """Returns the concatenation of all text parts in the response.
6363
6590
 
6364
6591
  If there are multiple candidates, returns the text from only the first one.
6592
+ If there are non-text parts in the response, this returns only the text
6593
+ parts.
6365
6594
  """
6366
- return self._get_text(warn_property='text')
6595
+ return self._get_text()
6367
6596
 
6368
6597
  @property
6369
6598
  def function_calls(self) -> Optional[list[FunctionCall]]:
6370
- """Returns the list of function calls in the response."""
6599
+ """Returns the list of function calls in the response.
6600
+
6601
+ If there are multiple candidates, this returns the function calls from only
6602
+ the
6603
+ first one.
6604
+ """
6371
6605
  if (
6372
6606
  not self.candidates
6373
6607
  or not self.candidates[0].content
6374
6608
  or not self.candidates[0].content.parts
6375
6609
  ):
6376
6610
  return None
6377
- if len(self.candidates) > 1:
6611
+ global _response_function_calls_warning_logged
6612
+ if len(self.candidates) > 1 and not _response_function_calls_warning_logged:
6378
6613
  logger.warning(
6379
6614
  'Warning: there are multiple candidates in the response, returning'
6380
6615
  ' function calls from the first one.'
6381
6616
  )
6617
+ _response_function_calls_warning_logged = True
6382
6618
  function_calls = [
6383
6619
  part.function_call
6384
6620
  for part in self.candidates[0].content.parts
@@ -6389,18 +6625,28 @@ class GenerateContentResponse(_common.BaseModel):
6389
6625
 
6390
6626
  @property
6391
6627
  def executable_code(self) -> Optional[str]:
6392
- """Returns the executable code in the response."""
6628
+ """Returns the executable code in the response.
6629
+
6630
+ If there are multiple candidates, this returns the executable code from only
6631
+ the
6632
+ first one.
6633
+ """
6393
6634
  if (
6394
6635
  not self.candidates
6395
6636
  or not self.candidates[0].content
6396
6637
  or not self.candidates[0].content.parts
6397
6638
  ):
6398
6639
  return None
6399
- if len(self.candidates) > 1:
6640
+ global _response_executable_code_warning_logged
6641
+ if (
6642
+ len(self.candidates) > 1
6643
+ and not _response_executable_code_warning_logged
6644
+ ):
6400
6645
  logging.warning(
6401
6646
  'Warning: there are multiple candidates in the response, returning'
6402
6647
  ' executable code from the first one.'
6403
6648
  )
6649
+ _response_executable_code_warning_logged = True
6404
6650
  for part in self.candidates[0].content.parts:
6405
6651
  if part.executable_code is not None:
6406
6652
  return part.executable_code.code
@@ -6408,18 +6654,25 @@ class GenerateContentResponse(_common.BaseModel):
6408
6654
 
6409
6655
  @property
6410
6656
  def code_execution_result(self) -> Optional[str]:
6411
- """Returns the code execution result in the response."""
6657
+ """Returns the code execution result in the response.
6658
+
6659
+ If there are multiple candidates, this returns the code execution result
6660
+ from only the
6661
+ first one.
6662
+ """
6412
6663
  if (
6413
6664
  not self.candidates
6414
6665
  or not self.candidates[0].content
6415
6666
  or not self.candidates[0].content.parts
6416
6667
  ):
6417
6668
  return None
6418
- if len(self.candidates) > 1:
6669
+ global _response_code_execution_warning_logged
6670
+ if len(self.candidates) > 1 and not _response_code_execution_warning_logged:
6419
6671
  logging.warning(
6420
6672
  'Warning: there are multiple candidates in the response, returning'
6421
6673
  ' code execution result from the first one.'
6422
6674
  )
6675
+ _response_code_execution_warning_logged = True
6423
6676
  for part in self.candidates[0].content.parts:
6424
6677
  if part.code_execution_result is not None:
6425
6678
  return part.code_execution_result.output
@@ -15911,7 +16164,11 @@ class LiveServerMessage(_common.BaseModel):
15911
16164
 
15912
16165
  @property
15913
16166
  def text(self) -> Optional[str]:
15914
- """Returns the concatenation of all text parts in the response."""
16167
+ """Returns the concatenation of all text parts in the response.
16168
+
16169
+ If there are non-text parts in the response, only the concatenated text
16170
+ result from text parts will be returned.
16171
+ """
15915
16172
  if (
15916
16173
  not self.server_content
15917
16174
  or not self.server_content
@@ -15931,17 +16188,23 @@ class LiveServerMessage(_common.BaseModel):
15931
16188
  if isinstance(part.thought, bool) and part.thought:
15932
16189
  continue
15933
16190
  text += part.text
15934
- if non_text_parts:
16191
+ global _live_server_text_warning_logged
16192
+ if non_text_parts and not _live_server_text_warning_logged:
15935
16193
  logger.warning(
15936
16194
  'Warning: there are non-text parts in the response:'
15937
16195
  f' {non_text_parts}, returning concatenated text result from text'
15938
16196
  ' parts, check out the non text parts for full response from model.'
15939
16197
  )
16198
+ _live_server_text_warning_logged = True
15940
16199
  return text if text else None
15941
16200
 
15942
16201
  @property
15943
16202
  def data(self) -> Optional[bytes]:
15944
- """Returns the concatenation of all inline data parts in the response."""
16203
+ """Returns the concatenation of all inline data parts in the response.
16204
+
16205
+ If there are non-data parts in the response, only the concatenated data
16206
+ result from the data parts will be returned.
16207
+ """
15945
16208
  if (
15946
16209
  not self.server_content
15947
16210
  or not self.server_content
@@ -15959,12 +16222,14 @@ class LiveServerMessage(_common.BaseModel):
15959
16222
  non_data_parts.append(field_name)
15960
16223
  if part.inline_data and isinstance(part.inline_data.data, bytes):
15961
16224
  concatenated_data += part.inline_data.data
15962
- if non_data_parts:
16225
+ global _live_server_data_warning_logged
16226
+ if non_data_parts and not _live_server_data_warning_logged:
15963
16227
  logger.warning(
15964
16228
  'Warning: there are non-data parts in the response:'
15965
16229
  f' {non_data_parts}, returning concatenated data result from data'
15966
16230
  ' parts, check out the non data parts for full response from model.'
15967
16231
  )
16232
+ _live_server_data_warning_logged = True
15968
16233
  return concatenated_data if len(concatenated_data) > 0 else None
15969
16234
 
15970
16235