Skip to main content
Learn how to send multiple prompts in one go using OpenAI’s Python client. This guide covers setup, code structure, and best practices for efficient batch processing.

Table of Contents

  1. Prerequisites
  2. Install & Import
  3. Define Your Prompts
  4. Helper Function for a Single Prompt
  5. Batch Processing Loop
  6. Inspecting Results
  7. Run the Script
  8. Reference Links

Prerequisites

  • Python 3.7+
  • An OpenAI API key
  • openai Python package
Store your API key as an environment variable for security:
Alternatively, pass it directly in code (not recommended for production).

Install & Import

Install the OpenAI Python client:
Then import and initialize the client:

Define Your Prompts

Build a list of user prompts to process in batch:
Feel free to extend this list to dozens or hundreds of items.

Helper Function for a Single Prompt

Encapsulate the API call in a reusable function:

Model Parameters

Batch Processing Loop

Iterate through all prompts and collect responses:
Batch requests can incur higher costs and rate limits. Monitor your usage in the OpenAI Dashboard.

Inspecting Results

Print each prompt alongside its generated response:
Sample output:

Run the Script

Save the code to batch_processing.py and execute:
Extend or customize the process_prompt function to add streaming output, error handling, or alternative model parameters as needed.

Watch Video

Practice Lab