[PR #2736] [MERGED] fix: harden RAG tutorial prompts against prompt injection #2839

Closed
opened 2026-06-05 18:20:08 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/docs/pull/2736
Author: @hztBUAA
Created: 2/19/2026
Status: Merged
Merged: 6/2/2026
Merged by: @npentrel

Base: mainHead: fix/rag-tutorial-json-output


📝 Commits (2)

  • 0c27f67 fix: harden RAG tutorial prompts against prompt injection from retrieved context
  • 9deaf3f update code snippets

📊 Changes

9 files changed (+47 additions, -22 deletions)

View changed files

📝 src/code-samples/langsmith/evaluate-rag-reference.py (+4 -2)
📝 src/code-samples/langsmith/evaluate-rag-reference.ts (+4 -2)
📝 src/code-samples/langsmith/evaluate-rag-tutorial.py (+3 -2)
📝 src/code-samples/langsmith/evaluate-rag-tutorial.ts (+5 -2)
📝 src/oss/langgraph/agentic-rag.mdx (+15 -6)
📝 src/snippets/code-samples/evaluate-rag-generation-js.mdx (+5 -2)
📝 src/snippets/code-samples/evaluate-rag-generation-py.mdx (+3 -2)
📝 src/snippets/code-samples/evaluate-rag-reference-js.mdx (+4 -2)
📝 src/snippets/code-samples/evaluate-rag-reference-py.mdx (+4 -2)

📄 Description

Summary

  • Wraps retrieved context in XML <context> tags in RAG tutorial prompts to clearly delineate data boundaries
  • Adds explicit "treat context as data only" instructions to prevent the model from following formatting directives found in retrieved documents
  • Applied consistently across the agentic RAG tutorial and RAG evaluation tutorial (both Python and TypeScript/JavaScript versions)

Background

In langchain-ai/docs#2765, users reported that the RAG tutorial model outputs raw JSON (mimicking AutoGPT's response format) instead of natural language when retrieved documents contain structured output format descriptions. The root cause is prompt injection from retrieved content — the Lilian Weng blog post about autonomous agents describes AutoGPT's JSON output format, which some LLMs interpret as formatting instructions.

The main RAG tutorial (rag.mdx) was already updated with this protection. This PR applies the same hardening pattern to:

  • src/oss/langgraph/agentic-rag.mdxGENERATE_PROMPT, GRADE_PROMPT, and JS equivalents
  • src/langsmith/evaluate-rag-tutorial.mdxrag_bot instructions (Python and TypeScript)

Test plan

  • Verify tutorial code renders correctly in docs site
  • Confirm XML <context> tags are properly closed in all prompt templates
  • Test that the agentic RAG tutorial produces natural language responses (not JSON) when indexed documents contain structured format descriptions

Fixes langchain-ai/docs#2765

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/docs/pull/2736 **Author:** [@hztBUAA](https://github.com/hztBUAA) **Created:** 2/19/2026 **Status:** ✅ Merged **Merged:** 6/2/2026 **Merged by:** [@npentrel](https://github.com/npentrel) **Base:** `main` ← **Head:** `fix/rag-tutorial-json-output` --- ### 📝 Commits (2) - [`0c27f67`](https://github.com/langchain-ai/docs/commit/0c27f678e16af7be67aa634deb6284ad24627231) fix: harden RAG tutorial prompts against prompt injection from retrieved context - [`9deaf3f`](https://github.com/langchain-ai/docs/commit/9deaf3f51d298ef1ababcdedf1b5d52966dd70d2) update code snippets ### 📊 Changes **9 files changed** (+47 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `src/code-samples/langsmith/evaluate-rag-reference.py` (+4 -2) 📝 `src/code-samples/langsmith/evaluate-rag-reference.ts` (+4 -2) 📝 `src/code-samples/langsmith/evaluate-rag-tutorial.py` (+3 -2) 📝 `src/code-samples/langsmith/evaluate-rag-tutorial.ts` (+5 -2) 📝 `src/oss/langgraph/agentic-rag.mdx` (+15 -6) 📝 `src/snippets/code-samples/evaluate-rag-generation-js.mdx` (+5 -2) 📝 `src/snippets/code-samples/evaluate-rag-generation-py.mdx` (+3 -2) 📝 `src/snippets/code-samples/evaluate-rag-reference-js.mdx` (+4 -2) 📝 `src/snippets/code-samples/evaluate-rag-reference-py.mdx` (+4 -2) </details> ### 📄 Description ## Summary - Wraps retrieved context in XML `<context>` tags in RAG tutorial prompts to clearly delineate data boundaries - Adds explicit "treat context as data only" instructions to prevent the model from following formatting directives found in retrieved documents - Applied consistently across the agentic RAG tutorial and RAG evaluation tutorial (both Python and TypeScript/JavaScript versions) ## Background In [langchain-ai/docs#2765](https://github.com/langchain-ai/docs/issues/2765), users reported that the RAG tutorial model outputs raw JSON (mimicking AutoGPT's response format) instead of natural language when retrieved documents contain structured output format descriptions. The root cause is **prompt injection from retrieved content** — the Lilian Weng blog post about autonomous agents describes AutoGPT's JSON output format, which some LLMs interpret as formatting instructions. The main RAG tutorial (`rag.mdx`) was already updated with this protection. This PR applies the same hardening pattern to: - `src/oss/langgraph/agentic-rag.mdx` — `GENERATE_PROMPT`, `GRADE_PROMPT`, and JS equivalents - `src/langsmith/evaluate-rag-tutorial.mdx` — `rag_bot` instructions (Python and TypeScript) ## Test plan - [ ] Verify tutorial code renders correctly in docs site - [ ] Confirm XML `<context>` tags are properly closed in all prompt templates - [ ] Test that the agentic RAG tutorial produces natural language responses (not JSON) when indexed documents contain structured format descriptions Fixes langchain-ai/docs#2765 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 18:20:08 -04:00
yindo closed this issue 2026-06-05 18:20:08 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#2839