Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.6 — Answer Formats
RAG and Vector Search · Example 2 of 2

Streaming an answer without showing half a citation

The chunks are scripted to split the markers on purpose. The vertical bars show where each piece would reach the screen.

Prose is not always the right output. Quotes, steps and a validated JSON object with an answer, citations and a confidence let a product show sources and let code check the model. Streaming needs one extra care: hold back half-finished citation markers.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write quote_ok(quote, source) that returns True when the quote appears in the source after lower-casing both and collapsing runs of whitespace to a single space. Print the result for the quote "unlimited rides" and "free rides" against "The monthly pass costs 25 dollars and includes unlimited rides of up to 45 minutes.".

02

Write next_step_ok(reply) that parses a JSON string and returns "ok" if it has a non-empty list steps of strings, else "bad" (also for invalid JSON). Print the result for '{"steps": ["Open the app", "Tap Forgot password"]}', '{"steps": []}' and 'oops'.

Explain it without notes

01

When would you ask for a quote, steps or JSON instead of prose?

02

What are the layers of validating a JSON answer?

03

Why is a model's own confidence not enough?

04

What goes wrong when you stream an answer with citations, and how do you fix it?

Streaming an answer without showing half a citation
Sign in to run this example in your browser.

Expected output

naive:  The pass costs 25 dol|lars [|1]. Rides last up to 45 minutes [|2|].
naive pieces that end inside a marker: 2
safe:   The pass costs 25 dol|lars |[1]. Rides last up to 45 minutes |[2].
safe pieces that end inside a marker: 0
citations: 1,2, all valid: yes