Using variables inside the System Prompt of AI Agent

Describe the problem/error/question

I am passing in a persona variable to the AI Agent in the $json.body.assistant variable, and when I tell the system prompt to use it, it doesn’t seem to pick it up properly.

What is the error message (if any)?

I select a mermaid persona, and it thinks its a pirate.

Please share your workflow

image

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.80.3
  • Database (default: SQLite): windows buffer memory
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: windows10

Hi, you will have to set system message to an expression to allow variable access, if you haven’t already.

I found using an expression similar to
{{ JSON.stringify( $(“AI Agent”).item.json.output ) }}
in the system prompt allowed it to pull in the data from the previous node and send it to the ai.

A variation that I used was {{ JSON.stringify($json.promptdata[0][“1”]) }}

I believe it was because the data I was sending needed to be converted to a single string/text line.

Yeah, thats also true, you must always convert the object to a string when providing to the model.

What I prefer to use as its easier than wrapping the expression is:
.toJsonString() on the object you want to convert.

But the main error of the OP is that they didn’t enable the input to accept expressions.