dsdanielpark / Gemini-API

The unofficial python package that returns response of Google Gemini through cookie values.
https://pypi.org/project/python-gemini-api/
MIT License
146 stars 9 forks source link

'NoneType' object has no attribute 'group' #13

Closed NoBanOnlyZXC closed 2 months ago

NoBanOnlyZXC commented 4 months ago

πŸ’šπŸ’œ Thank you for interest. β€οΈπŸ’› Please make sure to check for more efficient package management.

To Reproduce

Describe the bug not working gemini, writes ↓

Version OS: Win11 Python: 3.11.7 Gemini API: usa Using proxy: no Legion:

Code

cookies = {
    "key": google_api_key # google_api_key : str
}

GeminiClient = Gemini(cookies=cookies)

Error

'NoneType' object has no attribute 'group'
  File "D:\MH\gemini.py", line 18, in <module>
    GeminiClient = Gemini(cookies=cookies)
                   ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'group'
dsdanielpark commented 4 months ago

Please note that the work is still in progress, so there may be frequent errors. Thank you for your understanding!

I've encountered the same error while developing. I'm currently working on exception handling.

Probable Causes

Troubleshooting Solutions

  1. If possible, try changing your IP (use a different network).
  2. Log out of your Google account, close the browser, and try logging in again (or use incognito mode, private browsing, etc.).
  3. If you have another Google account, try using that one. When setting South Korea as the Google account region, I witnessed rapid updates of the error or cookie values, whereas setting Ukraine as the Google account region allowed for prolonged cookie retention, making it convenient for subsequent calls.
  4. If none of the above is possible, try again after a few hours.
  5. If you are currently using auto_cookies=True, please set it to False and manually pass the cookies. The auto_cookies feature is currently incomplete.
  6. If you've declared the Gemini object once with Gemini(cookies=cookies) and didn't receive an error, refrain from declaring the object again. Doing so will resend the request, potentially causing temporary errors due to rate limiting.
  7. (Most Important) As stated in the readme, if you've successfully instantiated the Gemini class and verified the response in generate_content, please refrain from closing the Gemini web page and revisiting it.
dsdanielpark commented 4 months ago

NoBanOnlyZXC

If you have google official api key from google ai studio. See this document. https://ai.google.dev/tutorials/python_quickstart

OlegRuban-ai commented 4 months ago

NoBanOnlyZXC

If you have google official api key from google ai studio. See this document. https://ai.google.dev/tutorials/python_quickstart

The official api key is not connected to the Internet and there is no Gemini Ultra, which can be used in the web version. This is why access to requests from the page is so important

OlegRuban-ai commented 4 months ago

This issue occurs because the __Secure-1PSIDTS cookie is updated every 30 minutes.

We need a mechanism to automatically remove it, but also for cases when requests are executed on a remote server, when there is no physical ability to keep the browser open.

I set the region to Ukraine and the USA - in each case this cookie was updated after 30 minutes.

mizrits commented 4 months ago

@ dsdanielpark I installed GeminiAPI for the first time since last July during the BardAPI days. I followed the steps, but it has been failing with the same error since the first time. Isn't this error occurring on Google side due to a cookie issue, but an error in GeminiAPI's Python code? I'm getting an error with Python, so I don't think cookies, rate limits, accounts, or countries are involved. Actually, troubleshooting was pointless.

From Google Translate. I apologize for the unnatural English. I am grateful for your work.

dsdanielpark commented 4 months ago

mizrits

In my case, it's likely that your experience is incorrect.

The error can occur when the received payload does not contain the specified cookie.

Currently, Google seems to apply stronger measures against repetitive requests than the previous Bard API, by either temporarily blocking them, caching some cookies without exposing them, or calling RotateCookie to prevent JSON hijacking.

I also experience the same error with repetitive requests after receiving normal responses.

1) If this error occurs, try to minimize the number of requests per hour as much as possible. If the error starts occurring, it's mostly a rate limiting issue.

2) Try using accounts from different countries (e.g., Ukraine) to attempt with all cookies, not just some.

3) Experiment with logging out and in, resetting the browser (incognito or private browsing), capture the cookie values, and create a client to ensure the generate_content results are returned correctly.

Various experiments may be needed, and if successful, it would be appreciated if you could document the process in an issue.

4) Once you receive the generate_content return successfully with a cookie value, close the gemini web in all browsers. If it remains open, the cookies may be refreshed, preventing the use of the original cookie values.

dsdanielpark commented 4 months ago

@ dsdanielpark

I installed GeminiAPI for the first time since last July during the BardAPI days.

I followed the steps, but it has been failing with the same error since the first time.

Isn't this error occurring on Google side due to a cookie issue, but an error in GeminiAPI's Python code?

I'm getting an error with Python, so I don't think cookies, rate limits, accounts, or countries are involved. Actually, troubleshooting was pointless.

From Google Translate. I apologize for the unnatural English. I am grateful for your work.

I will split package issues and API issues.

Please, give me a more time!

mizrits commented 4 months ago

OK, thanksπŸ’ͺ