Extracting structured data from OpenAI responses in n8n: How to get custom fields?

I’m working on a project where I need to pull specific info from user messages using the OpenAI tool in n8n. I want to get things like names and places in a neat JSON format.

Here’s what I’m aiming for:

{
  "reply": "Hey there! What should I call you?",
  "extracted_data": [
    {
      "key": "Location",
      "entries": [{ "text": "Miami" }]
    },
    {
      "key": "PersonName",
      "entries": [{ "text": "Sam" }]
    }
  ]
}

What’s the best way to set this up in n8n? I’m not sure how to write the prompt or configure the OpenAI node to always get this structure.

Also, any advice on making sure the output is consistent and easy for my code to read? Thanks!

To achieve a consistent structured output in n8n using OpenAI, you need to design a prompt that instructs the model on the exact JSON structure you require. Begin by defining a custom function within your prompt that mirrors your desired output. Next, configure the OpenAI node to enable function calling and include this function definition. Ensure your prompt clearly explains the data to extract and the output format. You can also add a Code node to post-process the output and implement error handling for unpredictable responses.

Testing with different inputs will help refine and stabilize your workflow.

hey emmad, for consistent structured output try using function calling in the openai node. define a function that matches ur desired json format in the prompt. make sure to explain clearly what data to extract. you might need a code node afterwards to handle any weird responses. good luck with ur project!

I’ve tackled a similar challenge in my work, and here’s what worked for me: Use OpenAI’s function calling feature. It’s a game-changer for getting structured outputs. In your prompt, define a function that mirrors your desired JSON structure. Be explicit about what you want extracted - names, locations, etc. I found it helpful to include examples in the prompt.

One thing to watch out for: sometimes the AI might miss certain fields or add unexpected ones. To handle this, I added a Code node after the OpenAI node to validate and clean up the output. This extra step really improved the consistency of my results.

Also, consider fine-tuning a model if you’re doing this at scale. It can significantly improve accuracy and reduce API costs in the long run.