gemini-webapi 1.8.4__py3-none-any.whl → 1.9.1__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.
gemini_webapi/client.py CHANGED
@@ -361,14 +361,17 @@ class GeminiClient:
361
361
  try:
362
362
  response_json = json.loads(response.text.split("\n")[2])
363
363
 
364
- # Plain request
365
- body = json.loads(response_json[0][2])
366
-
367
- if not body[4]:
368
- # Request with Gemini extensions enabled
369
- body = json.loads(response_json[4][2])
370
-
371
- if not body[4]:
364
+ body = None
365
+ for part in response_json:
366
+ try:
367
+ main_part = json.loads(part[2])
368
+ if main_part[4]:
369
+ body = main_part
370
+ break
371
+ except (IndexError, TypeError, ValueError):
372
+ continue
373
+
374
+ if not body:
372
375
  raise Exception
373
376
  except Exception:
374
377
  await self.close()
@@ -386,6 +389,11 @@ class GeminiClient:
386
389
  ):
387
390
  text = candidate[22] and candidate[22][0] or text
388
391
 
392
+ try:
393
+ thoughts = candidate[37][0][0]
394
+ except (TypeError, IndexError):
395
+ thoughts = None
396
+
389
397
  web_images = (
390
398
  candidate[12]
391
399
  and candidate[12][1]
@@ -435,6 +443,7 @@ class GeminiClient:
435
443
  Candidate(
436
444
  rcid=candidate[0],
437
445
  text=text,
446
+ thoughts=thoughts,
438
447
  web_images=web_images,
439
448
  generated_images=generated_images,
440
449
  )
@@ -25,6 +25,31 @@ class Headers(Enum):
25
25
 
26
26
  class Model(Enum):
27
27
  UNSPECIFIED = ("unspecified", {}, False)
28
+ G_2_0_FLASH = (
29
+ "gemini-2.0-flash",
30
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"f299729663a2343f"]'},
31
+ False,
32
+ )
33
+ G_2_0_FLASH_EXP = (
34
+ "gemini-2.0-flash-exp",
35
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"f299729663a2343f"]'},
36
+ False,
37
+ ) # Deprecated, should be removed in the future
38
+ G_2_0_FLASH_THINKING = (
39
+ "gemini-2.0-flash-thinking",
40
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"9c17b1863f581b8a"]'},
41
+ False,
42
+ )
43
+ G_2_0_FLASH_THINKING_WITH_APPS = (
44
+ "gemini-2.0-flash-thinking-with-apps",
45
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"f8f8f5ea629f5d37"]'},
46
+ False,
47
+ )
48
+ G_2_0_EXP_ADVANCED = (
49
+ "gemini-2.0-exp-advanced",
50
+ {"x-goog-ext-525001261-jspb": '[null,null,null,null,"b1e46a6037e6aa9f"]'},
51
+ True,
52
+ )
28
53
  G_1_5_FLASH = (
29
54
  "gemini-1.5-flash",
30
55
  {"x-goog-ext-525001261-jspb": '[null,null,null,null,"418ab5ea040b5c43"]'},
@@ -40,16 +65,6 @@ class Model(Enum):
40
65
  {"x-goog-ext-525001261-jspb": '[null,null,null,null,"e5a44cb1dae2b489"]'},
41
66
  True,
42
67
  )
43
- G_2_0_FLASH_EXP = (
44
- "gemini-2.0-flash-exp",
45
- {"x-goog-ext-525001261-jspb": '[null,null,null,null,"f299729663a2343f"]'},
46
- False,
47
- )
48
- G_2_0_EXP_ADVANCED = (
49
- "gemini-2.0-exp-advanced",
50
- {"x-goog-ext-525001261-jspb": '[null,null,null,null,"b1e46a6037e6aa9f"]'},
51
- True,
52
- )
53
68
 
54
69
  def __init__(self, name, header, advanced_only):
55
70
  self.model_name = name
@@ -13,6 +13,8 @@ class Candidate(BaseModel):
13
13
  Reply candidate ID to build the metadata
14
14
  text: `str`
15
15
  Text output
16
+ thoughts: `str`, optional
17
+ Model's thought process, can be empty. Only populated with `-thinking` models
16
18
  web_images: `list[WebImage]`, optional
