gemini-webapi 1.17.0__py3-none-any.whl → 1.17.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
@@ -240,7 +240,7 @@ class GeminiClient(GemMixin):
240
240
  self,
241
241
  prompt: str,
242
242
  files: list[str | Path] | None = None,
243
- model: Model | str = Model.UNSPECIFIED,
243
+ model: Model | str | dict = Model.UNSPECIFIED,
244
244
  gem: Gem | str | None = None,
245
245
  chat: Optional["ChatSession"] = None,
246
246
  **kwargs,
@@ -254,9 +254,10 @@ class GeminiClient(GemMixin):
254
254
  Prompt provided by user.
255
255
  files: `list[str | Path]`, optional
256
256
  List of file paths to be attached.
257
- model: `Model` | `str`, optional
257
+ model: `Model | str | dict`, optional
258
258
  Specify the model to use for generation.
259
- Pass either a `gemini_webapi.constants.Model` enum or a model name string.
259
+ Pass either a `gemini_webapi.constants.Model` enum or a model name string to use predefined models.
260
+ Pass a dictionary to use custom model header strings ("model_name" and "model_header" keys must be provided).
260
261
  gem: `Gem | str`, optional
261
262
  Specify a gem to use as system prompt for the chat session.
262
263
  Pass either a `gemini_webapi.types.Gem` object or a gem id string.
@@ -287,8 +288,15 @@ class GeminiClient(GemMixin):
287
288
 
288
289
  assert prompt, "Prompt cannot be empty."
289
290
 
290
- if not isinstance(model, Model):
291
+ if isinstance(model, str):
291
292
  model = Model.from_name(model)
293
+ elif isinstance(model, dict):
294
+ model = Model.from_dict(model)
295
+ elif not isinstance(model, Model):
296
+ raise TypeError(
297
+ f"'model' must be a `gemini_webapi.constants.Model` instance, "
298
+ f"string, or dictionary; got `{type(model).__name__}`"
299
+ )
292
300
 
293
301
  if isinstance(gem, Gem):
294
302
  gem_id = gem.id
@@ -362,7 +370,7 @@ class GeminiClient(GemMixin):
362
370
  if get_nested_value(part_json, [4]):
363
371
  body_index, body = part_index, part_json
364
372
  break
365
- except (TypeError, ValueError):
373
+ except json.JSONDecodeError:
366
374
  continue
367
375
 
368
376
  if not body:
@@ -443,13 +451,17 @@ class GeminiClient(GemMixin):
443
451
  if img_part_index < body_index:
444
452
  continue
445
453
  try:
446
- img_part = json.loads(part[2])
454
+ img_part_body = get_nested_value(part, [2])
455
+ if not img_part_body:
456
+ continue
457
+
458
+ img_part_json = json.loads(img_part_body)
447
459
  if get_nested_value(
448
- img_part, [4, candidate_index, 12, 7, 0]
460
+ img_part_json, [4, candidate_index, 12, 7, 0]
449
461
  ):
450
- img_body = img_part
462
+ img_body = img_part_json
451
463
  break
452
- except (IndexError, TypeError, ValueError):
464
+ except json.JSONDecodeError:
453
465
  continue
454
466
 
455
467
  if not img_body:
@@ -614,9 +626,10 @@ class ChatSession:
614
626
  Reply id, if provided together with metadata, will override the second value in it.
615
627
  rcid: `str`, optional
616
628
  Reply candidate id, if provided together with metadata, will override the third value in it.
617
- model: `Model` | `str`, optional
629
+ model: `Model | str | dict`, optional
618
630
  Specify the model to use for generation.
619
- Pass either a `gemini_webapi.constants.Model` enum or a model name string.
631
+ Pass either a `gemini_webapi.constants.Model` enum or a model name string to use predefined models.
632
+ Pass a dictionary to use custom model header strings ("model_name" and "model_header" keys must be provided).
620
633
  gem: `Gem | str`, optional
621
634
  Specify a gem to use as system prompt for the chat session.
622
635
  Pass either a `gemini_webapi.types.Gem` object or a gem id string.
@@ -637,13 +650,13 @@ class ChatSession:
637
650
  cid: str | None = None, # chat id
638
651
  rid: str | None = None, # reply id
