⚡ Bolt: Use AsyncGroq to prevent asyncio event loop blocking#92
⚡ Bolt: Use AsyncGroq to prevent asyncio event loop blocking#92Adityasingh-8858 wants to merge 1 commit into
Conversation
Migrated Groq to AsyncGroq in the `/ai-voice` and `/initiate-transfer` endpoints. This unblocks the FastAPI event loop during network I/O, allowing higher concurrency. Co-authored-by: Deepaksingh7238 <110552872+Deepaksingh7238@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR migrates Groq LLM calls in the FastAPI backend from the synchronous Groq client to the asynchronous AsyncGroq client to avoid blocking the asyncio event loop in async route handlers.
Changes:
- Replaced
GroqwithAsyncGroqinbackend/main.py. - Updated Groq chat completion calls in
/ai-voiceand/initiate-transferhandlers to useawait. - Added a short Bolt learning note documenting the async-client requirement for FastAPI endpoints.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
backend/main.py |
Switches Groq client usage to async and awaits network calls in async endpoints. |
.jules/bolt.md |
Documents the rationale and guidance for using async network clients in async FastAPI handlers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from groq import AsyncGroq | ||
| from livekit import api | ||
| from contextlib import asynccontextmanager |
| global groq_client | ||
| if groq_client is None: | ||
| groq_client = Groq(api_key=GROQ_API_KEY) | ||
| chat_completion = groq_client.chat.completions.create( | ||
| groq_client = AsyncGroq(api_key=GROQ_API_KEY) | ||
| chat_completion = await groq_client.chat.completions.create( |
| global groq_client | ||
| if groq_client is None: | ||
| groq_client = Groq(api_key=GROQ_API_KEY) | ||
| groq_client = AsyncGroq(api_key=GROQ_API_KEY) |
💡 What
Migrated the synchronous
GroqAPI client to the asynchronousAsyncGroqclient inbackend/main.py. Addedawaitto network calls within theai_voiceandinitiate_transferasync route handlers.🎯 Why
Using a synchronous network client inside an asynchronous FastAPI endpoint blocks the main asyncio event loop. Under load, this prevents the application from handling other concurrent requests efficiently, severely degrading performance. Moving to an async client eliminates this bottleneck.
📊 Impact
Prevents entire event loop freezes during Groq API latency. Increases concurrent request handling capacity dramatically without adding threads.
🔬 Measurement
Verify by running load tests against the
/ai-voiceor/initiate-transferendpoints and monitoring event loop blocking metrics. Also verified by running the existingpytesttest suite, which continues to pass.PR created automatically by Jules for task 664387477567385875 started by @Deepaksingh7238