Dynamically send images to open AI API - wrong JSON as response

Hi!

I want to send pictures to the open AI vision API (https://platform.openai.com/docs/guides/vision ) to ask questions. The amount of pictures can vary. Therefore I wanted to handover the image party dynamically.

Initialization worked fine and I can trigger the API if I use the JSON code I used to create the API connection. I use the standard bubble API connector.

However, when I use any form of dynamical data, I get the following error:

This is basically what I want to do:

As you can see, I tried already various stuff like making it JSON safe and getting rid of the quotation marks since it does not fit to the openAI documentation. The prompt and the model work as expected.

In the arbitrary text I wrote different version of the JSON I wanted to have. Such as image URL retrieval from the database (my prefered way), storing the JSON in a custom state, storing and retrieving the URL and the JSON in the database and doing a search for.

Do you have any idea?

It’s hard to know without the full request payload you are sending. We don’t see what you are doing in your arbitrary text and having a find and replace after format as json-safe sound a bad idea to me.

1 Like

Good point. If you can point me in the right direction how to find the full payload I will provide it.

From the bubble server logs I can find the following.

Unsuccessful run with just using the arbitrary text:

Unsuccessful run with using the arbitrary text and trying to make it JSON safe:

Content example of the arbitrary text:

In this case I just wanted to get the first image record. It has a JSON stored in a text field:

Getting the first image is an example. I tried various dynamic versions.

I used the solution from jmalmeida to get rid of the quotation marks as stated in this post:

I don’t consider this is the correct way. You are adding more step that you don’t need. You already have the JSON created, you don’t need to encode it as json-safe. This is to be used for a string. Don’t forget that encoded as json-safe add surrounding double quotes to string. If the API expect a string, that’s fine, but now you are sending a text, not a JSON object. Also, if you use find and replace to remove the first " and last ", this won’t work because now, all the JSON was escaped.

Send the request you have actually to a requestbin.com for example and inspect the body sent. You will probably see all json error (you can use jsonlint to validate the body)

1 Like

You pointed me in the right direction, thanks! I made it way too complicated after the first tests were unsuccessful.