639
652
  rcid: str | None = None, # reply candidate id
640
- model: Model | str = Model.UNSPECIFIED,
653
+ model: Model | str | dict = Model.UNSPECIFIED,
641
654
  gem: Gem | str | None = None,
642
655
  ):
643
656
  self.__metadata: list[str | None] = [None, None, None]
644
657
  self.geminiclient: GeminiClient = geminiclient
645
658
  self.last_output: ModelOutput | None = None
646
- self.model: Model | str = model
659
+ self.model: Model | str | dict = model
647
660
  self.gem: Gem | str | None = gem
648
661
 
649
662
  if metadata:
@@ -45,17 +45,23 @@ class Model(Enum):
45
45
  UNSPECIFIED = ("unspecified", {}, False)
46
46
  G_3_0_PRO = (
47
47
  "gemini-3.0-pro",
48
- {"x-goog-ext-525001261-jspb": '[1,null,null,null,"9d8ca3786ebdfbea",null,null,null,[4]]'},
49
- False,
50
- )
51
- G_2_5_FLASH = (
52
- "gemini-2.5-flash",
53
- {"x-goog-ext-525001261-jspb": '[1,null,null,null,"9ec249fc9ad08861",null,null,0,[4]]'},
48
+ {
49
+ "x-goog-ext-525001261-jspb": '[1,null,null,null,"9d8ca3786ebdfbea",null,null,0,[4]]'
50
+ },
54
51
  False,
55
52
  )
56
53
  G_2_5_PRO = (
57
54
  "gemini-2.5-pro",
58
- {"x-goog-ext-525001261-jspb": '[1,null,null,null,"4af6c7f5da75d65d",null,null,0,[4]]'},
55
+ {
56
+ "x-goog-ext-525001261-jspb": '[1,null,null,null,"4af6c7f5da75d65d",null,null,0,[4]]'
57
+ },
58
+ False,
59
+ )
60
+ G_2_5_FLASH = (
61
+ "gemini-2.5-flash",
62
+ {
63
+ "x-goog-ext-525001261-jspb": '[1,null,null,null,"9ec249fc9ad08861",null,null,0,[4]]'
64
+ },
59
65
  False,
60
66
  )
61
67
 
@@ -69,10 +75,28 @@ class Model(Enum):
69
75
  for model in cls:
70
76
  if model.model_name == name:
71
77
  return model
78
+
72
79
  raise ValueError(
73
80
  f"Unknown model name: {name}. Available models: {', '.join([model.model_name for model in cls])}"
74
81
  )
75
82
 
83
+ @classmethod
84
+ def from_dict(cls, model_dict: dict):
85
+ if "model_name" not in model_dict or "model_header" not in model_dict:
86
+ raise ValueError(
87
+ "When passing a custom model as a dictionary, 'model_name' and 'model_header' keys must be provided."
88
+ )
89
+
90
+ if not isinstance(model_dict["model_header"], dict):
91
+ raise ValueError(
92
+ "When passing a custom model as a dictionary, 'model_header' must be a dictionary containing valid header strings."
93
+ )
94
+
95
+ custom_model = cls.UNSPECIFIED
96
+ custom_model.model_name = model_dict["model_name"]
97
+ custom_model.model_header = model_dict["model_header"]
98
+ return custom_model
99
+
76
100
 
77
101
  class ErrorCode(IntEnum):
78
102
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gemini-webapi
3
- Version: 1.17.0
3
+ Version: 1.17.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
@@ -783,11 +783,11 @@ pip install -U browser-cookie3
783
783
 
784
784
  ```yaml
785
785
  services:
786
- main:
787
- environment:
788
- GEMINI_COOKIE_PATH: /tmp/gemini_webapi
789
- volumes:
790
- - ./gemini_cookies:/tmp/gemini_webapi
786
+ main:
787
+ environment:
788
+ GEMINI_COOKIE_PATH: /tmp/gemini_webapi
789
+ volumes:
790
+ - ./gemini_cookies:/tmp/gemini_webapi
791
791
  ```
792
792
 
793
793
  > [!NOTE]
@@ -929,6 +929,23 @@ async def main():
929
929
  asyncio.run(main())
