How to get AI Agent to output raw data

Describe the problem/error/question

Is there a way to get the AI Agent node to output the raw result? Currently, it outputs the result as a string inside a JSON object, like so:

[
   {
      "output": 
      "As of the most recent data, the top 5 most populated countries in the world are:\n\n1. **China** - Approximately 1.41 billion people\n2. **India** - Approximately 1.39 billion people\n3. **United States** - Approximately 332 million people\n4. **Indonesia** - Approximately 276 million people\n5. **Pakistan** - Approximately 225 million people\n\nKeep in mind these numbers are continually changing due to births, deaths, and other factors."
   }
]

The issue with this is that connecting a structured output parser that structures the response (if the response is JSON) fails because it’s a string.

Here’s an example of the response with a JSON answer:

[
{
"output": 
"{\n "countries": [\n {\n "name": "China",\n "population": 1411778724\n },\n {\n "name": "India",\n "population": 1387297452\n },\n {\n "name": "United States",\n "population": 331893745\n },\n {\n "name": "Indonesia",\n "population": 276362965\n },\n {\n "name": "Pakistan",\n "population": 225199937\n }\n ]\n}"
}
]

Given this, is there a way to get the AI Agent node to only output the raw result, so that it can work with the Structured Output Parser?

Please share your workflow

Information on your n8n setup

  • n8n version: 1.47.0
  • Database (default: SQLite): N/A
  • n8n EXECUTIONS_PROCESS setting (default: own, main): unchanged
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu

Hello @Praxder

The JSON.parse($json.output) method should be able to parse it

Is there a way to do that between the AI Agent and Structured Output Parser?

Well, as I get it, the Output parser is how the model should return data. But I’m not an AI expert

I figured it out. For anyone in the future with the same issue: the problem was that the JSON Schema I was passing to the Structured Output Parser was pretty complex and it had trouble parsing it. Simplify your schema until you find the property that won’t map.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.