17
19
  List of web images in reply, can be empty.
18
20
  generated_images: `list[GeneratedImage]`, optional
@@ -21,6 +23,7 @@ class Candidate(BaseModel):
21
23
 
22
24
  rcid: str
23
25
  text: str
26
+ thoughts: str | None = None
24
27
  web_images: list[WebImage] = []
25
28
  generated_images: list[GeneratedImage] = []
26
29
 
@@ -32,6 +32,10 @@ class ModelOutput(BaseModel):
32
32
  def text(self) -> str:
33
33
  return self.candidates[self.chosen].text
34
34
 
35
+ @property
36
+ def thoughts(self) -> str | None:
37
+ return self.candidates[self.chosen].thoughts
38
+
35
39
  @property
36
40
  def images(self) -> list[Image]:
37
41
  return self.candidates[self.chosen].images
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: gemini-webapi
3
- Version: 1.8.4
3
+ Version: 1.9.1
4
4
  Summary: ✨ An elegant async Python wrapper for Google Gemini web app
5
5
  Author: UZQueen
6
6
  License: GNU AFFERO GENERAL PUBLIC LICENSE
@@ -679,6 +679,7 @@ License-File: LICENSE
679
679
  Requires-Dist: httpx[http2]~=0.28.1
680
680
  Requires-Dist: pydantic~=2.10.5
681
681
  Requires-Dist: loguru~=0.7.3
682
+ Dynamic: license-file
682
683
 
683
684
  <p align="center">
684
685
  <img src="https://raw.githubusercontent.com/HanaokaYuzu/Gemini-API/master/assets/banner.png" width="55%" alt="Gemini Banner" align="center">
