Cannot use a file in post request to openai embedding endpoint

Hi all,

When I use this script with a valid bearer token,

with open('test.json', 'r') as f:
    jsonReport = json.load(f)

# print(jsonReport)
data = {
    "input": jsonReport,
    "model": "text-embedding-ada-002",
    "encoding_format": "float"
}

headers = {'Content-Type':'application/json', 'Authorization':'Bearer ' }
x = requests.post(url, data=json.dumps(data), headers=headers)
print(x.text)

I receive this error:

{
  "error": {
    "message": "'$.input' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

this is an example json I am using so its not filesize related…

{
    "element1": ["value1", "value2", "value3"],
    "element2": ["value4", "value5", "value6"],
    "element3": ["value7", "value8", "value9"],
    "element4": ["value10", "value11", "value12"]
  }
  

I have also tried reading the json as a string and using a var, same issue.

its late and I suspect its my python thats the issue?

thanks

did you check the url?

https://platform.openai.com/docs/api-reference/embeddings/create?lang=curl

input needs to be a string or an array - you’re passing an entire object…

Thanks, hadn’t realised that - still picking up python and its a million times better than java, but occasionally stuff like this catches me out.

It was the object issue.
I fixed with “input”: json.dumps(jsonReport) for the benefit of others

thanks :slight_smile: