Skip to content

fix: skip reference instructions in LLM prompt when include_references=false#2833

Open
majiayu000 wants to merge 2 commits intoHKUDS:mainfrom
majiayu000:fix/issue-2832-skip-reference-prompt-when-disabled
Open

fix: skip reference instructions in LLM prompt when include_references=false#2833
majiayu000 wants to merge 2 commits intoHKUDS:mainfrom
majiayu000:fix/issue-2832-skip-reference-prompt-when-disabled

Conversation

@majiayu000
Copy link
Copy Markdown

Description

When include_references=False, the structured references field is correctly nulled, but the LLM system prompt still contains reference formatting instructions (steps 1.4-1.6, section 4, section 5) and the context still includes the "Reference Document List" section. This causes the LLM to generate reference markers in the response text even though the API strips the structured references field.

Related Issues

Fixes #2832

Changes Made

  1. lightrag/prompt.py — Added no-reference variants of prompt templates:

    • rag_response_no_ref: rag_response without reference tracking instructions, References Section Format, and Reference Section Example
    • naive_rag_response_no_ref: Same treatment for naive_rag_response
    • kg_query_context_no_ref: kg_query_context without "Reference Document List" block and reference_id mentions in Document Chunks header
    • naive_query_context_no_ref: Same treatment for naive_query_context
  2. lightrag/operate.py — Conditional prompt selection:

    • _build_context_str: Selects no-ref system prompt and context template when include_references=False; skips building reference_list_str
    • kg_query: Selects no-ref system prompt when include_references=False and no custom system_prompt provided
    • naive_rag: Same treatment for naive query mode
  3. tests/test_include_references_prompt.py — 10 offline tests verifying:

    • No-ref prompt variants omit reference instructions
    • Default prompts retain reference instructions
    • Template placeholders are valid

Checklist

  • Changes tested locally
  • Code reviewed
  • Documentation updated (if necessary)
  • Unit tests added (if applicable)

Additional Notes

All 10 new tests pass. Pre-existing test failures in test_postgres_index_name.py are unrelated (postgres-specific tests that fail without asyncpg installed).

…s=false

When include_references=false, the LLM prompt still contained reference
formatting instructions (sections 4-5, reference_id tracking steps) and
the context included the Reference Document List. This caused the LLM to
generate reference markers in response text even though the API stripped
the structured references field.

Add no-reference prompt variants that omit reference instructions and
context templates that omit Reference Document List. Conditionally select
these variants in kg_query, _build_context_str, and naive_query when
include_references is disabled.

Closes HKUDS#2832

Signed-off-by: majiayu000 <1835304752@qq.com>
@majiayu000 majiayu000 marked this pull request as ready for review March 24, 2026 10:58
@danielaskdd
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d6bd724483

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

sys_prompt_temp = system_prompt if system_prompt else PROMPTS["rag_response"]
if system_prompt:
sys_prompt_temp = system_prompt
elif not query_param.include_references:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add include_references to kg_query cache key

This branch makes kg_query generate a different prompt when include_references is false, but the query cache key still omits that flag in the subsequent compute_args_hash(...) call. With enable_llm_cache=true, a response cached for one setting can be served to the other (e.g., cached cited answer returned when citations are disabled), violating the API contract for include_references.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed in f956920 — added include_references to the compute_args_hash call for kg_query.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed in f956920 — added include_references to the compute_args_hash call for kg_query.

When enable_llm_cache=true, kg_query and naive_query could return
stale cached responses generated under the opposite include_references
setting. Add include_references to compute_args_hash inputs for both
functions so the cache distinguishes between the two modes.

Signed-off-by: majiayu000 <1835304752@qq.com>
@danielaskdd
Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielaskdd danielaskdd added the tracked Issue is tracked by project label Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tracked Issue is tracked by project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: References are included in the response even if include_references=false

2 participants