@@ -725,14 +726,15 @@ A reverse-engineered asynchronous python wrapper for [Google Gemini](https://gem
725
726
  - [Authentication](#authentication)
726
727
  - [Usage](#usage)
727
728
  - [Initialization](#initialization)
729
+ - [Select language model](#select-language-model)
728
730
  - [Generate contents from text](#generate-contents-from-text)
729
731
  - [Generate contents from image](#generate-contents-from-image)
730
732
  - [Conversations across multiple turns](#conversations-across-multiple-turns)
731
733
  - [Continue previous conversations](#continue-previous-conversations)
734
+ - [Retrieve model's thought process](#retrieve-models-thought-process)
732
735
  - [Retrieve images in response](#retrieve-images-in-response)
733
736
  - [Generate images with ImageFx](#generate-images-with-imagefx)
734
737
  - [Save images to local files](#save-images-to-local-files)
735
- - [Specify language model version](#specify-language-model-version)
736
738
  - [Generate contents with Gemini extensions](#generate-contents-with-gemini-extensions)
737
739
  - [Check and switch to other reply candidates](#check-and-switch-to-other-reply-candidates)
738
740
  - [Control log level](#control-log-level)
@@ -815,6 +817,41 @@ asyncio.run(main())
815
817
  >
816
818
  > `auto_close` and `close_delay` are optional arguments for automatically closing the client after a certain period of inactivity. This feature is disabled by default. In an always-on service like chatbot, it's recommended to set `auto_close` to `True` combined with reasonable seconds of `close_delay` for better resource management.
817
819
 
820
+ ### Select language model
821
+
822
+ You can specify which language model to use by passing `model` argument to `GeminiClient.generate_content` or `GeminiClient.start_chat`. The default value is `unspecified`.
823
+
824
+ Currently available models (as of Feb 5, 2025):
825
+
826
+ - `unspecified` - Default model (same as `gemini-2.0-flash` if account does NOT have Gemini Advanced subscription)
827
+ - `gemini-2.0-flash` - Gemini 2.0 Flash
828
+ - `gemini-2.0-flash-thinking` - Gemini 2.0 Flash Thinking Experimental
829
+ - `gemini-2.0-flash-thinking-with-apps` - Gemini 2.0 Flash Thinking Experimental with apps
830
+ - `gemini-1.5-flash` - Gemini 1.5 Flash
831
+
832
+ Models pending update (may not work as expected):
833
+
834
+ - `gemini-2.0-exp-advanced` - Gemini 2.0 Experimental Advanced **(requires Gemini Advanced account)**
835
+ - `gemini-1.5-pro` - Gemini 1.5 Pro **(requires Gemini Advanced account)**
836
+ - `gemini-1.5-pro-research` - Gemini 1.5 Pro with Deep Research **(requires Gemini Advanced account)**
837
+
838
+ ```python
839
+ from gemini_webapi.constants import Model
840
+
841
+ async def main():
842
+ response1 = await client.generate_content(
843
+ "What's you language model version? Reply version number only.",
844
+ model=Model.G_2_0_FLASH,
845
+ )
846
+ print(f"Model version ({Model.G_2_0_FLASH.model_name}): {response1.text}")
847
+
848
+ chat = client.start_chat(model="gemini-2.0-flash-thinking")
849
+ response2 = await chat.send_message("What's you language model version? Reply version number only.")
850
+ print(f"Model version (gemini-2.0-flash-thinking): {response2.text}")
851
+
852
+ asyncio.run(main())
853
+ ```
854
+
818
855
  ### Generate contents from text
819
856
 
820
857
  Ask a one-turn quick question by calling `GeminiClient.generate_content`.
@@ -885,6 +922,21 @@ async def main():
885
922
  asyncio.run(main())
886
923
  ```
887
924
 
925
+ ### Retrieve model's thought process
926
+
927
+ When using models with thinking capabilities, the model's thought process will be populated in `ModelOutput.thoughts`.
928
+
929
+ ```python
930
+ async def main():
931
+ response = await client.generate_content(
932
+ "What's 1+1?", model="gemini-2.0-flash-thinking"
933
+ )
934
+ print(response.thoughts)
935
+ print(response.text)
936
+
937
+ asyncio.run(main())
938
+ ```
939
+
888
940
  ### Retrieve images in response
889
941
 
890
942
  Images in the API's output are stored as a list of `Image` objects. You can access the image title, URL, and description by calling `image.title`, `image.url` and `image.alt` respectively.
@@ -938,36 +990,6 @@ async def main():
938
990
  asyncio.run(main())
939
991
  ```
940
992
 
941
- ### Specify language model version
942
-
943
- You can choose a specified language model version by passing `model` argument to `GeminiClient.generate_content` or `GeminiClient.start_chat`. The default value is `unspecified`.
944
-
945
- Currently available models (as of Feb 1, 2025):
946
-
947
- - `unspecified` - Default model (Gemini 2.0 Flash if account does NOT have Gemini Advanced subscription)
948
- - `gemini-1.5-flash` - Gemini 1.5 Flash
949
- - `gemini-1.5-pro` - Gemini 1.5 Pro **(requires Gemini Advanced account)**
950
- - `gemini-1.5-pro-research` - Gemini 1.5 Pro with Deep Research **(requires Gemini Advanced account)**
951
- - `gemini-2.0-flash-exp` - Gemini 2.0 Flash Experimental
952
- - `gemini-2.0-exp-advanced` - Gemini 2.0 Experimental Advanced **(requires Gemini Advanced account)**
953
-
954
- ```python
955
- from gemini_webapi.constants import Model
956
-
957
- async def main():
958
- response1 = await client.generate_content(
959
- "What's you language model version? Reply version number only.",
960
- model="gemini-1.5-flash",
961
- )
962
- print(f"Model version (gemini-1.5-flash): {response1.text}")
963
-
964
- chat = client.start_chat(model=Model.G_2_0_FLASH_EXP)
965
- response2 = await chat.send_message("What's you language model version? Reply version number only.")
966
- print(f"Model version ({Model.G_2_0_FLASH_EXP.model_name}): {response2.text}")
967
-
968
- asyncio.run(main())
969
- ```
970
-
971
993
  ### Generate contents with Gemini extensions
972
994
 
973
995
  > [!IMPORTANT]
@@ -1,19 +1,19 @@
1
1
  gemini_webapi/__init__.py,sha256=28uNIywK4vCXxENaSagNWUzhqr1RyNtLzDF6WRRM4KQ,194
2
- gemini_webapi/client.py,sha256=PPr8hZljMKOxszKFJihvqUZaMV0eSNJ-X2d4Hvz7jFU,22759
3
- gemini_webapi/constants.py,sha256=_glqlB6Wy_VDi7h6ruorGGdtoyKfVg6shYfA_BAT9Q4,2178
2
+ gemini_webapi/client.py,sha256=4ptFBYy-ipcxox1x7WDOK04YMYU_cu6NwS8CSrmz3kM,23105
3
+ gemini_webapi/constants.py,sha256=_8TdJH3dvzSDo0qfhKCAerWXBbH71u0B0mSGOkkJQ5w,2737
4
4
  gemini_webapi/exceptions.py,sha256=6e-EXHGApi4iC0GDw7RKc3YqVK8UvEkHYaJyGQbReLw,548
5
5
  gemini_webapi/types/__init__.py,sha256=d2kvXnE004s2E2KDmPPLi5N-BQ59FgDSlrGrO3Wphww,163
6
- gemini_webapi/types/candidate.py,sha256=Z9bpIK4l8UWbUVMLEoophfhgROo93dxOM9cAwx77CkU,1030
6
+ gemini_webapi/types/candidate.py,sha256=dMoGr53WR7FYDRrXpG9Yd_n9YTHGwGaFHZ8zPt8RMWw,1178
7
7
  gemini_webapi/types/image.py,sha256=4BC8hxAWJrYFwzA60CivF1di4RZkzPKjcaSPPFKmRdY,5237
8
- gemini_webapi/types/modeloutput.py,sha256=sGEnaQtSOJE68ve5R5sFgW4POmsyWZAV5zei3Z4BRVk,1090
8
+ gemini_webapi/types/modeloutput.py,sha256=h07kQOkL5r-oPLvZ59uVtO1eP4FGy5ZpzuYQzAeQdr8,1196
9
9
  gemini_webapi/utils/__init__.py,sha256=mcm1kgQ5HHKyZrhHS-rd_GXbKMpIDUsq02XtmlQNN_I,357
10
10
  gemini_webapi/utils/get_access_token.py,sha256=uyb6tuzPr3mHttCjiM86M29ykrnHqsUClYdf5sVkyEQ,5465
11
11
  gemini_webapi/utils/load_browser_cookies.py,sha256=A5n_VsB7Rm8ck5lpy856UNJEhv30l3dvQ3j0g3ln1fE,1535
12
12
  gemini_webapi/utils/logger.py,sha256=PF4ROQq7scRRrWzeYdeYiYs2S2Jqr0bgjyrPbXVOCqE,816
13
13
  gemini_webapi/utils/rotate_1psidts.py,sha256=NyQ9OYPLBOcvpc8bodvEYDIVFrsYN0kdfc831lPEctM,1680
14
14
  gemini_webapi/utils/upload_file.py,sha256=A-ZQ54gy2wOVA_giEEVLQQK5zTJfrgq99Z5YYYOAFYc,1163
15
- gemini_webapi-1.8.4.dist-info/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
16
- gemini_webapi-1.8.4.dist-info/METADATA,sha256=zKj_zbFMsF1Gkm2VEsIkXX6Zab_x6dBNpPwjsz1efZQ,57135
17
- gemini_webapi-1.8.4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
18
- gemini_webapi-1.8.4.dist-info/top_level.txt,sha256=dtWtug_ZrmnUqCYuu8NmGzTgWglHeNzhHU_hXmqZGWE,14
19
- gemini_webapi-1.8.4.dist-info/RECORD,,
15
+ gemini_webapi-1.9.1.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
16
+ gemini_webapi-1.9.1.dist-info/METADATA,sha256=LXrhPGqkC1MfBvX868TaWV-xKuVpQmsDbi90KSrKHzg,57797
17
+ gemini_webapi-1.9.1.dist-info/WHEEL,sha256=tTnHoFhvKQHCh4jz3yCn0WPTYIy7wXx3CJtJ7SJGV7c,91
18
+ gemini_webapi-1.9.1.dist-info/top_level.txt,sha256=dtWtug_ZrmnUqCYuu8NmGzTgWglHeNzhHU_hXmqZGWE,14
19
+ gemini_webapi-1.9.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (77.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5