Citation Formatting for OpenAI Assistant RAG - n8n Workflow

Use this powerful n8n workflow to accurately retrieve and format file citations generated by an OpenAI Assistant using RAG (Retrieval-Augmented Generation) and vector stores.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Developers and technical content creators using Retrieval-Augmented Generation (RAG).

  • Teams needing validated, source-attributed outputs from OpenAI Assistants.

  • n8n specialists looking for advanced API parsing and data manipulation examples.

  • Users searching for robust n8n templates integrating custom formatting.

Overview

When leveraging OpenAI Assistants for RAG tasks, especially when they access files via a vector store, the resulting citations can sometimes be non-standard or lack the necessary context, often appearing as cryptic numerical references or odd characters. This sophisticated n8n workflow solves this issue by taking explicit control of the citation process. After the initial response from the OpenAI Assistant n8n node, the workflow makes a secondary HTTP request to the OpenAI API to retrieve the complete thread details, including full citation metadata.

By dynamically fetching the actual file names associated with the citation IDs and using a custom Code n8n node, this n8n template ensures that every cited reference is replaced with a clear, user-friendly filename. This improves accuracy, enhances readability, and demonstrates the power of the n8n platform for advanced data transformation tasks.

How it Works

This comprehensive n8n workflow initiates execution via the Chat Trigger n8n node, which provides the conversational interface within n8n itself, acting as the primary n8n trigger.


  1. Conversation Start: The Create a simple Trigger (Chat Trigger) receives the user query and passes it to the OpenAI Assistant with Vector Store n8n node. This node uses a pre-configured Assistant ID and an associated Vector Store to generate a RAG-based response.

  2. Full Data Retrieval: Immediately following the assistant's response, an HTTP Request n8n node (Get ALL Thread Content) calls the OpenAI API using the threadId to retrieve the full, detailed JSON content of the conversation thread, which contains the necessary citation annotations.

  3. Data Parsing (Splitting): The workflow uses three consecutive Split Out n8n nodes to dissect the complex API response: splitting messages by iteration, splitting content within messages, and finally, splitting the critical citation annotations.

  4. File Name Lookup: For each individual citation annotation, a subsequent HTTP Request n8n node (Retrieve file name from a file ID) is executed. This uses the citation's file ID to fetch the actual, human-readable filename from the OpenAI API.

  5. Data Structuring: The Regularize output n8n node uses a Set operation to standardize the output structure, gathering the citation ID, the fetched filename, and the original text placeholder that needs replacing.

  6. Aggregation: An Aggregate n8n node collects all processed citation data points into a single item, ensuring all necessary substitution variables are ready.

  7. Final Formatting: The Finnaly format the output Code n8n node takes the original AI response text and iterates through the aggregated citation data, executing a string replacement operation. It replaces the cryptic placeholders with the dynamically retrieved file names (e.g., substituting [0..10] with (filename.pdf)). This advanced logic completes the core purpose of this n8n workflow.

Installation Guide


  1. Import: Copy the provided n8n workflow JSON and paste it into your n8n instance via the 'New' menu -> 'Import from JSON'.

  2. OpenAI Credentials: This n8n workflow requires an OpenAI API Key credential. Ensure this credential is set up and selected within the OpenAI Assistant with Vector Store n8n node, the Get ALL Thread Content n8n node, and the Retrieve file name from a file ID n8n node.

  3. Assistant ID Configuration: The OpenAI Assistant with Vector Store n8n node is pre-configured with a placeholder Assistant ID. You must replace this with the ID of your actual OpenAI Assistant (created within the OpenAI platform) that has associated files and vector stores.

  4. Activate: Save and activate the n8n workflow. Since it uses a Chat Trigger, you can immediately test the flow using the built-in chat interface within the n8n editor interface.

Node Details

Create a simple Trigger (Chat Trigger): The starting n8n trigger for the conversational flow. It captures the user's input and initializes the thread with the OpenAI Assistant.
OpenAI Assistant with Vector Store (Langchain OpenAI node): Executes the core RAG query against a specified Assistant ID and uses the configured vector store for file retrieval.
Get ALL Thread Content (HTTP Request n8n node): Essential for overcoming limitations in the standard output. It retrieves the complete thread JSON data needed to extract detailed citation metadata.
Split Out n8n nodes: Used extensively (Split all message iterations, Split all content, Split all citations) to drill down into the complex response structure and isolate the annotation data.
Retrieve file name from a file ID (HTTP Request n8n node): This n8n node performs an API call to OpenAI's files endpoint to exchange the file ID found in the citation annotation for the actual filename.
Regularize output (Set n8n node): Standardizes the data item to contain only the necessary components: the citation ID, the filename, and the original substitution text.


  • Finnaly format the output (Code n8n node): Contains custom JavaScript logic. This powerful n8n node is responsible for iterating over the aggregated citations and using replaceAll to substitute the citation placeholders in the original text with the human-readable filename, achieving the desired citation formatting.

Related n8n Workflows

Free

Nodes: 10 Nodes
Updated: December 26 2025
View all

Featured*