I’m experiencing an issue with the GPT-4 API where a single image upload results in an unexpectedly high token count, leading to a “Request too large” error.
Setup
- Flutter frontend sends images via multipart/form-data.
static Future<dynamic> analyzeImages(List<File> images) async {
var uri = Uri.parse('$baseUrl/analyze_images/');
var request = http.MultipartRequest('POST', uri);
for (File image in images) {
var stream = http.ByteStream(image.openRead());
var length = await image.length();
var multipartFile = http.MultipartFile('images', stream, length, filename: image.path.split('/').last);
request.files.add(multipartFile);
}
var response = await request.send();
if (response.statusCode == 200) {
var responseData = await response.stream.bytesToString();
return json.decode(responseData);
} else {
throw Exception('Failed to analyze images');
}
}
- Django backend encodes images in base64 and sends them to the GPT-4 API.
@csrf_exempt
def analyze_images(request):
if request.method == 'POST':
images = request.FILES.getlist('images')
if images:
responses = []
headers = {
'Authorization': f'Bearer sk-proj-',
'Content-Type': 'application/json',
}
for image in images:
print(f"Processing image: {image.name}")
image_content = image.read()
print(f"Image size (bytes): {len(image_content)}")
encoded_image = base64.b64encode(image_content).decode('utf-8')
print(f"Encoded image size (chars): {len(encoded_image)}")
json_data = {
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are an AI that analyzes images."},
{"role": "user", "content": "Analyze this image:"},
{"role": "user", "content": f"data:image/jpeg;base64,{encoded_image}"}
],
"max_tokens": 500,
"temperature": 0.5
}
json_data_str = str(json_data)
print(f"Request JSON size (chars): {len(json_data_str)}")
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=json_data)
responses.append(response.json())
return JsonResponse({'results': responses})
else:
return JsonResponse({'error': 'No images provided'}, status=400)
else:
return JsonResponse({'error': 'Invalid request method'}, status=400)
Details:
- Image size: 1 MB
- Encoded image size: 1.4 million chars
- JSON request size: 1.4 million chars
- Resulting in 354,654 tokens.
The image is normal-sized, and I followed the official documentation. Is there a known issue or a specific encoding method required to reduce the token count?
Logs:
Processing image: example.jpg
Image size (bytes): 1063892
Encoded image size (chars): 1418524
Request JSON size (chars): 1418774
The weirdest thing is that when I do it by URL with the same image the tokens are 85 not 350.000, what I’m saying is that something must be wrong otherwise they wouldn’t put that you can upload images locally but only by URL.
Any guidance on resolving this issue would be appreciated.