Skip to content
mentoor

Topic 1 of 5 · 4 min read

Rewriting follow-up questions into search queries

Goal: Rewrite a follow-up chat message into a standalone search query and show the retrieval difference it makes.

A retriever sees one string, so every follow-up question must be rewritten into a standalone query before it is searched. The user types "what about for contractors?" and the index has no idea what "what about" refers to.

Check yourself · 1 of 3

A user's second message is "what about the cheaper one?". Why does searching with that exact text fail?

Choose one answer

Why it matters

Chat users write the way people talk: pronouns, ellipsis, typos, shorthand. Embedding search and keyword search both match on the words in the query. "How long does it take?" embeds close to nothing in particular, so you retrieve the most generic chunks, and the model then answers confidently from the wrong text. The failure looks like a bad model but it is a bad query.

How to do it

Add one cheap model call before retrieval. Give it the last few turns and the new message, and ask for a single query that makes sense with no history.

  1. Resolve references. Replace "it", "that", "the second one" with the named thing from earlier turns.
  2. Fold in constraints from earlier turns that still apply, such as the product, the region or the plan.
  3. Fix spelling and expand shorthand. "pto polcy" becomes "paid time off policy".
  4. Use the documents' vocabulary. If users say "refund" and the manual says "reimbursement", include both.
  5. Return only the query, not an answer. Use a structured output with one field so you can log it.

Keep the original message too. Show the user their own words and send the rewritten text to the search. Log both, because the rewritten query is the first thing you check when an answer is wrong.

Before and after

Weak: the app embeds the raw message.

User: How many vacation days do full-time staff get in Germany?
Assistant: 28 days per year under the German policy.
User: and for contractors?
Search query sent: "and for contractors?"

Fixed: the rewriter produces a standalone query.

Search query sent: "vacation days for contractors in Germany policy"

The first query retrieves anything that mentions contractors, from any country and any topic. The second retrieves the German contractor section.

What goes wrong

  • The rewriter invents a constraint. It adds "Germany" when the user had switched topics. Tell it to carry over only what the new message depends on.
  • The rewriter answers instead of rewriting, and you search with a hallucinated sentence. Constrain the output to one field and a short length.
  • Rewriting a message that is already standalone can drift the meaning. Skip the call when the message has no pronouns or gaps, or let the model return it unchanged.
  • Long history buries the point. Pass the last few turns, or a running summary, not the whole thread.

Examples

Pronoun resolution

History: the user asked about the Pro plan's API rate limit. New message: "Does it reset daily?"

Raw search for "Does it reset daily?" returns billing-cycle pages. Rewritten as "Pro plan API rate limit reset period", it returns the rate-limit page.

Vocabulary mismatch

A user asks "how do I get my money back?" The help center says "reimbursement" and "chargeback". A rewriter prompted to add the documents' terms returns "refund reimbursement request process", which matches the real headings.

Key points

  • A retriever only sees the string you send, so a follow-up like "and for contractors?" must be made standalone first.
  • Rewriting resolves pronouns, carries over still-relevant constraints, fixes typos and uses the documents' vocabulary.
  • Return the rewritten query in a one-field structured output so it cannot turn into an answer and can be logged.
  • Log the original and the rewritten query together; a wrong answer is usually traceable to the query.
  • Do not rewrite blindly: unneeded rewrites and carried-over stale constraints change the meaning.

Common mistakes

  • Sending the raw chat message to the index and blaming the embedding model for poor results.
  • Letting the rewriter answer the question, so the search text is a hallucination.
  • Carrying every earlier constraint forward even after the user changed topic.
  • Not logging the rewritten query, which hides the most common cause of bad answers.

Check yourself · 2 of 3

You add a query-rewriting step. Which output format is safest for the rewriter?

Choose one answer

Check yourself · 3 of 3

The log shows original: "and in France?" rewritten: "holiday policy in France". The earlier topic was expense claims. What is wrong?

Choose one answer

Practice items, not counted towards your mastery.

Start here