Skip to content

Refactor multiple flows to enhance performance, boost scalability, and ensure stability. - #131

Draft
luuquangvu wants to merge 295 commits into
Nativu5:mainfrom
luuquangvu:main
Draft

Refactor multiple flows to enhance performance, boost scalability, and ensure stability.#131
luuquangvu wants to merge 295 commits into
Nativu5:mainfrom
luuquangvu:main

Conversation

@luuquangvu

Copy link
Copy Markdown
Collaborator

This PR is still a work in progress and uses features that aren't yet officially available in the Gemini-API library, so we'll need to wait for the library's official update before merging. Feel free to try it out and share any feedback or report any issues you encounter. Thanks!

Here are some highlights of the changes:

  • The entire logic for storing conversation history has been rewritten, aiming for compatibility with various endpoints and easy scalability in the future.
  • The logic of the endpoints has been rewritten, and now all endpoints work correctly with both streaming and non-streaming flows.
  • Compatible with the latest library updates, including the ability to download full-size images and enable video or music generation.
  • All cookie-related errors will be fully resolved, and users will get a clear notification if the server invalidates cookies, making it simple to know when to manually refresh a new one.

…s found in either the raw or cleaned history.
…ystem instruction when reusing a session to save tokens.
… text file attachment

- When multiple chunks are sent simultaneously, Google will immediately invalidate the access token and reject the request
- When a prompt contains a structured format like JSON, splitting it can break the format and may cause the model to misunderstand the context
- Another minor tweak as Copilot suggested
…e sessions.

- Ensure that PR HanaokaYuzu/Gemini-API#220 is merged before proceeding with this PR.
…ith reusable sessions.

- Ensure that PR HanaokaYuzu/Gemini-API#220 is merged before proceeding with this PR.
- Introducing a new feature for real-time streaming responses.
- Fully resolve the problem with reusable sessions.
- Break down similar flow logic into helper functions.
- All endpoints now support inline Markdown images.
- Switch large prompts to use BytesIO to avoid reading and writing to disk.
…ith reusable sessions.

- Ensure that PR HanaokaYuzu/Gemini-API#220 is merged before proceeding with this PR.
- Introducing a new feature for real-time streaming responses.
- Fully resolve the problem with reusable sessions.
- Break down similar flow logic into helper functions.
- All endpoints now support inline Markdown images.
- Switch large prompts to use BytesIO to avoid reading and writing to disk.
- Remove duplicate images when saving and responding.
…ith reusable sessions.

- Ensure that PR HanaokaYuzu/Gemini-API#220 is merged before proceeding with this PR.
- Introducing a new feature for real-time streaming responses.
- Fully resolve the problem with reusable sessions.
- Break down similar flow logic into helper functions.
- All endpoints now support inline Markdown images.
- Switch large prompts to use BytesIO to avoid reading and writing to disk.
- Remove duplicate images when saving and responding.
…ith reusable sessions.

- Ensure that PR HanaokaYuzu/Gemini-API#220 is merged before proceeding with this PR.
- Introducing a new feature for real-time streaming responses.
- Fully resolve the problem with reusable sessions.
- Break down similar flow logic into helper functions.
- All endpoints now support inline Markdown images.
- Switch large prompts to use BytesIO to avoid reading and writing to disk.
- Remove duplicate images when saving and responding.
…ith reusable sessions.

- Ensure that PR HanaokaYuzu/Gemini-API#220 is merged before proceeding with this PR.
- Introducing a new feature for real-time streaming responses.
- Fully resolve the problem with reusable sessions.
- Break down similar flow logic into helper functions.
- All endpoints now support inline Markdown images.
- Switch large prompts to use BytesIO to avoid reading and writing to disk.
- Remove duplicate images when saving and responding.
@Vigno04

Vigno04 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

