Skip to content

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

Open
luuquangvu wants to merge 310 commits into
Nativu5:mainfrom
luuquangvu:main
Open

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

Conversation

@luuquangvu

@luuquangvu luuquangvu commented Mar 21, 2026

Copy link
Copy Markdown
Collaborator

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.

Close #103 , #113 , #129 , #130 , #133 , #139, #143 , #149 , #150 , #154

…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.
Caused by Gemini Web's post-processing mechanism.
Caused by Gemini Web's post-processing mechanism.
Caused by Gemini Web's post-processing mechanism.
Caused by Gemini Web's post-processing mechanism.
@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

luuquangvu and others added 12 commits August 12, 2026 23:50
…=false to avoid cold start delay and update workflows
…ion recovery, script restore (#6)

* Survivors from reconciled fork (PR #5 follow-up): C1 v1beta, SSRF guard, truncation recovery, model-resolution fix, start script

vs luuquangvu/main:
- app/server/gemini.py + app/models/gemini_models.py + app/models/__init__.py + app/main.py + app/server/middleware.py: native Gemini /v1beta REST surface (models, generateContent, streamGenerateContent, x-goog-api-key auth, Google error envelopes) - unobjected in review
- app/utils/helper.py + app/utils/config.py + config/config.yaml: SSRF guard (reject_unsafe_url, allow_private_url_fetch, url_fetch_timeout); recovery_timeout knob
- app/services/client.py + app/server/chat.py: truncated-stream recovery via LIST_CONVERSATION_TURNS polling; fix chat.py:688 calling nonexistent client.resolve_model (lib: _resolve_model_by_name)
- scripts/start-gemini-api.sh: restore local start/stop helper (deleted in 71c0db2 with no replacement)
- app/services/pool.py: extract _init_one/_init_attempt (behavior-preserving)

* Drop unused video-generation helper from PR surface (dead against luuquangvu models)

* Remove VideoGeneration import (dead on PR surface)

* Fix CI: move MIME registry below imports and use contextlib.suppress

Ruff E402/SIM105 in app/main.py (self-introduced by the survivors main.py
edit): the mimetypes loop ran before module imports and used try/except-pass.
Imports first, loop after (still at import time, before any upload), no noqa
needed. All 4 CI steps now pass: ruff check, ruff format, ty, pyright.

* Update the PR to align with the current flow:

- removing _recover_stream_output, as it’s now handled upstream.
- removing fetch_last_model_turn since it’s already included upstream.
- eliminating the unnecessary looks_like_html function.
@luuquangvu

Copy link
Copy Markdown
Collaborator Author

@Nativu5 The PR is ready to be merged, as the upstream library has incorporated new features related to it (HanaokaYuzu/Gemini-API#310). Since it was created some time ago, I’ve updated and improved it multiple times, which has made it a bit large. I also removed one feature from main, compaction, as it turned out to be less efficient than expected. Feel free to review the PR anytime. Thank you very much!

@luuquangvu
luuquangvu marked this pull request as ready for review August 16, 2026 17:13
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.

Image generation quality is significantly lower than Gemini Web

3 participants