Skip to content

fix(matchers): strip leading headers in context relevance grading - #10568

Open
AbhiPra24 wants to merge 1 commit into
promptfoo:mainfrom
AbhiPra24:fix/context-relevance-header-extraction
Open

fix(matchers): strip leading headers in context relevance grading#10568
AbhiPra24 wants to merge 1 commit into
promptfoo:mainfrom
AbhiPra24:fix/context-relevance-header-extraction

Conversation

@AbhiPra24

Copy link
Copy Markdown

Description

Strip leading markdown headers and preamble labels (e.g., # Extracted sentences, ## Relevant Context:, **Candidate sentences:**, Candidate sentences:, etc.) before splitting sentences so that grader response headers or context headers do not artificially inflate the sentence count denominator or score.

Fixes #10245

Testing

  • Added unit tests in test/matchers/shared.test.ts for stripLeadingHeaders, splitIntoSentences, and splitTextIntoSentences.
  • Added test suite in test/matchers/context-relevance.test.ts verifying context relevance scores without header pollution.

Strip leading markdown headers and preamble labels before splitting sentences to prevent headers from inflating the sentence count denominator or score.

Fixes promptfoo#10245
@AbhiPra24
AbhiPra24 requested review from a team and mldangelo-oai as code owners August 31, 2026 03:59

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

Copy link
Copy Markdown

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: 33700b2cc3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/matchers/shared.ts
Comment on lines +157 to +160
const hasSubsequentContent = lines.slice(startIdx + 1).some((l) => l.trim().length > 0);
if (hasSubsequentContent) {
startIdx++;
continue;

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 Preserve headings when grading array contexts

When context is a string[], matchesContextRelevance counts every non-empty chunk unchanged, but the grader output still passes through this branch. If the first relevant chunk is # Refund policy and the grader echoes it followed by another relevant chunk, this drops the first chunk merely because later output exists, producing a score of 1/2 instead of 2/2. Restrict stripping to recognized grader preambles or normalize the numerator and denominator identically.

AGENTS.md reference: src/matchers/AGENTS.md:L12-L15

Useful? React with 👍 / 👎.

Comment thread src/matchers/shared.ts
Comment on lines +109 to +110
const PREAMBLE_LABEL_PATTERN =
/^(?:#{1,6}\s+)?(?:\*{1,2}|_{1,2}|`+)?\s*(?:(?:candidate|extracted|relevant|selected)\s+(?:(?:sentences?|context|passages?|information|units?)\b(?:\([sS]\))?)|context\s+relevance\b)(?:\s*[:])?(?:\*{1,2}|_{1,2}|`+)?\s*:?\s*/i;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Require a delimiter before removing inline labels

Because the colon is optional and the pattern is not restricted to a label-only line, normal sentences such as Relevant information about Paris is here. or Context relevance matters here. are rewritten to about Paris is here. and matters here.. This splitter is also used for context-faithfulness statements, so those damaged statements are sent to the NLI grader and can change faithfulness verdicts; allow delimiter-free matching only when the whole line is the label.

AGENTS.md reference: src/matchers/AGENTS.md:L12-L15

Useful? React with 👍 / 👎.

@promptfoo-scanner promptfoo-scanner Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 All Clear

Reviewed changes to context relevance matching and shared text utilities. The PR adds header/preamble stripping before sentence splitting and uses the cleaned context in segmentation. No new LLM calls, tools, or capabilities were introduced, and output handling remains non-executable. No LLM security vulnerabilities were identified in these changes.

Minimum severity threshold: 🟡 Medium | To re-scan after changes, comment @promptfoo-scanner
Learn more


Was this helpful?  👍 Yes  |  👎 No 

@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.54%. Comparing base (9cd1924) to head (33700b2).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10568   +/-   ##
=======================================
  Coverage   82.54%   82.54%           
=======================================
  Files         944      944           
  Lines       80640    80687   +47     
  Branches    26959    26974   +15     
=======================================
+ Hits        66562    66605   +43     
- Misses      14078    14082    +4     
Flag Coverage Δ
backend 83.96% <83.33%> (+<0.01%) ⬆️
site 21.82% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

context-relevance score inflated: grader's response header counted as an extracted sentence

1 participant