The gemini-fastapi docker container starts successfully, initializes the Gemini client, and passes periodic health checks. However, when sending a POST request to the /v1/responses endpoint, the application crashes with a 500 Internal Server Error and throws an ASGI exception.
Logs / Stack Trace:
gemini-fastapi | 2026-07-30 08:35:40.631 | DEBUG | app.utils.logging:setup_logging:40 - Logger initialized. gemini-fastapi | 2026-07-30 08:35:40.631 | INFO | __main__:<module>:38 - Starting server at http://0.0.0.0:8000 ... gemini-fastapi | 2026-07-30 08:35:40.645 | INFO | uvicorn.server:_serve:91 - Started server process [7] gemini-fastapi | 2026-07-30 08:35:40.645 | INFO | uvicorn.lifespan.on:startup:48 - Waiting for application startup. gemini-fastapi | 2026-07-30 08:35:41.015 | DEBUG | gemini_webapi.utils.get_access_token:get_access_token:97 - HTTP Request: GET https://www.google.com [200] (HTTP/V3) gemini-fastapi | 2026-07-30 08:35:41.017 | DEBUG | gemini_webapi.utils.load_browser_cookies:load_browser_cookies:37 - Optional dependency 'browser-cookie3' not found. Skipping browser cookie loading. gemini-fastapi | 2026-07-30 08:35:41.474 | DEBUG | gemini_webapi.utils.get_access_token:_send_request:41 - HTTP Request: GET https://gemini.google.com/app [200] (HTTP/V3) gemini-fastapi | 2026-07-30 08:35:41.477 | DEBUG | gemini_webapi.utils.get_access_token:get_access_token:295 - Init attempt (1) from Cache succeeded. gemini-fastapi | 2026-07-30 08:35:41.690 | DEBUG | gemini_webapi.client:_batch_execute:2245 - HTTP Request: POST https://gemini.google.com/_/BardChatUi/data/batchexecute [200] (HTTP/V3) gemini-fastapi | 2026-07-30 08:35:41.692 | INFO | gemini_webapi.client:_fetch_user_status:537 - Account status: AVAILABLE - Account is authorized and has normal access. gemini-fastapi | 2026-07-30 08:35:42.513 | INFO | gemini_webapi.client:_fetch_quota:675 - Account quota updated: Gemini Flash [None-11] - 47689/48384 credits remaining (Resets: 2026-07-30 10:05:19 UTC) gemini-fastapi | 2026-07-30 08:35:42.676 | INFO | gemini_webapi.client:_fetch_quota:675 - Account quota updated: Gemini Pro [None-4] - 47688/48384 credits remaining (Resets: 2026-07-30 10:05:19 UTC) gemini-fastapi | 2026-07-30 08:35:42.806 | INFO | gemini_webapi.client:_fetch_extra_quota:800 - Extra quota check: Blocked=False, UsageLevel=0 gemini-fastapi | 2026-07-30 08:35:42.930 | INFO | gemini_webapi.client:_fetch_abuse_status:722 - Account abuse status: Clean (No flags detected). gemini-fastapi | 2026-07-30 08:35:43.079 | INFO | gemini_webapi.client:_fetch_usage_info:902 - Usage info updated gemini-fastapi | 2026-07-30 08:35:43.080 | SUCCESS | gemini_webapi.client:init:329 - Gemini client initialized successfully. gemini-fastapi | 2026-07-30 08:35:43.080 | INFO | app.server.chat:refresh_available_models_cache:983 - Cached 3 available model(s). gemini-fastapi | 2026-07-30 08:35:43.081 | INFO | app.services.lmdb:_init_environment:164 - LMDB environment initialized at data/lmdb gemini-fastapi | 2026-07-30 08:35:43.081 | INFO | app.main:_run_retention_cleanup:30 - Starting LMDB retention cleanup task gemini-fastapi | 2026-07-30 08:35:43.081 | INFO | app.main:lifespan:77 - Gemini clients initialized: ['Gemini_Davide']. gemini-fastapi | 2026-07-30 08:35:43.081 | INFO | app.main:lifespan:78 - Gemini API Server ready to serve requests. gemini-fastapi | 2026-07-30 08:35:43.081 | INFO | uvicorn.lifespan.on:startup:62 - Application startup complete. gemini-fastapi | 2026-07-30 08:35:44.228 | INFO | uvicorn.protocols.http.httptools_impl:send:485 - 127.0.0.1:57354 - "GET /health HTTP/1.1" 200 gemini-fastapi | 2026-07-30 08:37:13.214 | DEBUG | gemini_webapi.client:start_activity_watchdog:412 - Heartbeat triggered. Time since last activity: 91s ... gemini-fastapi | 2026-07-30 08:41:14.777 | INFO | uvicorn.protocols.http.httptools_impl:send:485 - 127.0.0.1:40978 - "GET /health HTTP/1.1" 200 gemini-fastapi | 2026-07-30 08:41:29.223 | DEBUG | app.server.chat:_convert_responses_to_app_messages:869 - Normalized Responses input: 1 message items. gemini-fastapi | 2026-07-30 08:41:29.224 | INFO | uvicorn.protocols.http.httptools_impl:send:485 - 172.18.0.11:44966 - "POST /v1/responses HTTP/1.1" 500 gemini-fastapi | 2026-07-30 08:41:29.224 | ERROR | uvicorn.protocols.http.httptools_impl:run_asgi:427 - Exception in ASGI application gemini-fastapi | gemini-fastapi | Traceback (most recent call last): gemini-fastapi | gemini-fastapi | File "/app/run.py", line 39, in <module> gemini-fastapi | uvicorn.run( gemini-fastapi | │ └ <function run at 0xf1c30b90a0c0> gemini-fastapi | └ <module 'uvicorn' from '/app/.venv/lib/python3.13/site-packages/uvicorn/__init__.py'> ... gemini-fastapi | File "/app/.venv/lib/python3.13/site-packages/starlette/routing.py", line 660, in __call__ gemini-fastapi | await self.middleware_stack(scope, receive, send) gemini-fastapi | │ │ │ │ └ <function wrap_app_handling_exceptions.<locals>.wrapped_app.<locals>.sender at 0xf1c309715c60> gemini-fastapi | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.httptools_impl.RequestResponseCycle object at 0xf1c3096... gemini-fastapi | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.18.0.20', 8000), '... gemini-fastapi | │ └ <bound method APIRouter.app of <fastapi.routing.APIRouter object at 0xf1c30998e8b0>> gemini-fastapi | └ <fastapi.routing.APIRouter object at 0xf1c30998e8b0>

