DEV Community

Cover image for Align AI Retry Prompts with Topic Validation
Raylabs
Raylabs

Posted on Originally published at raylabs.app

Align AI Retry Prompts with Topic Validation

Publishing workflows often fail when an AI generator suggests internal links that a final validator subsequently rejects. This mismatch occurs because the generator ranks all available candidates by relevance before applying topic constraints, causing high-scoring but irrelevant links to displace valid, lower-scoring options.

Quick solution:

def get_valid_candidates(candidates, topic_filter):
    # Fixture: 2 high-score wrong-topic, 1 lower-score correct-topic
    # Failure mode: Sorting before filtering selects wrong-topic links
    eligible = [c for c in candidates if c.topic == topic_filter]
    return sorted(eligible, key=lambda x: x.score, reverse=True)[:2]

# candidates = [{"score": 10, "topic": "marketing"}, {"score": 9, "topic": "marketing"}, {"score": 5, "topic": "api"}]
# get_valid_candidates(candidates, "api") -> returns [{"score": 5, "topic": "api"}]
Enter fullscreen mode Exit fullscreen mode

The Candidate Selection Failure

When a generator selects links for a retry prompt, it often treats the entire catalog as a flat pool. If your draft discusses "API Security" but the generator ranks two "General Marketing" links higher than a relevant "API Authentication" link, the validator will reject the draft. This happens even if the "API Authentication" link is perfectly eligible. To ensure your links remain useful, you must verify that the link purpose is clear from the anchor text and its surrounding context, as outlined by the W3C Link Purpose in Context guidelines.

Implementing Pre-Ranking Filters

To fix this, you must filter the candidate pool by topic before performing any ranking operations. This ensures that the generator only considers links that meet your publication's topical requirements. If the filtered set is empty, the system should return an empty list rather than falling back to incompatible links. This approach is essential for maintaining high-quality internal linking, which helps search engines understand the relationship between pages, as described in the Google Search Central link best practices. When building these pipelines, you should also consider how to Fix Content Hash Mismatch in Publishing Pipelines to ensure your link selection logic remains consistent across different stages of the pipeline.

Verification Checklist

  • Filter candidates by topic before applying any sorting or ranking logic.
  • Verify that the generator returns an empty list if no candidates match the topic filter.
  • Confirm that the validator rejects any link that does not meet the topic constraint.
  • Test the retry prompt with a fixture containing two high-scoring, wrong-topic links and one lower-scoring, correct-topic link to ensure the correct link is selected.

Top comments (0)