# RAG - Query Transformation and Expansion

> Source: <https://dev.to/ramya_perumal/rag-query-transformation-and-expansion-n6i>
> Published: 2026-07-29 17:27:33+00:00

When a user asks a blended or incomplete query, such as:

"How do I deploy it?"

the LLM uses the background context it already has to transform the query into a more meaningful one.

For example:

"How do I deploy a FastAPI application?"

By transforming the query into a more specific one, the system is able to retrieve more relevant documents from the vector database.

For query transformation to work effectively, the LLM should have some background information about the conversation.

When a user query is converted into an embedding, the point obtained in the vector database may not be close to the most relevant documents.

By expanding the query into different variations, we can retrieve more relevant documents.

**Original User Query**

"How do I deploy a FastAPI application?"

**Expanded Queries**

These are different variations of the original user query.

For each variation of the user query, the system retrieves a set of relevant contexts from the vector database.

During the augmentation phase, the retrieved contexts, along with the original user query, are sent to the LLM so that it can generate more accurate results.

Both **query expansion** and **query transformation** are performed by the LLM.

Instead of calling the LLM for query expansion, we can use a rule-based approach by storing related queries for a user query as a cluster in the vector database.

When a user submits a query, the system retrieves the related queries from the cluster and uses them to improve document retrieval.