@luuquangvu

Copy link
Copy Markdown
Collaborator Author

@Vigno04 Because the log is truncated, I can't see the exact error. I can only guess from error 500 that the cause might be due to incorrect parameters you provided. Please provide more detailed information so I can understand why the error occurred.

@Vigno04

Vigno04 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

config are copied from the repo with just api key and stuff like that added, this is the logs:

gemini-fastapi-sanitized.log

@luuquangvu

Copy link
Copy Markdown
Collaborator Author

@Vigno04 I’ve made a small update to address the issue. Please try updating now to confirm if it resolves the problem.

@Vigno04

Vigno04 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

unfortunatelly not:
error.log

@luuquangvu

Copy link
Copy Markdown
Collaborator Author

@Vigno04 The two log files show different errors. Which client are you using to connect?

@Vigno04

Vigno04 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

I'm using openwebui sending "ciao" with no extra tool activa other than the base ones, i did docker compose down and docker compose up -d then sent the message and registered the logs

@luuquangvu

Copy link
Copy Markdown
Collaborator Author

@Vigno04 I’ve made a quick update to fix the issue. Go ahead and update now to see if it solves the problem.

@Vigno04

Vigno04 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

it was still broken but i may have found a fix, the problem was just with the responses format and i was able to fix it by changing the line 1562 in chat .py from:
"tools": request.tools or [],
to
"tools": [t.model_dump(exclude_none=True) if hasattr(t, "model_dump") else t.dict(exclude_none=True) if hasattr(t, "dict") else t for t in request.tools] if request.tools else [],

@luuquangvu

Copy link
Copy Markdown
Collaborator Author

@Vigno04 I’ve made an update to fix another issue with the strict schema validator rejecting null fields. Go ahead and update now to see if it resolves the problem.

@Vigno04

Vigno04 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

fixed thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants