ai newsDi., 1. Dez.

Stop pasting retrieved docs into your prompt as JSON: wrap each one in tags (Monday AI Insight)

Monday AI Insight. One practical prompting lesson for RAG and long-document apps: how you package the context is part of the prompt. Retrieved chunks dumped in as a JSON array, or glued together with blank lines, make the model work harder to tell where one source ends, which source said what, and

dev.toMontag 05:29 · 30.11.3 Min. Lesezeit
Bild: dev.to

Monday AI Insight. One practical prompting lesson for RAG and long-document apps: how you package the context is part of the prompt. Retrieved chunks dumped in as a JSON array, or glued together with blank lines, make the model work harder to tell where one source ends, which source said what, and where your instructions start.

Answer the question using these documents: [{"id": 1, "title": "Refund policy", "content": "..."}, {"id": 2, "title": "Billing FAQ", "content": "..."},...] Question: Can I get a refund after 30 days? Enter fullscreen mode Exit fullscreen mode It works in a demo. Then the context grows to dozens of chunks, some chunks contain quotes, code or their own JSON, and answers start mixing sources, citing the wrong document, or treating text inside a document as an instruction.

OpenAI, GPT-4.1 Prompting Guide (Delimiters section). For adding a large number of documents or files to the input context, OpenAI reports from its long context testing:

The same guide adds a caveat worth remembering: if the documents you retrieve contain lots of XML themselves, an XML delimiter will likely be less effective. Pick a delimiter that stands out from your content.

Anthropic, Claude prompting best practices. XML tags help Claude parse prompts that mix instructions, context, examples and variable inputs. Use consistent, descriptive tag names and nest them when content has a hierarchy. For multiple documents, Anthropic's example wraps each one in <document index="n"> with <source> and <document_content> subtags inside a <documents> block.

The two vendors give slightly different advice on placement, so know which model you're on:

A layout that satisfies both: short instructions first, then the tagged documents, then the question and a one-line restatement of the key rules.

You answer questions using only the documents below. If they don't contain the answer, say so. <documents> <document index="1"> <source>refund-policy.md</source> <document_content>... </document_content> </document> <document index="2"> <source>billing-faq.md</source> <document_content>... </document_content> </document> </documents> First, copy the sentences that answer the question into <quotes> tags with their document index. Then answer in <answer> tags, citing the index for each claim. Question: Can I get a refund after 30 days? Enter fullscreen mode Exit fullscreen mode A small Python helper:

from html import escape def format_docs ( chunks ): parts = [ "<documents>" ] for i, c in enumerate ( chunks, 1 ): parts. append ( f ' <document index="{i}">' ) parts. append ( f " <source>{escape(c['source'])}</source>" ) parts. append ( f " <document_content>\n{c['text']}\n </document_content>" ) parts. append ( " </document>" ) parts. append ( "</documents>" ) return "\n". join ( parts ) Enter fullscreen mode Exit fullscreen mode Two details in that pattern come straight from Anthropic's guidance:

Tags are structure, not security. Wrapping a retrieved web page in <document> does not stop instructions hidden inside it from being followed. Treat that as a separate problem (I wrote about it in my prompt-injection posts, linked below). Tags make the boundary clear; your architecture has to enforce it.

Takeaway: your model reads the context exactly as you packaged it. Give every document a clear start, end and source, and put the question where your model expects it.

What do you use to separate retrieved docs in your prompts today: JSON, XML, markdown, or just blank lines?

Quelle: dev.to (dev.to). Original öffnen