How to correctly generate a JSON file for the Batch API?

Hi,

I need to convert the image to base64 encoding and then use a URL to send it to the Batch API for processing.
However, I found that the URL transmission cannot be completed. My JSON file contains base64 encoding instead of a URL link. Can someone help me check the code?

Thanks for you help.

The code is as follows:


import base64
import requests
import json
import os

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

folder_path = r'E:\test \image'

def list_image_files(folder_path):
    all_files = os.listdir(folder_path)
    image_files_list = [file for file in all_files if os.path.splitext(file)[1].lower() in [".jpg",".jpeg"]]
    return image_files_list

image_path_list = list_image_files(folder_path)

if os.path.isfile("batch.jsonl"):
    os.remove("batch.jsonl")

for i, image_path in enumerate(image_path_list):

    base64_image = encode_image(image_path)

    payload = {
        "custom_id": f"task-{i+1}",
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
            "model": "gpt-4o",
            "temperature": 0.1,
            "top_p": 0.1,
            "max_tokens": 300,
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": """
Extract the content from the image and separate it into two parts: 
1. Printed problem statement.
2. Handwritten student answers.
"""
                        },
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": f"data:image/jpeg;base64,{base64_image}",
                                "detail": "high"
                            }
                        }
                    ]
                }
            ]
        }
    }
    with open("batch.jsonl", 'a') as file:
        file.write(json.dumps(payload) + '\n')


batch_endpoint = "https://api.openai.com/v1/batches"
headers = {
    "Authorization": f"API-KEY",
    "Content-Type": "application/json"
}

with open("batch.jsonl", 'r') as file:
    tasks = file.readlines()


batch_payload = {
    "tasks": [json.loads(task) for task in tasks]
}

response = requests.post(batch_endpoint, headers=headers, json=batch_payload)

if response.status_code == 200:
    print("Batch request submitted successfully.")
    print(response.json())
else:
    print(f"Failed to submit batch request: {response.status_code}")
    print(response.json())

It is a ‘JSONL file’ instead of a ‘JSON file’. Also, the request function is not needed to create the JSONL file.