930
930
  ```
931
931
 
932
+ You can also pass custom model header strings directly to access models which are not listed above.
933
+
934
+ ```python
935
+ # "model_name" and "model_header" keys must be present
936
+ custom_model = {
937
+ "model_name": "xxx",
938
+ "model_header": {
939
+ "x-goog-ext-525001261-jspb": "[1,null,null,null,'e6fa609c3fa255c0',null,null,null,[4]]"
940
+ },
941
+ }
942
+
943
+ response = await client.generate_content(
944
+ "What's your model version?",
945
+ model=custom_model
946
+ )
947
+ ```
948
+
932
949
  ### Apply system prompt with Gemini Gems
933
950
 
934
951
  System prompt can be applied to conversations via [Gemini Gems](https://gemini.google.com/gems/view). To use a gem, you can pass `gem` argument to `GeminiClient.generate_content` or `GeminiClient.start_chat`. `gem` can be either a string of gem id or a `gemini_webapi.Gem` object. Only one gem can be applied to a single conversation.
@@ -1,6 +1,6 @@
1
1
  gemini_webapi/__init__.py,sha256=7ELCiUoI10ea3daeJxnv0UwqLVKpM7rxsgOZsPMstO8,150
2
- gemini_webapi/client.py,sha256=9hcf0MOSiYbysv9rtcPm6wsRnb2aRLv8c8LL9Kxf8k4,28476
3
- gemini_webapi/constants.py,sha256=frgypJCzQvbtfNxYAOntyc9WSP0MiQ_RpsK4bJ-N6VI,2502
2
+ gemini_webapi/client.py,sha256=zcsJHaZAi_lOIOtwQ-6PqS2S6-8m_Jt3rZRK7cH0EBQ,29288
3
+ gemini_webapi/constants.py,sha256=tm9y8oKuD3QhKXUbVXZKroYCoPpqgBJdSTwoeue43Ss,3295
4
4
  gemini_webapi/exceptions.py,sha256=qkXrIpr0L7LtGbq3VcTO8D1xZ50pJtt0dDRp5I3uDSg,1038
5
5
  gemini_webapi/components/__init__.py,sha256=wolxuAJJ32-jmHOKgpsesexP7hXea1JMo5vI52wysTI,48
6
6
  gemini_webapi/components/gem_mixin.py,sha256=WPJkYDS4yQpLMBNQ94LQo5w59RgkllWaSiHsFG1k5GU,8795
@@ -18,8 +18,8 @@ gemini_webapi/utils/logger.py,sha256=0VcxhVLhHBRDQutNCpapP1y_MhPoQ2ud1uIFLqxC3Z8
18
18
  gemini_webapi/utils/parsing.py,sha256=z-t0bDbXVIa0-3_ZmTK19PvL6zBM5vuy56z2jv1Yu1I,2105
19
19
  gemini_webapi/utils/rotate_1psidts.py,sha256=XjEeQnZS3ZI6wOl0Zb5CvsbIrg0BVVNas7cE6f3x_XE,1802
20
20
  gemini_webapi/utils/upload_file.py,sha256=SJOMr6kryK_ClrKmqI96fqZBNFOMPsyAvFINAGAU3rk,1468
21
- gemini_webapi-1.17.0.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
22
- gemini_webapi-1.17.0.dist-info/METADATA,sha256=EuMINxma9gp-2YC-cd2w_mOecbBNAyJzUcIiZ-oT_1A,61299
23
- gemini_webapi-1.17.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
24
- gemini_webapi-1.17.0.dist-info/top_level.txt,sha256=dtWtug_ZrmnUqCYuu8NmGzTgWglHeNzhHU_hXmqZGWE,14
25
- gemini_webapi-1.17.0.dist-info/RECORD,,
21
+ gemini_webapi-1.17.1.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
22
+ gemini_webapi-1.17.1.dist-info/METADATA,sha256=OlCVaM8XIgS_w6zxowLrxLxc5ksPf-3W27oodzH2_MM,61763
23
+ gemini_webapi-1.17.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
24
+ gemini_webapi-1.17.1.dist-info/top_level.txt,sha256=dtWtug_ZrmnUqCYuu8NmGzTgWglHeNzhHU_hXmqZGWE,14
25
+ gemini_webapi-1.17.1.dist-info/RECORD,,