I need help setting up the AI integration in n8n to pull out specific info from customer messages. I want it to grab things like names and locations, then spit out a neat JSON response.
Here’s what I’m aiming for:
{
"reply": "Hello! What should I call you? 👋",
"extracted_data": [
{
"key": "Location",
"entries": [{ "text": "New York" }]
},
{
"key": "FirstName",
"entries": [{ "text": "Samantha" }]
}
]
}
What’s the best way to write the prompt? And how do I set up the AI node in n8n to always give me this JSON format?
Also, any advice on making sure the output is consistent and easy for my code to read? Thanks!
I’ve actually been working with n8n’s AI integration recently and found a good approach for this. The key is crafting a clear prompt that instructs the AI on exactly what you want extracted and how to format it.
For your case, I’d structure the prompt something like:
‘Extract the following information from the user message: Location, FirstName. Respond in this exact JSON format:
{
“reply”: “[Your generated reply here]”,
“extracted_data”: [
{
“key”: “[Field name]”,
“entries”: [{ “text”: “[Extracted value]” }]
}
]
}
Include all fields, even if empty.’
In the n8n AI node settings, make sure to set the ‘Output Format’ to JSON. This ensures you get consistently structured data.
To further improve consistency, you might consider using a JSON Schema in your subsequent nodes to validate the AI output structure. This can help catch any occasional formatting issues before they cause problems downstream in your workflow.
hey, i’ve been using n8n’s integration too. after the ai node, i use a function node to verify and fix the json struct. it does a quick check to spot issues. might work for u too.
I’ve found that using a two-step approach works well for extracting structured data with n8n’s AI integration. First, craft a detailed prompt that clearly outlines the desired output format and fields to extract. Then, follow the AI node with a Function node to parse and validate the response.
For the prompt, be explicit about the JSON structure you want:
“Extract Location and FirstName. Output in this JSON format: {"reply": "[AI response]", "extracted_data": [{"key": "[field]", "entries": [{"text": "[value]"}]}]}”
In the Function node, implement error checking and reformatting if needed. This ensures consistent output even if the AI occasionally deviates from the requested format.
Remember to set the AI node’s output to JSON and consider implementing retry logic for more robust workflows.