Skip to content

AI Search: Add how-to guide for chunk citations in AI responses#31684

Open
G4brym wants to merge 2 commits into
productionfrom
ai-search/chunk-citations-how-to
Open

AI Search: Add how-to guide for chunk citations in AI responses#31684
G4brym wants to merge 2 commits into
productionfrom
ai-search/chunk-citations-how-to

Conversation

@G4brym

@G4brym G4brym commented Jun 25, 2026

Copy link
Copy Markdown
Member

What

Adds a new how-to page under /ai-search/how-to/chunk-citations/ that explains how to extract and display source citations from AI Search responses.

Sections

  • How citations work — explains the flow (retrieve chunks → pass to LLM → return chunks alongside answer)
  • Display citations from chat completions — basic example mapping chunks to citation objects
  • Deduplicate citations by source — group multiple chunks from the same document into a single citation
  • Parse citations from a streaming response — extract the event: chunks SSE event when using stream: true
  • Use scoring details to rank citations — filter low-relevance chunks and show confidence indicators
  • Citation fields reference — table of all chunk fields useful for citations

Notes

All examples use the Workers binding (AiSearchNamespace) and follow the existing how-to page style (TypeScriptExample component, same frontmatter shape).

@cloudflare-docs-bot

cloudflare-docs-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Review

⚠️ 2 warnings, 💡 1 suggestion found in commit 179bd15.

Code Review

This code review is in beta and may not always be helpful — use your judgment.

Warnings (2)
File Issue
ai-search/how-to/chunk-citations.mdx line 233 Double close of streaming writer — On failure, finally at line 229 already closes the writer, then the .catch(() => writer.close()) at line 233 attempts to close it again. The second close() can reject with an unhandled promise rejection. Fix: Remove the .catch() handler or check the writer state before closing; rely on the finally block to clean up.
ai-search/how-to/chunk-citations.mdx line 207 JSON.parse without error handlingJSON.parse(line.slice(6)) can throw on malformed SSE payloads. The outer .catch only closes the writer, so the client receives a silent, truncated stream. Fix: Wrap the JSON.parse in a try/catch, and either forward an error event or terminate the response with a non-OK status when payload parsing fails.
Suggestions (1)
File Issue
ai-search/how-to/chunk-citations.mdx line 219 Fragile SSE event state handling — After processing the first data: line of a chunks event, currentEvent is reset to "". If a single chunks event spans multiple data: lines, subsequent lines are forwarded raw instead of being transformed. Fix: Reset currentEvent only on empty lines (SSE event boundaries) or when encountering a new event: line, instead of after every data line.

Style Guide Review

✅ No style-guide issues found.

Commands

Only codeowners can run commands. Post a comment with the command to trigger it.

Command Description
/review Runs a review now. Incremental if a prior review exists, full if not.
/full-review Re-reviews the entire PR diff from scratch, ignoring incremental history. Useful after a rebase, when you want a fresh review, or if the bot gets out of sync and reports issues that no longer exist.
/ignore-review-limit Permanently lifts the 2-review automatic limit for this PR. Future pushes will trigger reviews as normal.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/ai-search/ @rita3ko, @irvinebroque, @aninibread, @G4brym, @mchenco, @digizeph, @cloudflare/product-owners

@ask-bonk

ask-bonk Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

I fetched PR #31684's diff via gh pr diff and locally inspected the new chunk-citations.mdx against existing AI Search how-to pages and the Workers binding API docs. I was validating frontmatter, checking for sidebar order collisions (multiple pages use order: 5), and verifying technical accuracy in the streaming/citations examples before finishing the review.

github run

@github-actions

Copy link
Copy Markdown
Contributor

@ALIABDULRAHMANALREHAWI

ALIABDULRAHMANALREHAWI commented Jun 25, 2026 via email

Copy link
Copy Markdown

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants