Reasoning Patterns and Structured Schemas
You can run a six-step chain on a short shop note, assign one label from a fixed taxonomy, and reject a JSON object that breaks a schema you check by hand.
- Level
- Navigator
- Time
- 18 h

Jump to
After this course you can
The student can build a multi-step extraction and classification pipeline that produces 100% schema-valid JSON with exact source line citations across 50 frozen multimodal documents.
Six steps, written before the object
Chain-of-thought, in this course, is a fixed list of six steps that you write out before the JSON object. Step 1 numbers every non-empty source line, starting at 1, in reading order. Step 2 computes net. Step 3 computes tax and gross. Step 4 picks one label. Step 5 builds the list of cited line numbers. Step 6 writes the object and a stop line. Each step shows its inputs and its one result. When the gross in the object disagrees with the gross in step 3, you reject the reply at that step. A bare total, with the arithmetic left unwritten, gives you no line to check.
The money rule is the same on every note. Read the lines from the top and take the first line that contains Qty, then a whole number Q, then at, then a number U, then each. In every note here, U is written with two decimal places. That line is nQ, and net = Q times U. If no line has that shape, net is null and nQ is absent. If net is a number, take the first fragment that contains tax, then a whole number R, then percent. Then tax = net times R / 100 and gross = net + tax, and nT is the line that held the fragment. If net is a number and the fragment is absent, tax is null and gross is null. If net is null, tax is null and gross is null, and you leave the tax fragment unread. These commitments are K1, K2, and K3. K1: a null net forces a null tax and a null gross. K2: a missing tax fragment forces a null tax and a null gross. K3: a found fragment sets tax and gross by the two formulas. K4, used in step 6: lines must equal the list from step 5, in that order.
Practice the arithmetic on one line: Qty 4 at 12.50 each, tax 25 percent. Write C1 as the copy 4, 12.50, and 25. Write C2 as net = 4 times 12.50 = 50.00. Write C3 as tax = 50.00 times 25 / 100 = 12.50. The long path is 50.00 times 25 = 1250.00, then 1250.00 / 100 = 12.50. Write C4 as gross = 50.00 + 12.50 = 62.50. A chain that writes 4 times 12.50 = 48.00 has failed at C2. Later lines that correctly take 25 percent of 48.00 stay rejected, because they inherited the bad product. A digit that C1 never copied stays out of C2, C3, and C4.
What each step is allowed to read
Decomposition means each step has a named input and a named output, and the step stays inside that handoff. Step 2 may read the numbered transcript and may write net and nQ. It picks no label. Step 3 may read the transcript, net, and nQ, and may write tax, gross, and nT. It leaves net unchanged. Step 4 may read the transcript and may read whether net is null. It writes only the label, and it runs no new multiplication. Step 5 may read nQ, nT, and the label, and it writes only the list lines. On paper, cover the transcript when you do step 5. If you still need a sentence you forgot to cite, step 4 or step 2 is incomplete, and you go back and widen that step.
When the source is a photo or a scan of a page, step 1 is a transcription. You write one numbered line for each visual line later steps may use, and you run steps 2 through 6 on that transcript alone. A stamp, a logo, or a handwritten total that never became a numbered line has no citation. It stays out of net, tax, gross, and lines. The object stores line numbers, not pixel positions. If you later notice a missed stamp, you add a line to the transcript and you recompute from step 2. You leave the old gross untouched until that new chain agrees with it.
Step 5 builds lines with one procedure. Start from an empty list. If nQ exists, append it. If nT exists and differs from nQ, append nT. If the label is REFUND, append every line number whose text contains the whole word refund or the whole word return. If the label is QUESTION, append 1. If the label is OTHER, append 1. Sort ascending and drop duplicates. Every note in this lesson has at least one source line, and the procedure then yields at least one integer. K4 says the object must use that list unchanged. An ORDER note cites the money lines only. A REFUND note cites the money lines and every line that held the winning word. At most one qty line and one tax fragment appear in these notes. If a later note has two, take the first of each and ignore the rest.
Four labels, first match wins
The taxonomy has four labels, and the tests run in one order. The first test that matches wins. Later tests are skipped, including tests that would also have matched. Lowercase the line before a word test. A whole word has a non-letter on each side, and the start or end of the line counts as a side. The word returned misses return, because a letter follows. The word prefund misses refund, because a letter precedes. Test 1, REFUND: some line contains the whole word refund or the whole word return. Test 2, ORDER: net is a number. Test 3, QUESTION: the last character of line 1 is ?. Test 4, OTHER: the three tests above missed. The order is part of the definition. Leave the order unstated and the same note can take two labels on two runs.
Five one-line notes fix the easy cases. Note R is Refund 40.00 for order 18. The whole word refund matches on line 1. The qty shape is absent, so net, tax, and gross are null. The label is REFUND and lines is [1]. Note O is Qty 2 at 10.00 each. net = 2 times 10.00 = 20.00. The tax fragment is absent, so tax and gross are null. The REFUND test misses. net is a number, so the label is ORDER and lines is [1]. Note Q is Where is order 18? The digits 18 are an order id. The qty shape is absent, so the three money fields are null. REFUND misses. ORDER misses. Line 1 ends with ?, so the label is QUESTION and lines is [1]. Note T is Thanks for the help. The three money fields are null, REFUND misses, ORDER misses, and line 1 ends with a period, so the label is OTHER and lines is [1]. Note M is Can I return this and get 40.00 back? The whole word return matches. The qty shape is absent, so the money fields are null. The label is REFUND and lines is [1]. The QUESTION test is skipped on note M, even though line 1 ends with ?.
Note W is two lines, so you can watch ORDER beat a question mark. Line 1 is Where is order 7? Line 2 is Qty 2 at 10.00 each. net = 2 times 10.00 = 20.00. The tax fragment is absent, so tax and gross are null. REFUND misses. ORDER matches because net is a number, and the QUESTION test is skipped. nQ is 2, so lines is [2]. Across R, O, Q, T, M, and W the labels are REFUND, ORDER, QUESTION, OTHER, REFUND, and ORDER. Two notes are REFUND. No note receives two labels. A question mark decides the label only when REFUND missed and net is null.
Schema rules, stop string, penalties
JSON Schema, for this lesson, is five pencil checks on one object. The keys are label, net, tax, gross, and lines. R1 fails when any of those five keys is absent. R2 fails unless label is exactly REFUND, ORDER, QUESTION, or OTHER. R3 fails unless net, tax, and gross are each a JSON number or JSON null. The string 20 fails R3. The string null fails R3. The numerals 20, 20.0, and 20.00 are the same JSON number, and each passes R3. R4 fails unless lines is an array of JSON integers with length at least 1. An empty array fails R4. The number 2.5 inside the array fails R4. R5 fails when any extra key is present. In schema language, additionalProperties set to false is R5, enum is the closed list in R2, and minItems set to 1 is the length demand in R4.
Check five objects against those rules. V is {"label":"ORDER","net":20,"tax":null,"gross":null,"lines":[1]}. V passes R1 through R5, and it passes K1 through K3. B is {"label":"ORDER","net":"20","tax":null,"gross":null,"lines":[1]}. B fails R3 because net is a string. X is {"label":"ORDER","net":20,"tax":null,"gross":null,"lines":[1],"note":"ok"}. X fails R5 because note is an extra key. G is {"label":"ORDER","net":null,"tax":null,"gross":20,"lines":[1]}. G passes R1 through R5 and fails K1, because gross is 20 while net is null. L is {"label":"OTHER","net":null,"tax":null,"gross":null,"lines":[]}. L fails R4. Pydantic is a Python library that checks typed fields. Aim it at the same five fields, reject extra keys, and reject a string where a number is required, and it rejects B and X for the same reasons R3 and R5 do. Libraries still disagree about whether the integer 20 may stand where a decimal type is declared. R3 accepts 20 and 20.0. When the library and R1 through R5 disagree, keep the pencil rules and write the disagreement next to the object.
A stop sequence is a string that ends generation when the sampler emits it. The stop string here is case-sensitive, and it is a line whose characters are exactly END. You write the six steps and the object with no run of the three capitals E, N, D, then you write that line. A stop on the string END cuts the reply at the first occurrence, so those capitals stay off the chain. The lowercase word end in ordinary prose does not trigger a case-sensitive stop. The stop string checks no types. You still run R1 through R5 on the finished text. Temperature, the frequency penalty F, and the presence penalty P change scores before the draw. They leave the schema untouched. The lesson formula is adjusted = score - F times count - P times appeared. count is how many times that candidate has already been emitted. appeared is 1 when count is at least 1, and 0 when count is 0. Temperature 0 keeps the winning adjusted score. If two adjusted scores tie, the candidate earlier in your list wins. A temperature above 0 would divide the adjusted scores by that temperature before the usual softmax. This lesson asks you to recompute the adjusted scores, not a full softmax.
Score the candidates in the order brace, quote, zebra, with raw scores 2, 2, and 1. The reply has already used brace once and quote four times, and has never used zebra, so the counts are 1, 4, and 0. At F=1 and P=0 the adjusted scores are 2 - 1 times 1 - 0 = 1, 2 - 1 times 4 - 0 = -2, and 1 - 0 - 0 = 1. Brace and zebra tie, and brace wins because it stands earlier. At F=0 and P=1 the adjusted scores are 2 - 0 - 1 = 1, 2 - 0 - 1 = 1, and 1 - 0 - 0 = 1. All three tie, and brace wins. At F=1 and P=1 the adjusted scores are 2 - 1 - 1 = 0, 2 - 4 - 1 = -3, and 1 - 0 - 0 = 1. Zebra wins. For a schema object the lesson sets temperature 0, F=0, and P=0, so the formula subtracts nothing. At those settings the adjusted scores stay 2, 2, and 1, brace and quote tie, and brace wins by the earlier place. The row F=1 and P=0 is the warning: a frequency penalty of 1 pulls the quote, which JSON still needs, down to -2.
The lamp note, end to end
The transcript has three lines. Line 1 is Please refund the lamp. Line 2 is Qty 3 at 15.00 each, tax 10 percent. Line 3 is Ship to the desk. Step 1 writes those three numbers and no others. Step 2 finds the qty shape on line 2, with Q = 3 and U = 15.00. C2 writes net = 3 times 15.00 = 45.00, and nQ = 2. Line 1 and line 3 have no qty shape. Step 3 finds tax 10 percent on line 2, so R = 10 and nT = 2. C3 writes tax = 45.00 times 10 / 100 = 4.50. C4 writes gross = 45.00 + 4.50 = 49.50. Because nT equals nQ, step 5 will append that line once.
Step 4 lowercases the transcript. Line 1 contains the whole word refund, bounded by spaces. REFUND matches at once. ORDER is skipped even though net is 45.00. QUESTION is skipped even though you might have expected a complaint. The matched test is the refund-word test, and the label is REFUND. Step 5 starts empty, appends nQ = 2, skips a second copy of nT because it equals nQ, and appends line 1 because the label is REFUND and line 1 holds refund. Line 3 holds neither the word nor the money. Sorted unique lines are [1, 2]. Step 6 writes {"label":"REFUND","net":45.00,"tax":4.50,"gross":49.50,"lines":[1,2]} and then a line whose characters are exactly END. R1 through R5 pass. K1 through K4 pass.
Two nearby failures show why the chain stays on the page. If C4 had written 45.00 + 4.50 = 49.05, you would reject at C4, and you would refuse a JSON gross of 49.05 or of 49.50 until C4 and the object show the same sum. 45.00 + 4.50 is 49.50, so 49.05 fails the addition. If the transcript drops line 1, the word refund is absent, REFUND misses, and ORDER matches on net 45.00. lines is then [2]. The object is {"label":"ORDER","net":45.00,"tax":4.50,"gross":49.50,"lines":[2]}. That object is the right reading of the shorter transcript and the wrong reading of the original three lines. The chain records which transcript you used. A stamp that never received a number cannot change this gross.
Run a new note on paper
Use the rules above on this transcript. Line 1 is Where is the lamp? Line 2 is Qty 5 at 8.00 each. Line 3 is No tax on this note. Write net, tax, gross, the label, which test was skipped, and lines. Then write the object and the stop line. Next, mark V, B, X, G, and L as schema pass or schema fail, and say whether G passes K1. Then, from the raw scores 2, 2, and 1 and the counts 1, 4, and 0, write the adjusted scores at F=1 and P=0, name the winner, and write the temperature, F, and P that the lesson sets for a schema object.
Line 2 matches Qty 5 at 8.00 each, so net = 5 times 8.00 = 40.00 and nQ = 2. The words No tax on this note lack the shape tax, then a whole number, then percent, so the tax fragment is absent. tax is null and gross is null. No line contains the whole word refund or return. net is a number, so the label is ORDER, and the QUESTION test is skipped even though line 1 ends with ?. lines is [2]. The object is {"label":"ORDER","net":40.00,"tax":null,"gross":null,"lines":[2]} followed by a line END. V passes the schema. B fails R3. X fails R5. G passes the schema and fails K1. L fails R4. The adjusted scores are 1, -2, and 1, and brace wins the tie. The lesson sets temperature 0, F=0, and P=0 for the schema object.
Check questions
On the line Qty 4 at 12.50 each, tax 25 percent, what are net, tax, and gross?
50.00, 12.50, and 62.50. The path is 4 times 12.50 = 50.00, then 50.00 times 25 / 100 = 12.50, then 50.00 + 12.50 = 62.50.
In the three-line lamp note, what are the label, the gross, and the lines array?
REFUND, 49.50, and [1, 2]. Line 1 is cited because it contains the whole word refund. Line 2 is the qty line, and the tax fragment sits on that same line.
Which of V and G pass rules R1 through R5, and which pipeline rule does G fail?
V and G both pass R1 through R5. G fails K1, because gross is 20 while net is null.
With raw scores 2, 2, and 1 for brace, quote, and zebra, and counts 1, 4, and 0, what are the adjusted scores at F=1 and P=0, which candidate wins, and what temperature, F, and P does the lesson set for a schema object?
The adjusted scores are 1, -2, and 1. Brace wins the tie because it stands earlier in the list. The lesson sets temperature 0, F=0, and P=0.
A usable extraction writes the six steps, applies one taxonomy in a fixed order, and keeps the object inside R1 through R5 and K1 through K4. Temperature 0 with F and P at 0 keeps the draw on the leading token, and the stop line END only ends the text. The arithmetic in the chain and the numbers in the object still have to match.
Final task
Submit an extraction script and schema validator that passes automated checks on 50 frozen fixture documents, matching the keyed classification labels and line citations on ≥45/50 items.
Terms in this course
This course has no terms of its own in the glossary yet. Terms from earlier courses are used here too.
The first lesson is open to read. The rest of the course is being written, and tests and diplomas come later. Join the waitlist