API Batch file creation - 'Collection' object is not callable

Hello,
The following code from the documentation and the cookbook gives me the error :
TypeError: ‘Collection’ object is not callable. If you meant to call the ‘create’ method on a ‘Database’ object it is failing because no such method exists.

Did I miss something obvious or is it an API problem ?


 from openai import OpenAI
 client = OpenAI(api_key='XXXXXXXXX', )
 data = [
  {"custom_id": "request-1", 
   "method": "POST", 
   "url": "/v1/chat/completions", 
   "body": {"model": "gpt-3.5-turbo-0125", 
            "messages": [
                {
                    "role": "system", 
                 "content": "You are a helpful assistant."
                 },
                {
                    "role": "user", 
                 "content": "Hello world!"
                 }
                ],
            "max_tokens": 1000
            }},
  {"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo-0125", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
  ]
  
 file_name = "new_file.jsonl"
 with open(file_name, 'w') as file:
     for obj in data:
          file.write(json.dumps(obj) + '\n')
  
batch_input_file = client.files.create(
    file=open(file_name, "rb"),
    purpose="batch"
  )

PS : trying to figure out how to post formatted code…

Welcome to the Forum!

I tried with the following code and faced no issues:

from openai import OpenAI
import os
import json

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY","sk-proj-..."))


data = [
    {"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo-0125", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user","content": "Hello world!"}],"max_tokens": 1000}},
    {"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo-0125", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
]

file_name = "new_file.jsonl"
with open(file_name, 'w') as file:
    for obj in data:
        file.write(json.dumps(obj) + '\n')


batch_file = client.files.create(
  file=open(file_name, "rb"),
  purpose="batch"
)

print(batch_file)

Btw, to paste code here in the Forum, just click on the small settings/widget icon and select preformatted text.

Hi,
Thank you for your answer, it’s strange because when I run exactly your code, I still have the same error… I will retry in an hour with another computer.

1 Like

Do you have the latest version of the OpenAI Python library installed?

2 Likes

Hi,
I did the update just in case.
It seems there is some randomness in this : I was able to run the code @jr.2509 posted and then my code after (with a more complex dataset). And a few hours laters, I had the same error with the code from @jr.2509
So it seems the problem is “solved” :smiley:

1 Like