Bubble API Call to OpenAI (GPT-4o) – JSON Formatting Issue

Hi everyone,

I’m currently integrating OpenAI’s GPT-4o API into my Bubble app but I’m running into an issue with the JSON formatting when making API requests. I keep getting a HTTP 400 error saying:

“We could not parse the JSON body of your request. The OpenAI API expects a JSON payload, but what was sent was not valid JSON.”

**Current Setup in Bubble:

[
{
“role”: “system”,
“content”: “You are an AI assistant.”
},
{
“role”: “user”,
“content”: Reflexion_Input’s value
}
]
**

  • I’m using the ChatGPT - Chat Completion plugin.
  • The body message field currently looks like this:

json

KopierenBearbeiten

[
  {
    "role": "system",
    "content": "You are an AI assistant."
  },
  {
    "role": "user",
    "content": Reflexion_Input's value
  }
]
  • Issue: The Reflexion_Input’s value is coming from a Bubble input field, but it seems that it is not properly formatted as JSON. I tried using :formatted as JSON-safe, but I can’t find this option in Bubble.

Questions:

  1. How do I correctly format the input field value as valid JSON?
  2. Is there a manual way to write the request body as a JSON string while inserting the input field dynamically?
  3. What’s the best practice in Bubble to ensure OpenAI receives a valid JSON request?

Would appreciate any help or guidance!

Can you share screenshot of your settings?

hello i can help you to solve this issue bcs i have 5+ years of experience on bubble.io.

let me send you DM

did you get this resolved? running into a similar issue

this is what I use, yours gave me your error, probably because it is missing messages . One problem might be your quotes are curly quotes

{
“model”: “gpt-4-turbo”,
“messages”: [
{
“role”: “system”,
“content”: “You are a helpful assistant.”
},
{
“role”: “user”,
“content”:
}
]
}

:format as json safe should be available on any string. It will surround the string in quotes so you shouldnt include include quotes around your variable if you are using it in the json template.