How to Properly Append API Responses to an Existing JSON Array?

I’m working with Claude’s API which is stateless and requires sending the full conversation history to the API (alternating “user” and “assistant” messages) like this:

messages=[
        {"role": "user", "content": "Hello, Claude"},
        {"role": "assistant", "content": "Hello!"},
        {"role": "user", "content": "Can you describe LLMs to me?"}
    ]

I have created a “messages” variable that is an array. This variable will hold the prompts and responses and is what I pass into the API as the “messages” part in the above code.

The basic idea is I pass in a prompt to the API, get the response, and add() the response to the array variable so that it now contains the prompt + the response. Then add prompt #2 to the variable, and keep looping through until all prompts have been sent.

I’m having a hard time getting the data correct for this to work.

The first issue is Make telling me I don’t have proper JSON. I’m parsing the response from the HTTP module, and inserting it into a JSON string like so:

Which gives me this error:

OK, so then I decide to create JSON using that content like so:

Then I insert a parse JSON module after it which gives me this:

I then add that to my “messages” variable array, but you can see the structure is now an array within an array, rather than a collection in an array:

Which means, once I loop back through to get the next prompt, I now have an array with:

  1. a collection (prompt 1),
  2. an array (response 1), and
  3. a collection (prompt 2) as seen here:

This obviously then throws an error when trying to pass it into the API.

I tried using the toCollection() function on the response, which didn’t seem to work.

I’m a bit lost on what to do here. :frowning:

Welcome to the Make community!

The collection property, messages, is an array.

You’re not using an array here.

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

Thanks, I fixed that but that didn’t seem to be creating the main issue I was having further downstreem.

What I needed to do was use the Parse JSON module like you detailed in this post to create my own structure so that I could create an object/collection.

Thanks!