Maintaining Email Thread with OpenAI and Gmail in n8n

Hello everyone,

I’ve set up my workflow to automatically respond to incoming Gmail emails using an OpenAI assistant. It works well initially. However, I’ve encountered an issue during testing: the workflow doesn’t maintain the email thread.

For example:

  • The client sends an initial inquiry.
  • The agent needs more information and asks for it.
  • When the client replies with the missing information, the agent treats it as a new, separate inquiry instead of continuing the existing conversation.

I believe this can be resolved with a Code node, but I’m unsure how to configure it. When I examine the Code node’s output, it appears to be empty.

Could someone please advise me on how to properly configure the Code node to maintain the email thread?

Thank you!

Are you using the memory sub-node in your workflow? Memory should help you keep track of the thread. In this case, you may need to get the ID of the thread from your gmail nodes, and use that as the session key in your memory. It would look something like this:

Replace the “Simple Memory” node with something durable, that doesn’t lose all contents when n8n restarts (if you already have Postgres running, you can use that, or choose any of the other services).