How to fix "role" property error in Pipedream when setting up prior message history with OpenAI?

This topic was automatically generated from Slack. You can find the original thread here.

I’m using pipedream with openai, and I’m trying to set up prior message history. I keep getting the following error:

"Configuration error

The following message doesn't have a "role" property:"

I have tried using this command

`{{steps.chat.$return_value.original_messages_with_assistant_response}}

as is outlined in this blog post. I have also tried entering it manually:
[{"role":"user","content":"Hello"},{"role":"assistant","content":"Hello! How can I assist you today?"}]
I keep getting the same configuration error.
What am I doing wrong?

Hi dan!

The prop input field can accept strings or evaluate JS code. So when you input {{steps.chat.$return_value.original_messages_with_assistant_response}} it will evaluate the return value given from the chat step, taking the original_messages_with_assistant_response key in the resulted object.
So can you confirm that the chat step returned object has the original_messages_with_assistant_response key?

You can also input it manually, as you mentioned. But for it to be evaluated as JS, it should be wrapped in {{ }}, as so: {{[{"role":"user","content":"Hello"},{"role":"assistant","content":"Hello! How can I assist you today?"}]}}

Thanks for responding.

Yes, it returns as follows:
Configuration error
The following message doesn't have a "role" property:
[
{
"role": "user",
"content": "Hello"
},
{
"role": "assistant",
"content": "Hi there! How may I assist you today?"
}
]
When I enter manually in this format:
{{[{"role":"user","content":"Hello"},{"role":"assistant","content":"Hello! How can I assist you today?"}]}}
I get back
"SyntaxError
Invalid or unexpected token"

I tried by copying and pasting the manual code, and ran successfully.
Are you able to take a screenshot of the step? Feel free to block any sensitive information

Ah, the value for {{steps.chat.$return_value.original_messages_with_assistant_response}} is an array. Can you please try this?

{{steps.chat.$return_value.original_messages_with_assistant_response[0]}}

Yes! that worked.
Can you explain this?
I want to build a chat with a data store that updates the chat history, based on previous results. How can I do this if the result I result I update the data store with returns an error. How would I add those numbers you added in my data store automatically?
Thank You

Great to hear!
That’s just the initial debugging, because yeah, actually it shouldn’t be this way.
How are you saving the response to the data store?

After openai generates content, I append it to a data record. In the workflow, before openai I include ‘get record’ from data store. In prior messages I input the result from ‘get record’.

Thanks for the screenshots, they are very helpful.
So in the second image, instead of passing the value through Construct an array, can you please try in Enter a custom expression? Because currently it is passing inside an array with length 1

The result was the same.
I tried as follows and it worked

You’ll probably have to delete the current records in the data store so it gets fresh results

With this method I am only retrieving one result from the dataset. How would I write in a few numbers
for example 1 to 18
{{steps.get_record_or_create.$return_value[1,18]}}
Thank You for your help.

In your data store, you can save an array of objects like {"role":"user","content":"Hello"} , start with an empty array [] . Then in the end of your workflow execution, you can append the response in the data store object and save it again. For example:

  1. Get array of prior messages from data store
  2. Pass array to OpenAI with your prompt
  3. Create a new object with your prompt and the OpenAI response, e.g. priorMessagesArray.push([{"role": "user", "content": "your prompt"}, {"role": "assistant": "response from OpenAI"}])
  4. Save the updated array back to the data store
    Then as you workflow executes, you can append the response

My question is, being that I can’t pass the whole array to prior message history like:
{{steps.get_record_or_create.$return_value}}
only by writing the actual value like:
{{steps.get_record_or_create.$return_value[18]}}
How can I write the last 2 values
I want to write something like this:
{{steps.get_record_or_create.$return_value[-1, -2]}}

For that, .slice() would work