Introduction
In today’s fast-paced data-driven world, the ability to process large volumes of data efficiently is imperative for businesses and developers alike. Whether you’re building a chatbot, analyzing customer feedback, or generating content dynamically, batch processing can save time and resources. This blog post will guide you through implementing efficient batch processing in Python, focusing on how to handle multiple requests to an API while ensuring robust error handling and performance tracking.
Sequential Batch Processing
This function demonstrates how to process API requests sequentially, allowing for clear error handling and performance tracking.
def sequential_batch_processing(client, prompts):
"""
Process requests sequentially.
Args:
client: Gemini client
prompts: List of prompts
Returns:
list: Results
"""
print(f"\n[REFRESH] Sequential Processing ({len(prompts)} requests)")
print("-" * 70)
results = []
start_time = time.time()
for i, prompt in enumerate(prompts, 1):
print(f"Processing {i}/{len(prompts)}...", end=" ")
try:
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=prompt
)
results.append({
"prompt": prompt,
"response": response.text,
"success": True
})
print("[OK]")
except Exception as e:
results.append({
"prompt": prompt,
"error": str(e),
"success": False
})
print(f" ({str(e)[:30]}...)")
duration = time.time() - start_time
print(f"\n Completed in {duration:.2f} seconds")
print(f" Average: {duration/len(prompts):.2f}s per request")
return results
Prerequisites and Setup
Before diving into the implementation, ensure you have the following prerequisites:
📚 Recommended Python Learning Resources
Level up your Python skills with these hand-picked resources:
100 Professional HTML Email Templates | Color and Font Customizer
100 Professional HTML Email Templates | Color and Font Customizer
Complete Gemini API Guide – 42 Python Scripts, 70+ Page PDF & Cheat Sheet – Digital Download
Complete Gemini API Guide – 42 Python Scripts, 70+ Page PDF & Cheat Sheet – Digital Download
ACT Test (American College Testing) Prep Flashcards Bundle: Vocabulary, Math, Grammar, and Science
ACT Test (American College Testing) Prep Flashcards Bundle: Vocabulary, Math, Grammar, and Science
Leonardo.Ai API Mastery: Python Automation Guide (PDF + Code + HTML
Leonardo.Ai API Mastery: Python Automation Guide (PDF + Code + HTML
Parallel Batch Processing
This function illustrates how to process API requests in parallel using `ThreadPoolExecutor`, which can significantly reduce processing time for large batches.
def parallel_batch_processing(client, prompts, max_workers=5):
"""
Process requests in parallel.
Args:
client: Gemini client
prompts: List of prompts
max_workers: Maximum parallel workers
Returns:
list: Results
"""
print(f"\n[FAST] Parallel Processing ({len(prompts)} requests, {max_workers} workers)")
print("-" * 70)
def process_single(prompt):
"""Process single prompt."""
try:
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=prompt
)
return {
"prompt": prompt,
"response": response.text,
"success": True
}
except Exception as e:
return {
"prompt": prompt,
"error": str(e),
"success": False
}
results = []
start_time = time.time()
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {executor.submit(process_single, prompt): i
for i, prompt in enumerate(prompts)}
for future in as_completed(futures):
i = futures[future]
result = future.result()
results.append(result)
status = "[OK]" if result["success"] else ""
print(f"Request {i+1}/{len(prompts)} {status}")
duration = time.time() - start_time
print(f"\n Completed in {duration:.2f} seconds")
print(f" Average: {duration/len(prompts):.2f}s per request")
print(f" Speedup: {len(prompts)/max_workers:.1f}x faster (theoretical)")
return results
- Python 3.x: Make sure you have Python installed on your machine. You can download it from the official Python website.
- Google Gemini API: You’ll need access to Google’s Gemini API. Sign up and obtain your API key to get started.
- Environment Setup: Familiarity with setting up environment variables for sensitive information is essential. Use tools like
dotenvor set them directly in your terminal. - Basic Python Knowledge: A solid understanding of Python basics, including functions, exception handling, and libraries like
concurrent.futures.
Core Concepts Explanation
Understanding the core concepts of batch processing is crucial for implementing an efficient solution. Here are the key ideas:
Error Handling in Batch Processing
This helper function demonstrates how to handle errors gracefully during API requests, ensuring that each prompt’s result is captured regardless of success or failure.
def process_single(prompt):
"""Process single prompt."""
try:
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=prompt
)
return {
"prompt": prompt,
"response": response.text,
"success": True
}
except Exception as e:
return {
"prompt": prompt,
"error": str(e),
"success": False
}
Sequential vs. Parallel Processing
Sequential processing handles requests one at a time, which is easier to manage and debug but can be slow, especially with large datasets. In contrast, parallel processing allows multiple requests to be processed simultaneously, significantly reducing overall processing time. However, it introduces complexity in managing concurrency and potential race conditions.
Error Handling
In batch processing, errors can arise from several sources, including network issues or invalid inputs. Effective error handling ensures that a failure in one request does not halt the entire batch. This can be achieved through try-except blocks and logging mechanisms.
Progress Tracking
Monitoring the progress of batch processes is essential for user experience. Providing feedback on how many requests have been completed helps users understand the status of their operations.
Result Aggregation
Combining the results from each processed request into a coherent structure is vital for further analysis and reporting. This can be accomplished through lists or dictionaries, depending on the data type.
Step-by-Step Implementation Walkthrough
Now that we have a solid understanding of the core concepts, let’s walk through the implementation of batch processing using the Gemini API.
Environment Variable for API Key
This snippet shows how to securely retrieve an API key from environment variables, which is crucial for protecting sensitive information in your code.
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print("\n[X] Error: GEMINI_API_KEY not set")
return
Setting Up the Client
First, you need to initialize the Gemini client. This involves retrieving your API key from the environment variables, ensuring that sensitive information is not hard-coded into your scripts.
Sequential Batch Processing
The first method we will implement is the sequential batch processing function. As shown in the implementation, this function iterates through each prompt, processes it, and appends the results to a list. This approach is straightforward but can be time-consuming for large datasets.
Parallel Batch Processing
Next, we implement parallel batch processing using ThreadPoolExecutor. This allows multiple prompts to be processed simultaneously. As shown in the implementation, this method takes advantage of Python’s threading capabilities to improve performance while managing the number of concurrent threads with the max_workers parameter.
Error Handling in Batch Processing
To ensure that our application remains robust, we create a helper function to handle individual prompt processing. This function captures any errors and logs them while allowing the batch to continue processing. This is vital for maintaining the integrity of the overall operation, as shown in the implementation.
Progress Tracking and Result Aggregation
Throughout the processing, we track the progress and aggregate results into a final data structure. This information is presented to the user, allowing for informed decisions based on the success or failure of each request.
Advanced Features or Optimizations
Once you have the basic batch processing working, consider implementing the following optimizations:
Tracking Progress and Performance
This part of the code provides feedback on the total processing time and average time per request, which is important for performance monitoring in batch processing tasks.
duration = time.time() - start_time
print(f"\n Completed in {duration:.2f} seconds")
print(f" Average: {duration/len(prompts):.2f}s per request")
- Dynamic Throttling: Implement a mechanism to adjust the number of concurrent requests based on API rate limits, ensuring you don’t exceed allowed quotas.
- Retry Logic: Incorporate a retry strategy for transient errors, which can improve the success rate of your requests.
- Logging and Monitoring: Set up logging to capture detailed information about each request and response, helping you diagnose issues in production.
- Performance Metrics: Measure the time taken for each batch process, allowing you to identify bottlenecks and optimize performance further.
Practical Applications
This batch processing framework can be applied in various scenarios, including:
- Content Generation: Automatically generate responses or content for chatbots or customer interactions.
- Data Analysis: Process large datasets for analytics or reporting, handling multiple data points quickly.
- Integration Tasks: Efficiently integrate data from different sources or APIs, enabling seamless workflows.
Common Pitfalls and Solutions
While implementing batch processing, be aware of common pitfalls:
- Network Limitations: Be cautious of network latency and API rate limits. Implement throttling and backoff strategies to mitigate these issues.
- Resource Management: Ensure that your application does not consume too much memory or CPU, especially with large datasets. Monitor resource usage and optimize accordingly.
- Debugging Complexity: Parallel processing can complicate debugging. Use logging generously to trace issues effectively.
Conclusion and Next Steps
In this guide, we explored how to implement efficient batch processing in Python using the Gemini API. By understanding the core concepts and following a structured approach, you can handle large volumes of data efficiently while ensuring robust error handling and performance tracking.
As next steps, consider experimenting with the provided implementations, optimizing them for your specific use cases, and exploring additional batch processing patterns. With practice, you’ll be well-equipped to handle complex data processing tasks in your applications.
Happy coding!
About This Tutorial: This code tutorial is designed to help you learn Python programming through practical examples. Always test code in a development environment first and adapt it to your specific needs.
Want to accelerate your Python learning? Check out our premium Python resources including Flashcards, Cheat Sheets, Interivew preparation guides, Certification guides, and a range of tutorials on various technical areas.


