n8n

How to Format OpenAI Chat for Source Accuracy?

Turn chat replies into clear, cited answers. This setup helps content and support teams deliver responses that point to real files, with correct file names and optional links. It is useful any time you need proof behind the text, like articles, FAQs, and internal docs.

A chat button in n8n sends the message to an OpenAI assistant that uses a file store for retrieval. The system then pulls the full thread from the OpenAI API so all citations are captured. Messages are split into parts, citations are extracted, and file names are looked up from file ids. The output is normalized, aggregated, and formatted by a code step to insert numbered citations and optional Markdown links. There is also a short memory buffer to keep recent context. You can convert the final Markdown to HTML if you need it.

You need an OpenAI account, an API key, and an assistant that has file retrieval enabled with your documents uploaded. Connect your OpenAI credential in n8n and select the correct assistant. Expect faster content review and fewer errors when checking sources. Use it for content pieces with references, policy or legal notes, and customer answers that must cite PDFs or docs.

What are the key features?

  • Chat trigger inside n8n to test and collect user messages
  • OpenAI assistant with file retrieval for knowledge backed answers
  • HTTP call to fetch full thread messages so all citations are included
  • Split steps to break out messages, content blocks, and citation annotations
  • File lookup call to turn file ids into human friendly file names
  • Set node that standardizes id, filename, and text fields for later steps
  • Aggregate node that combines many citations into a single clean payload
  • Code step that inserts numbered citations and optional Markdown links
  • Optional Markdown to HTML conversion for web ready output
  • Memory buffer window to keep recent chat context for better replies

What are the benefits?

  • Reduce manual citation cleanup from 30 minutes to under 5 minutes per answer
  • Improve source accuracy by centralizing file ids to real file names
  • Automate up to 80 percent of formatting work for cited responses
  • Support many chat sessions at once with aggregated handling of citations
  • Deliver HTML or Markdown outputs without extra editing

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with OpenAI. See the Tools Required section above for links to create accounts with these services.
  3. In your OpenAI account, create an API key from the API settings page and store it securely.
  4. In the OpenAI platform, create an assistant and enable file retrieval. Upload or connect your documents to the assistant file store.
  5. In the n8n credentials manager, create a new OpenAI credential. Paste your API key, name the credential clearly, and save.
  6. Open the OpenAI Assistant node in the workflow. Select your OpenAI credential and choose your assistant from the list.
  7. Open the chat trigger node and enable it so you can test messages from the n8n chat panel.
  8. Send a test message in the chat panel. Confirm the assistant responds and that a thread is created.
  9. Open the HTTP Request node that pulls thread messages. Ensure it uses your OpenAI credential and the thread id from the previous step.
  10. Verify the Split nodes: one splits thread data into messages, another splits message content, and another splits text annotations for citations.
  11. Open the HTTP Request node that retrieves file names. Make sure it uses your OpenAI credential and reads the file id from the citation.
  12. Check the Set node to ensure id, filename, and text are mapped correctly for each citation.
  13. Confirm the Aggregate node is set to combine all items so the code step can process a single payload.
  14. Review the Code node. Adjust how citations render and add Markdown links to files if needed. Enable the Markdown to HTML node if you want HTML output.
  15. Run a full test in the chat panel. You should see numbered citations with correct file names. If citations are missing, confirm file retrieval is enabled on the assistant, verify your documents are in the file store, and ensure the request includes the Assistants v2 beta header setting.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

OpenAI

Sign up

Pay-as-you-go: GPT-5 at $1.25 per 1M input tokens and $10 per 1M output tokens

Credits:
Davi Saranszky Mesquita LinkedIn

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.