Error while uploading File via python open ai api

Getting error while uploding file

Error: Error code: 500 - {‘message’: ‘Unexpected token 'L', “LS1lODQyNT”… is not valid JSON’}

Code is simple

from openai import OpenAI , OpenAIError

client = OpenAI(
# In order to use provided API key, make sure that models you create point to this custom base URL.
base_url=‘’,
# The temporary API key giving access to ChatGPT 4o model. Quotas apply: you have 500’000 input and 500’000 output tokens, use them wisely :wink:
api_key=<‘api-key’>
)

completion = client.chat.completions.create(
model=“gpt-4o”,
messages=[
{“role”: “user”, “content”: “Hello!”}
]
)
vector_store = client.vector_stores.create(name=“Product Catalog”)
try:
client.files.create(
file=open(“data.jsonl”, “rb”),
purpose=“assistants”
)
except OpenAIError as e:

Handle all OpenAI API errors

print(f"Error: {e}")
#vector_store_file = client.vector_stores.files.create(

vector_store_id=vector_store.id,

file_id=“file-abc123”

#)
tried with multiple file types

Perhaps you need to explain a little better what your are trying to do.

Using a JSONL for a vector store makes no sense, as they are supposed to perform semantic text searches, broken into little chunks to allow searching.

But if you are just trying to understand how a vector store works on a RAG, you can have a look at this cookbook where it is explained in detail:

And as an alternative, you can use file inputs directly avoiding the need of creating a vector store, depending on your use case.

1 Like

hi

I am getting the same error while uploading the file directly or using vector store

Error with products.pdf: Error code: 500 - {‘message’: ‘Unexpected token 'L', “LS0yYjVmNW”… is not valid JSON’}

code snippet below

from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
import concurrent
import PyPDF2
import os
import pandas as pd
import base64

def upload_single_pdf(file_path: str, vector_store_id: str):
    file_name = os.path.basename(file_path)
    try:
        file_response = client.files.create(file=open(file_path, 'rb'), purpose="assistants")
        attach_response = client.vector_stores.files.create(
            vector_store_id=vector_store_id,
            file_id=[file_response.id](http://file_response.id)
        )
        return {"file": file_name, "status": "success"}
    except Exception as e:
        print(f"Error with {file_name}: {str(e)}")
        return {"file": file_name, "status": "failed", "error": str(e)}
vector_store = client.vector_stores.create(name="Product Catalog")
vresp = upload_single_pdf('products.pdf', [vector_store.id](http://vector_store.id))

That looks weird.

Is your PDF a valid one? Try creating the vector store manually to check it out first.

hi
I am able to create a vector store manually and upload the same pdf file directly with no issues

have you tried updating your openai package?
pip install --upgrade openai

Was that a typo or really your code?