{
  "name": "Reliable AI agent output without structured output parser - w/ OpenAI & Switch",
  "nodes": [
    {
      "id": "",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -400,
        760
      ]
    },
    {
      "id": "",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -800,
        760
      ]
    },
    {
      "id": "",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -400,
        920
      ]
    },
    {
      "id": "",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -260,
        920
      ]
    },
    {
      "id": "",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        860,
        760
      ]
    },
    {
      "id": "",
      "name": "Validate Output + Set `aiRunIndex`",
      "type": "n8n-nodes-base.set",
      "position": [
        260,
        760
      ]
    },
    {
      "id": "",
      "name": "Format Schema Error Prompt",
      "type": "n8n-nodes-base.set",
      "position": [
        920,
        1020
      ]
    },
    {
      "id": "",
      "name": "Valid Schema Output",
      "type": "n8n-nodes-base.set",
      "position": [
        1460,
        760
      ]
    },
    {
      "id": "",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        600,
        -260
      ],
      "parameters": {
        "width": 520,
        "height": 1140,
        "content": "## Switch\n\n## ⚠️ Warning : 🔁 **Infinite loop risk**\n\nIf you **modify this node without understanding** the expressions inside, it can cause an infinite loop, which is **exactly what you don’t want** f"
      }
    },
    {
      "id": "",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -480,
        -160
      ],
      "parameters": {
        "width": 500,
        "height": 1040,
        "content": "## AI Agent\n\n\nThis node sets an **AI agent** to generate nutritional information based on a food name provided via `chatInput`.\n\n\n---\n\n## How it works:\n\n- 💬 **Prompt definition:**\n  The agent is instr"
      }
    },
    {
      "id": "",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        60,
        -160
      ],
      "parameters": {
        "width": 500,
        "height": 1040,
        "content": "## Validate Output + Set `aiRunIndex`\n\nThis node **validates** the AI output to ensure it matches the expected nutrition schema,  \nand sets a helper variable `aiRunIndex` for retry tracking.\n\nOf cours"
      }
    },
    {
      "id": "",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1160,
        360
      ],
      "parameters": {
        "width": 700,
        "height": 520,
        "content": "## Valid Schema Output\n\n\nThis node stores the **validated nutritional data** under a clean variable name: `nutritionalValues`.\n\n\n---\n\n## What it does:\n\n- Takes the parsed and validated `output` from e"
      }
    },
    {
      "id": "",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1160,
        940
      ],
      "parameters": {
        "width": 700,
        "height": 600,
        "content": "## Output Handling (Valid & Invalid Schema)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## Set schemaValidationError & lastAgentOutput\n- Stores the output of the node \"Validate Output + Set `aiRunIndex`\" in a variable called `sc"
      }
    },
    {
      "id": "",
      "name": "Set schemaValidationError & lastAgentOutput",
      "type": "n8n-nodes-base.set",
      "position": [
        1280,
        1020
      ]
    },
    {
      "id": "",
      "name": "Set chat Output",
      "type": "n8n-nodes-base.set",
      "position": [
        1640,
        1020
      ]
    },
    {
      "id": "",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1580,
        -60
      ],
      "parameters": {
        "width": 720,
        "height": 940,
        "content": "## Reliable Structured Output from AI Agent *Without* the Structured Output Parser - with OpenAI & Switch node\n\nThis workflow serves as a **solid foundation** when you need an **AI Agent to return out"
      }
    }
  ],
  "connections": {
    "Switch": {
      "main": [
        [
          {
            "node": "Format Schema Error Prompt",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Valid Schema Output",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set schemaValidationError & lastAgentOutput",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Validate Output + Set `aiRunIndex`",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Format Schema Error Prompt": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Output + Set `aiRunIndex`": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set schemaValidationError & lastAgentOutput": {
      "main": [
        [
          {
            "node": "Set chat Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}