Skip to content

⚡ perf: Optimize FastAPI endpoint with async Gemini API call#8

Open
Sir-Ripley wants to merge 1 commit intomainfrom
perf/async-gemini-call-18003193398847644355
Open

⚡ perf: Optimize FastAPI endpoint with async Gemini API call#8
Sir-Ripley wants to merge 1 commit intomainfrom
perf/async-gemini-call-18003193398847644355

Conversation

@Sir-Ripley
Copy link
Owner

@Sir-Ripley Sir-Ripley commented Mar 14, 2026

💡 What: Changed the synchronous model.generate_content(prompt) method call to the asynchronous await model.generate_content_async(prompt) method inside the async def consult() FastAPI endpoint in ChronoHolographicCipher.ipynb.

🎯 Why: Calling a blocking, synchronous I/O function (like a network request to an API) inside an asynchronous function blocks the entire event loop. This severely degrades the performance and throughput of the server when handling concurrent requests. Making the call asynchronous allows the event loop to yield control and process other incoming requests while waiting for the API response.

📊 Measured Improvement: A local benchmark simulation was created to test the execution time of 3 concurrent requests to a mock API that takes 1 second to respond.

  • Baseline (Sync): 3 concurrent requests took ~3.00 seconds.
  • Improvement (Async): 3 concurrent requests took ~1.00 second.
  • Change over baseline: 3x speedup in handling concurrent requests for this specific test case. By not blocking the event loop, the server can now process multiple /consult_astheria requests concurrently instead of sequentially.

PR created automatically by Jules for task 18003193398847644355 started by @Sir-Ripley

Summary by Sourcery

Enhancements:

  • Replace the synchronous Gemini content generation call with an asynchronous variant within the consult endpoint to avoid blocking the event loop and improve concurrent request handling.

… endpoint

In `ChronoHolographicCipher.ipynb`, the `consult()` FastAPI endpoint was
blocking the event loop by calling the synchronous `generate_content()` method
of the Gemini model. This change replaces it with `generate_content_async()`,
awaiting the result, which allows the server to handle concurrent requests
without blocking.

Co-authored-by: Sir-Ripley <31619989+Sir-Ripley@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 14, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates a FastAPI async endpoint in ChronoHolographicCipher.ipynb to use the Gemini client’s asynchronous generate_content_async API instead of the blocking synchronous generate_content call, eliminating event-loop blocking and improving concurrent request throughput.

Sequence diagram for async Gemini generate_content_async call in consult endpoint

sequenceDiagram
    actor User
    participant FastAPIApp
    participant ConsultEndpoint
    participant GeminiModel
    participant GeminiAPI

    User->>FastAPIApp: HTTP POST /consult_astheria
    FastAPIApp->>ConsultEndpoint: invoke async consult(prompt)
    ConsultEndpoint->>GeminiModel: await generate_content_async(prompt)
    GeminiModel->>GeminiAPI: async request prompt
    GeminiAPI-->>GeminiModel: response
    GeminiModel-->>ConsultEndpoint: response
    ConsultEndpoint-->>FastAPIApp: JSON result
    FastAPIApp-->>User: HTTP 200 response
Loading

File-Level Changes

Change Details Files
Switch the Gemini content generation call in the async FastAPI consult endpoint from a blocking sync API to the non-blocking async API.
  • Replace model.generate_content(prompt) with await model.generate_content_async(prompt) inside the async def consult() endpoint
  • Ensure the call site is awaited so it properly cooperates with the event loop and allows concurrent handling of requests
ChronoHolographicCipher.ipynb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on a critical performance enhancement for a FastAPI endpoint by refactoring a blocking external API call to an asynchronous one. This change ensures that the server's event loop remains non-blocked during I/O operations, thereby substantially improving the application's throughput and responsiveness, especially when handling multiple concurrent user requests.

Highlights

  • Performance Optimization: Switched a synchronous Gemini API call to its asynchronous counterpart within a FastAPI endpoint to prevent blocking the event loop.
  • Concurrency Improvement: This change significantly improves the server's ability to handle concurrent requests, demonstrated by a 3x speedup in a local benchmark for concurrent API calls.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • ChronoHolographicCipher.ipynb
    • Updated the Gemini API call from synchronous to asynchronous within the consult() function.
Activity
  • PR created automatically by Jules for task 18003193398847644355 started by @Sir-Ripley.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly optimizes the FastAPI endpoint by switching to an asynchronous Gemini API call, which will improve performance under concurrent loads. My review includes a critical security comment regarding a hardcoded API key that is used for this API call. This key should be removed from the source code and managed securely.

"\n",
" # Sending the vibe out and waiting for the echo to return\n",
" response = model.generate_content(prompt)\n",
" response = await model.generate_content_async(prompt)\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-critical critical

This line makes an asynchronous call to the Gemini API. The model object used for this call is authenticated using an API key that is hardcoded on line 125. Committing secrets like API keys directly into source code is a critical security vulnerability. If this code is committed, the key will be exposed in the repository's history and can be discovered and abused, leading to unauthorized access and potential charges to your account. The key should be removed from the source code and loaded from a secure source, such as environment variables (injected at runtime) or a dedicated secrets management service.

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.

1 participant