How to beginner · 4 min read

Batch classification with OpenAI API

Quick answer
Use the OpenAI API's chat.completions.create method with a batch of classification prompts by sending multiple messages in a single request or iterating over inputs. Batch processing improves throughput and reduces latency when classifying many texts.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the official openai Python package and set your API key as an environment variable for secure authentication.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example demonstrates batch classification by sending multiple user messages in one chat.completions.create call using the gpt-4o model. Each message contains a text to classify with a prompt instructing the model to output the category.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

texts_to_classify = [
    "I love this product!",
    "The movie was boring and too long.",
    "Great customer service and fast shipping.",
    "The food was cold and tasteless."
]

messages = [
    {
        "role": "user",
        "content": f"Classify the sentiment of this text as Positive, Negative, or Neutral:\n\n{txt}"
    }
    for txt in texts_to_classify
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

for i, choice in enumerate(response.choices):
    print(f"Text: {texts_to_classify[i]}")
    print(f"Classification: {choice.message.content.strip()}")
    print("---")
output
Text: I love this product!
Classification: Positive
---
Text: The movie was boring and too long.
Classification: Negative
---
Text: Great customer service and fast shipping.
Classification: Positive
---
Text: The food was cold and tasteless.
Classification: Negative
---

Common variations

  • Async calls: Use async and await with the OpenAI client for concurrency.
  • Streaming: Stream partial classification results with stream=True.
  • Different models: Use gpt-4o-mini for faster, cheaper classification or claude-3-5-sonnet-20241022 for Anthropic API.
python
import asyncio
import os
from openai import OpenAI

async def batch_classify_async(texts):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [
        {"role": "user", "content": f"Classify the sentiment of this text as Positive, Negative, or Neutral:\n\n{txt}"}
        for txt in texts
    ]
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=messages
    )
    for i, choice in enumerate(response.choices):
        print(f"Text: {texts[i]}")
        print(f"Classification: {choice.message.content.strip()}")
        print("---")

if __name__ == "__main__":
    texts = [
        "I love this product!",
        "The movie was boring and too long.",
        "Great customer service and fast shipping.",
        "The food was cold and tasteless."
    ]
    asyncio.run(batch_classify_async(texts))
output
Text: I love this product!
Classification: Positive
---
Text: The movie was boring and too long.
Classification: Negative
---
Text: Great customer service and fast shipping.
Classification: Positive
---
Text: The food was cold and tasteless.
Classification: Negative
---

Troubleshooting

  • If you receive RateLimitError, reduce batch size or add retry logic.
  • If classifications are inconsistent, refine your prompt for clarity.
  • Ensure your API key is set correctly in OPENAI_API_KEY environment variable.

Key Takeaways

  • Batch classification improves throughput by sending multiple inputs in one API call.
  • Use clear, consistent prompts to get reliable classification results.
  • Async calls enable concurrent batch processing for better performance.
  • Adjust model choice based on speed, cost, and accuracy needs.
  • Handle rate limits by controlling batch size and implementing retries.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