IA chat - answer not properly displayed

:bullseye: What is your goal?

Creating an AI chat, fed internal data, powered by OpenAI and articulated through Make.com,

:thinking: What is the problem & what have you tried?

I have an AI chat powered by a make scenario. All goes well : the question is transfered, the data to answer selected, the answer compiled by an OpenAI module and then sent to a webhook to be sent back to the chat.

However, the answer doesn’t reach it and I only get an error message. What is weirder is that when I put the text content of the JSON instead of the JSON itself, it gets transfered fine to the chat.


Module 1 : Webhook / receives the question asked in Chat

Module 2 : OpenAI / selects the theme of the question among set choices

Module 3 : Airtable / gathers data relevant to the theme previously identified

Module 4 : Text aggregator / compiles data in one text

Module 5 : OpenAI 2 / answers the question based on acquired data

Module 6 : Webhook 2 / sends the answer back to the chat

:camera_with_flash: Screenshots (scenario flow, module settings, errors)

Additional info :

  • I have no more luck using {{5.choices.message.content}} (or any other JSON from the OpenAI module)
  • I have try replacing potentially problematique part of the code (ex : {{replace(5.choices[1].message.content; “\n”; “\n”)}} )
  • I have tried using a text aggregator as a buffer between the openai and webhook modules

None of those fixed the issue

Welcome to the Make community!

Escaping strings before mapping into another JSON string

Most of the time, you cannot directly map variables containing text content into another JSON string without escaping them, as the variable might contain special characters that are reserved. Special characters in JSON needs to be escaped (encoded) as a “literal” character, otherwise they make the whole JSON invalid when you map the variable value when creating another JSON string.

You can escape string variables by passing the text into the “Transform to JSON” module. You can also build a valid JSON string by using a Data Structure with the “Create JSON” module. Then, you can map the output of the Transform module into another JSON string, or simply use the whole output of the Create module.

:warning: Note:

When using the “Transform to JSON” module, the output should already contain the double quotes " " around your text, so when you map this output into your JSON, you should not need to wrap another pair of double quotes around it.

Alternatively, you can use the built-in function replace to replace those special characters with their escaped versions. To do this, simply paste this into the field, and replace the 1.text variable:

{{ replace(replace(1.text; "/([""\\])/g"; "\$1"); "/\r?\n\r?/g"; "\n") }}

For more information on JSON and escaping JSON strings, see:

Hope this helps! If you are still having trouble, please provide more details.

— @samliew
P.S.: investing some effort into the tutorials in the Make Academy will save you lots of time and frustration using Make!