High severity intermediate · Fix: 2-5 min

OpenAIError

openai.OpenAIError (embedding batch size too large)

What this error means
The OpenAI API rejects embedding requests when the batch size exceeds the model or endpoint limits, causing an error.

Stack trace

traceback
openai.OpenAIError: The batch size for embeddings is too large. Reduce the number of inputs per request and try again.
QUICK FIX
Reduce your embedding batch size to a smaller number (e.g., 16 or 32) and retry the request.

Why it happens

Embedding endpoints have strict limits on the number of input texts processed per request. Sending too many texts in one batch exceeds these limits, triggering an API error. This protects the service from overload and ensures consistent latency.

Detection

Monitor API error responses for OpenAIError messages indicating batch size limits exceeded. Log batch sizes before sending embedding requests to catch oversized batches early.

Causes & fixes

1

Sending too many texts in a single embedding request exceeding API batch size limits

✓ Fix

Split the input texts into smaller batches within the allowed size before calling the embedding API.

2

Not respecting model-specific maximum tokens or input length constraints in batch

✓ Fix

Check and enforce model documentation limits on tokens and input length per batch, adjusting batch size accordingly.

3

Using a generic batch size without dynamically adjusting for input length or API feedback

✓ Fix

Implement dynamic batching logic that adapts batch size based on input length and API error responses.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
texts = ["text1", "text2", ..., "text100"]  # Large batch

# This line triggers the batch size too large error
response = client.embeddings.create(model="text-embedding-3-large", input=texts)
print(response)
Fixed - works correctly
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
texts = ["text1", "text2", ..., "text100"]  # Large batch

# Split texts into smaller batches to avoid batch size error
batch_size = 16
responses = []
for i in range(0, len(texts), batch_size):
    batch = texts[i:i+batch_size]
    response = client.embeddings.create(model="text-embedding-3-large", input=batch)
    responses.extend(response.data)
print(responses)  # Now works without batch size error
Split the large input list into smaller batches within API limits to avoid exceeding the maximum allowed batch size.

Workaround

Catch the OpenAIError exception, then automatically retry the embedding request with smaller batch sizes until successful.

Prevention

Implement batching logic that respects the documented maximum batch size and input length limits for embedding models, and monitor API error messages to adjust dynamically.

Python 3.9+ · openai >=1.0.0 · tested on 1.5.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.