/n8n-tutorials

How to fix “Cannot read property choices of undefined” in OpenAI node?

Learn how to fix “Cannot read property choices of undefined” in OpenAI Node with updated API steps and quick troubleshooting tips.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to fix “Cannot read property choices of undefined” in OpenAI node?

The error happens because the OpenAI node is outputting a different JSON shape than you expect. The old n8n OpenAI (Chat / Completion) nodes used data.choices, but the new n8n OpenAI node (OpenAI Assistant, Chat Model, etc.) uses data in a different structure, so choices doesn't exist. Fix it by checking the actual output of the node (in the "Execution Data" panel) and updating your expression to match the real path. In most cases, you need either {{ $json.data[0].text }} or {{ $json.choices[0].message.content }}, depending on which OpenAI node type you're using.

 

Why the error occurs

 

The message Cannot read property choices of undefined means:

  • You wrote an expression like {{ $json.choices[0].text }} or {{ $json.data.choices }}
  • But the actual JSON the OpenAI node produced does not contain choices at that path
  • So n8n tries to read it and fails, because choices is undefined

The key detail: n8n doesn’t normalize OpenAI output. The shape depends on the model type you select inside the node.

 

The three real OpenAI output formats in n8n

 

OpenAI nodes in n8n produce one of these **actual** structures:

  • Chat Model (gpt-4, gpt-3.5, etc.)
    Output contains choices, like:
    \`\`\`json { "id": "chatcmpl-123", "choices": [ { "message": { "role": "assistant", "content": "Hello!" } } ] } \`\`\`
  • Completion (legacy text-davinci-003)
    Also uses choices but inside choices[0].text.
  • OpenAI Assistants / Threads
    Does not output choices at all. Structure looks like:
    \`\`\`json { "data": [ { "id": "msg\_123", "content": [ { "type": "text", "text": { "value": "Hello!" } } ] } ] } \`\`\`

If you use expressions written for the first format while your node is returning the third format, you get the error.

 

How to fix it step-by-step

 

  • Open your workflow, run the node once manually.
  • Click the OpenAI node → Execution Data.
  • Look at what the JSON actually contains. Ignore tutorials, ignore assumptions — trust the real output.
  • Update your expression to match the real path.

 

Correct expression examples

 

If your node output shows "choices":

// Chat model
{{ $json.choices[0].message.content }}

// Completion (old)
{{ $json.choices[0].text }}

If your node output shows "data" array (Assistants API):

{{ $json.data[0].content[0].text.value }}

 

A safe pattern to avoid the error in production

 

You can guard against undefined by checking existence:

{{ $json.choices?.[0]?.message?.content || '' }}

This won’t fix wrong paths, but it prevents the workflow from crashing. It’s useful in production where unexpected model changes or partial errors happen.

 

The root cause to remember

 

Every time you change model type in the OpenAI node, the JSON structure may change. n8n does not automatically adapt your expressions. Always check the real output of the node and rewrite expressions accordingly. That is the reliable production fix for “Cannot read property choices of undefined”.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022