google-genai 0.2.0__py3-none-any.whl → 0.2.2__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/__init__.py CHANGED
@@ -17,6 +17,6 @@
17
17
 
18
18
  from .client import Client
19
19
 
20
- __version__ = '0.2.0'
20
+ __version__ = '0.2.2'
21
21
 
22
22
  __all__ = ['Client']
@@ -51,7 +51,7 @@ class HttpOptions(TypedDict):
51
51
  def _append_library_version_headers(headers: dict[str, str]) -> None:
52
52
  """Appends the telemetry header to the headers dict."""
53
53
  # TODO: Automate revisions to the SDK library version.
54
- library_label = f'google-genai-sdk/0.2.0'
54
+ library_label = f'google-genai-sdk/0.2.2'
55
55
  language_label = 'gl-python/' + sys.version.split()[0]
56
56
  version_header_value = f'{library_label} {language_label}'
57
57
  if (
@@ -200,7 +200,7 @@ class ApiClient:
200
200
  _append_library_version_headers(self._http_options['headers'])
201
201
 
202
202
  def _websocket_base_url(self):
203
- url_parts = urllib.parse.urlparse(self._http_options['base_url'])
203
+ url_parts = urlparse(self._http_options['base_url'])
204
204
  return url_parts._replace(scheme='wss').geturl()
205
205
 
206
206
  def _build_request(
google/genai/errors.py CHANGED
@@ -29,29 +29,49 @@ class APIError(Exception):
29
29
  code: int
30
30
  response: requests.Response
31
31
 
32
- message: str = ''
33
- status: str = 'UNKNOWN'
34
- details: Optional[Any] = None
32
+ status: Optional[str] = None
33
+ message: Optional[str] = None
34
+ response: Optional[Any] = None
35
35
 
36
36
  def __init__(
37
37
  self, code: int, response: Union[requests.Response, 'ReplayResponse']
38
38
  ):
39
- self.code = code
40
39
  self.response = response
41
40
 
42
41
  if isinstance(response, requests.Response):
43
42
  try:
44
- raw_error = response.json().get('error', {})
43
+ # do not do any extra muanipulation on the response.
44
+ # return the raw response json as is.
45
+ response_json = response.json()
45
46
  except requests.exceptions.JSONDecodeError:
46
- raw_error = {'message': response.text, 'status': response.reason}
47
+ response_json = {
48
+ 'message': response.text,
49
+ 'status': response.reason,
50
+ }
47
51
  else:
48
- raw_error = response.body_segments[0].get('error', {})
52
+ response_json = response.body_segments[0].get('error', {})
53
+
54
+ self.details = response_json
55
+ self.message = self._get_message(response_json)
56
+ self.status = self._get_status(response_json)
57
+ self.code = code if code else self._get_code(response_json)
58
+
59
+ super().__init__(f'{self.code} {self.status}. {self.details}')
60
+
61
+ def _get_status(self, response_json):
62
+ return response_json.get(
63
+ 'status', response_json.get('error', {}).get('status', None)
64
+ )
49
65
 
50
- self.message = raw_error.get('message', '')
51
- self.status = raw_error.get('status', 'UNKNOWN')
52
- self.details = raw_error.get('details', None)
66
+ def _get_message(self, response_json):
67
+ return response_json.get(
68
+ 'message', response_json.get('error', {}).get('message', None)
69
+ )
53
70
 
54
- super().__init__(f'{self.code} {self.status}. {self.message}')
71
+ def _get_code(self, response_json):
72
+ return response_json.get(
73
+ 'code', response_json.get('error', {}).get('code', None)
74
+ )
55
75
 
56
76
  def _to_replay_record(self):
57
77
  """Returns a dictionary representation of the error for replay recording.
google/genai/types.py CHANGED
@@ -1280,7 +1280,7 @@ class AutomaticFunctionCallingConfig(_common.BaseModel):
1280
1280
  """,
1281
1281
  )
1282
1282
  maximum_remote_calls: Optional[int] = Field(
1283
- default=None,
1283
+ default=10,
1284
1284
  description="""If automatic function calling is enabled,
1285
1285
  maximum number of remote calls for automatic function calling.
1286
1286
  This number should be a positive integer.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: google-genai
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: GenAI Python SDK
5
5
  Author-email: Google LLC <googleapis-packages@google.com>
6
6
  License: Apache-2.0
@@ -159,6 +159,14 @@ response = client.models.generate_content(
159
159
  response.text
160
160
  ```
161
161
 
162
+ #### Manually declare and invoke a function for function calling
163
+
164
+ If you don't want to use the automatic function support, you can manually
165
+ declare the function and invoke it.
166
+
167
+ The following example shows how to declare a function and pass it as a tool.
168
+ Then you will receive a function call part in the response.
169
+
162
170
  ``` python
163
171
  function = dict(
164
172
  name="get_current_weather",
@@ -187,15 +195,24 @@ response = client.models.generate_content(
187
195
  response.candidates[0].content.parts[0].function_call
188
196
  ```
189
197
 
198
+ After you receive the function call part from model, you can invoke the function
199
+ and get the function response. And then you can pass the function response to
200
+ the model.
201
+ The following example shows how to do it for a simple function invocation.
202
+
190
203
  ``` python
191
204
  function_call_part = response.candidates[0].content.parts[0]
192
205
 
193
- function_response = get_current_weather(**function_call_part.function_call.args)
206
+ try:
207
+ function_result = get_current_weather(**function_call_part.function_call.args)
208
+ function_response = {'result': function_result}
209
+ except Exception as e: # instead of raising the exception, you can let the model handle it
210
+ function_response = {'error': str(e)}
194
211
 
195
212
 
196
213
  function_response_part = types.Part.from_function_response(
197
214
  name=function_call_part.function_call.name,
198
- response={'result': function_response}
215
+ response=function_response,
199
216
  )
200
217
 
201
218
  response = client.models.generate_content(
@@ -273,6 +290,8 @@ print(response.text)
273
290
 
274
291
  ### Streaming
275
292
 
293
+ #### Streaming for text content
294
+
276
295
  ``` python
277
296
  for chunk in client.models.generate_content_stream(
278
297
  model='gemini-2.0-flash-exp', contents='Tell me a story in 300 words.'
@@ -280,6 +299,47 @@ for chunk in client.models.generate_content_stream(
280
299
  print(chunk.text)
281
300
  ```
282
301
 
302
+ #### Streaming for image content
303
+
304
+ If your image is stored in Google Cloud Storage, you can use the `from_uri`
305
+ class method to create a Part object.
306
+
307
+ ``` python
308
+ for chunk in client.models.generate_content_stream(
309
+ model='gemini-1.5-flash',
310
+ contents=[
311
+ 'What is this image about?',
312
+ types.Part.from_uri(
313
+ file_uri='gs://generativeai-downloads/images/scones.jpg',
314
+ mime_type='image/jpeg'
315
+ )
316
+ ],
317
+ ):
318
+ print(chunk.text)
319
+ ```
320
+
321
+ If your image is stored in your local file system, you can read it in as bytes
322
+ data and use the `from_bytes` class method to create a Part object.
323
+
324
+ ``` python
325
+ YOUR_IMAGE_PATH = 'your_image_path'
326
+ YOUR_IMAGE_MIME_TYPE = 'your_image_mime_type'
327
+ with open(YOUR_IMAGE_PATH, 'rb') as f:
328
+ image_bytes = f.read()
329
+
330
+ for chunk in client.models.generate_content_stream(
331
+ model='gemini-1.5-flash',
332
+ contents=[
333
+ 'What is this image about?',
334
+ types.Part.from_bytes(
335
+ data=image_bytes,
336
+ mime_type=YOUR_IMAGE_MIME_TYPE
337
+ )
338
+ ],
339
+ ):
340
+ print(chunk.text)
341
+ ```
342
+
283
343
  ### Async
284
344
 
285
345
  `client.aio` exposes all the analogous `async` methods that are
@@ -1,5 +1,5 @@
1
- google/genai/__init__.py,sha256=bO4TBLSOack_93tDti_USNTkE8cvkLUn45TH-WS-HOE,674
2
- google/genai/_api_client.py,sha256=74qm-UTxDQ3-KCb61eqD3SAM379MrsQw8ly6GBLoDB8,15963
1
+ google/genai/__init__.py,sha256=R7sy9MQmlItVERIQEWt25bApyNXxDIkYP4nl3-EtX50,674
2
+ google/genai/_api_client.py,sha256=naJy-6OjgjUNlciZNxDconmSOFhewD_0QsggNY1aCik,15950
3
3
  google/genai/_automatic_function_calling_util.py,sha256=E25_66RH3DbDIucq7x-93XWPPBwB9FnzwD1NCGyPrjM,10242
4
4
  google/genai/_common.py,sha256=Yj5cBkq5QRNFSBqvpB949Rjo7cbIhdtKp5dJxMW_I6I,7971
5
5
  google/genai/_extra_utils.py,sha256=GQZnraFCrMffqrBEpurdcBmgrltRsnYgMizt-Ok6xX8,11098
@@ -10,15 +10,15 @@ google/genai/batches.py,sha256=Wi4Kptampp2WepAqv_AawwNCR6MKVhLKmzJdYXDQ_aE,37148
10
10
  google/genai/caches.py,sha256=LJm2raykec7_iCHsVbEtX4v942mR-OSQvxTVKcBN2RA,53434
11
11
  google/genai/chats.py,sha256=x-vCXrsxZ8kdEZ_0ZDfrBQnQ9urCr42x3urP0OXHyTo,5688
12
12
  google/genai/client.py,sha256=HH_lYnjPOwW-4Vgynyw4K8cwurT2g578Dc51H_uk7GY,9244
13
- google/genai/errors.py,sha256=TrlUk1jz7r1aN1lrL3FZZ30LU4iMfSonm1ZwEAk07k4,3048
13
+ google/genai/errors.py,sha256=ZqJvfuJ7oS334WBrq3otzdZfmhEgcM1OBZhHccYzDok,3665
14
14
  google/genai/files.py,sha256=dn3q8P9aTN9OG3PtA4AYDs9hF6Uk-jkMjgAW7dSlt_4,35573
15
15
  google/genai/live.py,sha256=T-pOtq7k43wE2VjQzqLrx-kqhotS66I2PY_NHBdv9G8,22056
16
16
  google/genai/models.py,sha256=t5XgwlgkNrQKb6eww0oBGzjMiMQaj-BQedc8lVdJHz4,154834
17
17
  google/genai/pagers.py,sha256=hSHd-gLvEzYWwK85i8EcFNWUMKtszUs7Nw2r3L7d6_U,6686
18
18
  google/genai/tunings.py,sha256=tFTSEaECKZ6xeYcxUTIKUmXqPoDymYP3eyTcEKjnPa4,49010
19
- google/genai/types.py,sha256=mIjtCSXbp6CRL5iEhtdxczoMTtyQ1EKYpBlzLvGIedY,263841
20
- google_genai-0.2.0.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
21
- google_genai-0.2.0.dist-info/METADATA,sha256=qhc4AtoMxFa_-BIEzNTw36s-cE1imA3zOoILYIpL7as,17371
22
- google_genai-0.2.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
23
- google_genai-0.2.0.dist-info/top_level.txt,sha256=_1QvSJIhFAGfxb79D6DhB7SUw2X6T4rwnz_LLrbcD3c,7
24
- google_genai-0.2.0.dist-info/RECORD,,
19
+ google/genai/types.py,sha256=JC7CBQVRzVwImsT03t6Qv_vMYq8V58z3SF-rzvrUJHc,263839
20
+ google_genai-0.2.2.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
21
+ google_genai-0.2.2.dist-info/METADATA,sha256=Q1eNLWWM0fqko3S5gfudpPYrlmnebdnXXLOJGjAtmG0,19175
22
+ google_genai-0.2.2.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
23
+ google_genai-0.2.2.dist-info/top_level.txt,sha256=_1QvSJIhFAGfxb79D6DhB7SUw2X6T4rwnz_LLrbcD3c,7
24
+ google_genai-0.2.2.dist-info/RECORD,,