<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: anders lee</title>
    <description>The latest articles on DEV Community by anders lee (@anders_lee_28dbcc2fd8e034).</description>
    <link>https://dev.to/anders_lee_28dbcc2fd8e034</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4175492%2F7613d15a-11ea-4474-9276-ab13c5bc91fa.png</url>
      <title>DEV Community: anders lee</title>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anders_lee_28dbcc2fd8e034"/>
    <language>en</language>
    <item>
      <title>The car fee that changes every January 1: storing indexed fees with a year, not just a number</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Tue, 24 Nov 2026 04:05:01 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/the-car-fee-that-changes-every-january-1-storing-indexed-fees-with-a-year-not-just-a-number-5gfj</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/the-car-fee-that-changes-every-january-1-storing-indexed-fees-with-a-year-not-just-a-number-5gfj</guid>
      <description>&lt;p&gt;Most numbers in a car tax calculator change when a legislature votes. A few change on a schedule, every year, whether anyone votes or not. Pennsylvania has two of them on the same receipt, and they changed how I store fee data.&lt;/p&gt;

&lt;p&gt;This post covers those two fees, the four fields I ended up keeping for each, and the checks that go with them. Every number below comes from PennDOT or the state dealer board, and each one sits in the data file next to its source URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two fees on one Pennsylvania receipt
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The road user charge (RUC).&lt;/strong&gt; Under Acts 85 and 149 of 2024, battery-electric and plug-in hybrid vehicles in Pennsylvania pay an annual road user charge together with their registration. For 2026:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Vehicle&lt;/th&gt;
&lt;th&gt;1 year&lt;/th&gt;
&lt;th&gt;2 years&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Battery-electric (EV)&lt;/td&gt;
&lt;td&gt;$250&lt;/td&gt;
&lt;td&gt;$500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Plug-in hybrid (PHEV)&lt;/td&gt;
&lt;td&gt;$63&lt;/td&gt;
&lt;td&gt;$126&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;From 2027 PennDOT adjusts it every January 1 by the consumer price index. The PHEV amount is 25% of the EV amount, rounded up to the next dollar: 25% of $250 is $62.50, so $63.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The dealer documentary fee cap.&lt;/strong&gt; Pennsylvania limits what a dealer may charge for paperwork. The State Board of Vehicle Manufacturers, Dealers and Salespersons re-indexes the limit to CPI every year:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Year&lt;/th&gt;
&lt;th&gt;Electronic&lt;/th&gt;
&lt;th&gt;Non-electronic&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2025&lt;/td&gt;
&lt;td&gt;$477&lt;/td&gt;
&lt;td&gt;$398&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026&lt;/td&gt;
&lt;td&gt;$490&lt;/td&gt;
&lt;td&gt;$409&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two fees, two different agencies, the same pattern: the number is only true for one calendar year.&lt;/p&gt;

&lt;h2&gt;
  
  
  A number alone is a bug waiting for January
&lt;/h2&gt;

&lt;p&gt;The first version of my data had &lt;code&gt;"ev": 250&lt;/code&gt;. Nothing about that line tells you it stops being correct on January 1, 2027. The page would keep showing $250 with full confidence, and the date on the page (when I last checked the source) would make it look more trustworthy, not less.&lt;/p&gt;

&lt;p&gt;So each indexed fee now carries the year it applies to, separately from the date I checked it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"roadUserCharge"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"year"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"ev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"evTwoYear"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"phev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;63&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"phevTwoYear"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;126&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"asOf"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-05"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"sources"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://www.pa.gov/agencies/dmv/vehicle-services/road-user-charge"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"docFee"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"cap"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;490&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"capOther"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;409&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"capYear"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"asOf"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-05"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;asOf&lt;/code&gt; answers "when did someone last read the source?". &lt;code&gt;year&lt;/code&gt; answers "for which period is this number the law?". They drift apart: in December 2026 a check on the 5th is still fresh, but the year is about to end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't compute next year's number
&lt;/h2&gt;

&lt;p&gt;It's tempting to store a formula: last year's fee times CPI. I don't, for three reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The agency picks the CPI series, the period and the rounding. Pennsylvania's dealer board publishes the new cap in an annual notice; your CPI guess and their number can differ by a dollar, and a dollar off on a legal cap is wrong.&lt;/li&gt;
&lt;li&gt;The base can change. A legislature can reset an indexed fee at any time, and the formula would keep running on the old base.&lt;/li&gt;
&lt;li&gt;The published number is what a buyer sees at the counter. If my estimate disagrees with the receipt, the receipt wins.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So next year's value is data I copy from the published notice, not a value I derive. The code only knows how to show it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Derived values: store them, then test the rule
&lt;/h2&gt;

&lt;p&gt;The PHEV charge &lt;em&gt;is&lt;/em&gt; defined by a rule (25% of the EV charge, rounded up). I still store it as data, because the official page lists it, and I'd rather a data test catch a mismatch than have the code silently disagree with PennDOT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PA PHEV road user charge is 25% of EV, rounded up; two-year amounts are double"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roadUserCharge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;evTwoYear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phevTwoYear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phev&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the 2027 amounts arrive, typing them in is one edit, and this test tells me if I copied one of the four wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Put the year in the label
&lt;/h2&gt;

&lt;p&gt;The calculator line for an EV reads "Road user charge, electric vehicle (2026, 1 year)". Two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A buyer reading the estimate in January sees immediately which year it was built for.&lt;/li&gt;
&lt;li&gt;"1 year" matters because Pennsylvania also sells two-year registrations, and the estimate shows the one-year registration fee. Mixing a one-year registration with a two-year charge would be internally inconsistent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code is small:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roadUserCharge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"electric vehicle"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"plug-in hybrid"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plateTransfer&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="sb"&gt;`Road user charge, ${what} (none if the plate comes from another EV/PHEV)`&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`Road user charge, ${what} (${ruc.year}, 1 year)`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phev&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The plate-transfer branch is the other lesson from this fee. In Pennsylvania the plate belongs to the owner, not the car. If you move your plate from one EV or PHEV to another, the RUC balance moves with it for the same registration period. The calculator can't know what car your old plate was on, so it keeps the charge in the total and says in the label when it would be zero. An estimate that admits what it can't see beats one that guesses.&lt;/p&gt;

&lt;h2&gt;
  
  
  A cap, a threshold and "no cap" are three different fields
&lt;/h2&gt;

&lt;p&gt;Once doc fees had a &lt;code&gt;capYear&lt;/code&gt;, I looked at how the other states in the calculator handle them. "Max doc fee" turned out to mean different things:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Pennsylvania&lt;/td&gt;
&lt;td&gt;Hard cap, re-indexed yearly: $490 / $409 (2026)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;cap&lt;/code&gt;, &lt;code&gt;capOther&lt;/code&gt;, &lt;code&gt;capYear&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;New York&lt;/td&gt;
&lt;td&gt;Hard cap: $175 for handling registration and title paperwork&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cap&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Texas&lt;/td&gt;
&lt;td&gt;No flat cap. On financed sales a dealer may charge up to $225 without a filing; above that it must file a notice and cost analysis with the Office of Consumer Credit Commissioner first&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;cap: null&lt;/code&gt;, &lt;code&gt;threshold: 225&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Florida&lt;/td&gt;
&lt;td&gt;No dollar cap; the fee must carry a printed disclosure&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cap: null&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A Texas &lt;code&gt;cap: 225&lt;/code&gt; would be wrong: a dealer &lt;em&gt;can&lt;/em&gt; charge more after filing. A Florida &lt;code&gt;cap: 0&lt;/code&gt; would be wrong too. &lt;code&gt;null&lt;/code&gt; plus a separate &lt;code&gt;threshold&lt;/code&gt; keeps the meaning.&lt;/p&gt;

&lt;p&gt;Whether the fee is taxed is yet another field, and it doesn't follow from the cap. Pennsylvania's fee is not taxable. New York and Texas don't tax a separately stated fee. Florida treats discretionary dealer charges as part of the taxable price. So in the calculator, the doc fee is added to the taxable amount in Florida and stays outside it in the other three.&lt;/p&gt;

&lt;h2&gt;
  
  
  A reminder that runs itself
&lt;/h2&gt;

&lt;p&gt;The last piece is boring and the most useful: every field with a &lt;code&gt;year&lt;/code&gt; is a date on which the data expires. A build step can compare it with the current year and warn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;thisYear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getFullYear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registration&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roadUserCharge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ruc&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ruc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;thisYear&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`${st}: road user charge is for ${ruc.year}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;docFee&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capYear&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;docFee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capYear&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;thisYear&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`${st}: doc fee cap is for ${d.docFee.capYear}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It doesn't fix anything. It just makes sure the first build in January says out loud that two numbers are now from last year.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Store the period a fee applies to (&lt;code&gt;year&lt;/code&gt;) separately from when you checked it (&lt;code&gt;asOf&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Copy indexed values from the official notice; don't project them with your own CPI math.&lt;/li&gt;
&lt;li&gt;Keep derived values as data and test the rule that links them.&lt;/li&gt;
&lt;li&gt;Put the year in the user-facing label of any fee that changes on a schedule.&lt;/li&gt;
&lt;li&gt;Model "cap", "threshold" and "no cap" as different fields, and taxability as its own field.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Pennsylvania fees, with the road user charge and doc fee cap applied, are on the &lt;a href="https://carsalestax.com/pennsylvania/registration-fees/" rel="noopener noreferrer"&gt;Pennsylvania registration fees page&lt;/a&gt; of carsalestax.com.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>opendata</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>My music API refused 145 songs for naming an artist. A third of those artists were ordinary words</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Tue, 24 Nov 2026 01:05:01 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/my-music-api-refused-145-songs-for-naming-an-artist-a-third-of-those-artists-were-ordinary-words-3g8g</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/my-music-api-refused-145-songs-for-naming-an-artist-a-third-of-those-artists-were-ordinary-words-3g8g</guid>
      <description>&lt;p&gt;I run a small AI song generator. People describe a song, or paste their own lyrics plus a style line ("acoustic guitar, warm female vocals"), and a third-party music generation API turns that into a track. Most of my users don't write in English: the biggest groups are Ukrainian, Slovak, Polish and Czech, and the site runs in 24 languages.&lt;/p&gt;

&lt;p&gt;The API has one rule that shows up in my error logs again and again: no artist names. You can ask for "80s synth pop", not for a song "like" a named band. Fair enough. The trouble is what the filter thinks an artist name is.&lt;/p&gt;

&lt;p&gt;Last week a quality check flagged six paid generations in four days that had failed with an artist-name refusal. Five of them were the same word: &lt;strong&gt;rytmus&lt;/strong&gt;. In Slovak and Czech that just means &lt;em&gt;rhythm&lt;/em&gt;. One user wrote "acoustic guitar, rytmus", got refused three times in a row, then changed it to "rhythm" and got the song. Another wrote "a slower rhythm, strong bass, r&amp;amp;b" in Slovak, was refused twice, and never generated again.&lt;/p&gt;

&lt;p&gt;So I went back through every refusal in the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers
&lt;/h2&gt;

&lt;p&gt;From May 2024 to mid-November 2026 the site sent about 4,500 generations. 145 came back refused for an artist name.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Period&lt;/th&gt;
&lt;th&gt;Generations&lt;/th&gt;
&lt;th&gt;Refused for artist name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;May–Sep 2024&lt;/td&gt;
&lt;td&gt;3,644&lt;/td&gt;
&lt;td&gt;138 (3.8 %)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Oct 2024–Nov 2025&lt;/td&gt;
&lt;td&gt;259&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dec 2025–Nov 2026&lt;/td&gt;
&lt;td&gt;637&lt;/td&gt;
&lt;td&gt;7 (1.1 %)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The quiet middle period had little traffic, so I won't read much into the zero. The 2024 refusals were mostly on the free-text song description (112), the rest on the style tags (26). All 7 recent ones were on the style tags.&lt;/p&gt;

&lt;p&gt;Then I read the 145 names the filter reported, and sorted them by hand.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Real artists, asked for on purpose:&lt;/strong&gt; the majority. Pop stars, classic rock bands, rappers, a few metal bands. The filter was right, and the user needed a better hint (more on that below).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ordinary words: 49 of 145, by my reading.&lt;/strong&gt; About a third.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;First names of the person the song was for:&lt;/strong&gt; at least 4. On a site where people make birthday and anniversary songs, that one hurts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The ordinary words
&lt;/h2&gt;

&lt;p&gt;These are words that the filter reported as an artist name, with what they mean in the language the user wrote in:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Reported "artist"&lt;/th&gt;
&lt;th&gt;Language&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Times&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;melodico&lt;/td&gt;
&lt;td&gt;Italian&lt;/td&gt;
&lt;td&gt;melodic&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;latino&lt;/td&gt;
&lt;td&gt;Spanish&lt;/td&gt;
&lt;td&gt;Latin (as in Latin pop)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rytmus&lt;/td&gt;
&lt;td&gt;Slovak / Czech&lt;/td&gt;
&lt;td&gt;rhythm&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pesado&lt;/td&gt;
&lt;td&gt;Portuguese&lt;/td&gt;
&lt;td&gt;heavy&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wiatr&lt;/td&gt;
&lt;td&gt;Polish&lt;/td&gt;
&lt;td&gt;wind&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;epica&lt;/td&gt;
&lt;td&gt;Portuguese (épica)&lt;/td&gt;
&lt;td&gt;epic&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sin miedo&lt;/td&gt;
&lt;td&gt;Spanish&lt;/td&gt;
&lt;td&gt;without fear&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bebe&lt;/td&gt;
&lt;td&gt;Portuguese (bebê)&lt;/td&gt;
&lt;td&gt;baby&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;salvador&lt;/td&gt;
&lt;td&gt;Portuguese&lt;/td&gt;
&lt;td&gt;saviour (in a hymn)&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gato&lt;/td&gt;
&lt;td&gt;Portuguese&lt;/td&gt;
&lt;td&gt;cat&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;letto&lt;/td&gt;
&lt;td&gt;Italian&lt;/td&gt;
&lt;td&gt;bed&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;catedral&lt;/td&gt;
&lt;td&gt;Portuguese&lt;/td&gt;
&lt;td&gt;cathedral&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;havasi&lt;/td&gt;
&lt;td&gt;Turkish (havası)&lt;/td&gt;
&lt;td&gt;tune, air&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;devochka&lt;/td&gt;
&lt;td&gt;Russian (девочка)&lt;/td&gt;
&lt;td&gt;little girl&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;nhac thieu nhi&lt;/td&gt;
&lt;td&gt;Vietnamese (nhạc thiếu nhi)&lt;/td&gt;
&lt;td&gt;children's music&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tage&lt;/td&gt;
&lt;td&gt;German&lt;/td&gt;
&lt;td&gt;days&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;trunks&lt;/td&gt;
&lt;td&gt;English&lt;/td&gt;
&lt;td&gt;an elephant's trunks, in a zoo song&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Plus a handful of single cases like &lt;em&gt;roadtrip&lt;/em&gt;, &lt;em&gt;menor&lt;/em&gt; (minor), &lt;em&gt;rico&lt;/em&gt; (rich), &lt;em&gt;libera&lt;/em&gt; (sets free), &lt;em&gt;vedo&lt;/em&gt; (I see), &lt;em&gt;levante&lt;/em&gt; (rise).&lt;/p&gt;

&lt;p&gt;Most of these are genre and mood words, which is exactly what the API asks you to write instead of artist names. "Pop rock melodico italiano" is a textbook style description. It was refused nine times.&lt;/p&gt;

&lt;h2&gt;
  
  
  The filter folds the text before it matches
&lt;/h2&gt;

&lt;p&gt;Look at the right-hand spelling in the table. The user wrote &lt;em&gt;épica&lt;/em&gt;, &lt;em&gt;bebê&lt;/em&gt;, &lt;em&gt;Havası&lt;/em&gt;, &lt;em&gt;девочка&lt;/em&gt;, &lt;em&gt;Nhạc thiếu nhi&lt;/em&gt;. The refusal reported &lt;em&gt;epica&lt;/em&gt;, &lt;em&gt;bebe&lt;/em&gt;, &lt;em&gt;havasi&lt;/em&gt;, &lt;em&gt;devochka&lt;/em&gt;, &lt;em&gt;nhac thieu nhi&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So the filter strips accents (Vietnamese tone marks included) and transliterates Cyrillic into plain Latin letters, and only then compares against its list of names. That is why it catches so much outside English: once every word is folded to ASCII, a lot of common words in other languages land on a band or a stage name somewhere. A Russian rap prompt that mentioned a well-known American rapper in the genitive case came back as &lt;em&gt;rossa&lt;/em&gt;: folded and transliterated, inflection included.&lt;/p&gt;

&lt;p&gt;Two practical consequences for anyone sending user text to a filtered API:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you keep a list of known false positives, match it on folded text too, or you'll miss the accented forms users really type.&lt;/li&gt;
&lt;li&gt;Your users can't predict this. "Write genres and moods, not artist names" is good advice that still fails for an Italian writing &lt;em&gt;melodico&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What I changed
&lt;/h2&gt;

&lt;p&gt;Three small things, in order of how often they help.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Replace known false positives before submitting.&lt;/strong&gt; A short, explicit list, only words that have actually been refused. It is applied to the style line in the generate route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;FALSE_ARTIST_WORDS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;rytmus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"rhythm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// sk/cs; 5 refusals in Nov 2026&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;escapeRe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*+?^&lt;/span&gt;&lt;span class="nv"&gt;${}&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;\&lt;span class="p"&gt;]&lt;/span&gt;\\&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\\$&amp;amp;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// \b doesn't know č or š, so use Unicode letter boundaries&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;wordRe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`(?&amp;lt;![\\p{L}\\p{N}])${escapeRe(word)}(?![\\p{L}\\p{N}])`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"giu"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;replaceFalseArtistWords&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;repl&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;FALSE_ARTIST_WORDS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;wordRe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;repl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;\b&lt;/code&gt; detail matters. In JavaScript, &lt;code&gt;\b&lt;/code&gt; only understands ASCII word characters, so it sees a word boundary between &lt;code&gt;č&lt;/code&gt; and the next letter, and &lt;code&gt;\brytmus\b&lt;/code&gt; can match the tail of a longer word. The lookarounds with &lt;code&gt;\p{L}&lt;/code&gt; (any letter, needs the &lt;code&gt;u&lt;/code&gt; flag) fix that.&lt;/p&gt;

&lt;p&gt;The list starts with one word on purpose. Replacing a word changes what the user asked for, so each entry has to be a confirmed refusal with an obvious English equivalent. &lt;em&gt;melodico → melodic&lt;/em&gt; and &lt;em&gt;pesado → heavy&lt;/em&gt; are the next candidates from the table above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Retry once without the name.&lt;/strong&gt; The refusal arrives in the provider's async callback. The generate route now keeps the submitted parameters on the job row, so the callback can strip the reported word from the style line and resubmit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;artist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;artistFromRefusal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refusal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// "rytmus", "bon jovi", ...&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;style&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;stripArtist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;artist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// also tidies leftover commas and slashes&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;style&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fall back to fail + refund&lt;/span&gt;
&lt;span class="c1"&gt;// resubmit with the same params and the new style; the song stays "processing"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rules I set: only for the style field in custom mode, at most two retries per song (one prompt named two bands), never when stripping leaves the style empty, and the user is not charged again. If the retry can't run, the old path applies: the song fails and the credit goes back.&lt;/p&gt;

&lt;p&gt;I deliberately do &lt;strong&gt;not&lt;/strong&gt; retry refusals on the free-text description. Removing a word from "a song for my wife ___ for our anniversary" can remove the one word the song is about.&lt;/p&gt;

&lt;p&gt;An honest gap: going through the old rows for this post showed the provider has changed the wording of this error at least twice. In 2024 it was &lt;code&gt;moderation_failure; Tags contained artist name: X&lt;/code&gt; or &lt;code&gt;Song Description contained artist name: X&lt;/code&gt;. Since December 2025 it is &lt;code&gt;Your tags contain artist name X - we don't reference specific artists&lt;/code&gt;. My parser only knows the current one. If the wording changes again, the retry silently stops working and everything falls back to fail-and-refund. Accepting all three formats, plus an alert when a refusal arrives that the parser can't read, is the next change. The retry has also not fired in production yet, so I can't report its success rate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Tell the user what happened, in their language.&lt;/strong&gt; Before, a refused song just showed up as failed, at best with the provider's raw English message. Now the owner sees, on the song page and in their song list, a sentence like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The music AI doesn't accept artist names in "Music Style Tag" ("rytmus"). Describe the sound with genres and moods instead, e.g. "pop rock, warm male vocals".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It uses the same label as the form field in each of the 24 languages, so people know which box to edit. For the real artist requests, which are the majority, this is the fix that matters. Most of those users weren't trying to copy anyone; they used a famous name as shorthand for a sound, because that's how people describe music to each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell someone building on a filtered generation API
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Log the refusal reason with the request.&lt;/strong&gt; I could only do this analysis because the full response was stored next to each job. Without it, 145 refusals would have been 145 anonymous failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Group refusals by language.&lt;/strong&gt; Moderation tuned on English text behaves differently once it folds everything to ASCII. Look at the words, not just the count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parse provider error messages defensively.&lt;/strong&gt; Match more than one wording, and alert when nothing matches, because a silent parser failure looks exactly like "no refusals".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat the error text as product copy.&lt;/strong&gt; The person who hit the filter needs to know which field to change and what to write instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The site, if you want to see the form these style lines go into: &lt;a href="https://aisong.fun/?utm_source=devto&amp;amp;utm_medium=article" rel="noopener noreferrer"&gt;AISong.Fun&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>i18n</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Diffing US tariff schedule revisions in Python: the rate column stayed the same, the dates moved</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Sat, 21 Nov 2026 01:05:01 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/diffing-us-tariff-schedule-revisions-in-python-the-rate-column-stayed-the-same-the-dates-moved-3icg</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/diffing-us-tariff-schedule-revisions-in-python-the-rate-column-stayed-the-same-the-dates-moved-3icg</guid>
      <description>&lt;p&gt;The US Harmonized Tariff Schedule is not a yearly file. USITC published the 2026 Basic edition on December 31, 2025, and Revision 23 took effect on November 10, 2026. That is 24 editions in a bit over ten months, one every 13.7 days on average (the closest two were 3 days apart, the longest gap was 42 days).&lt;/p&gt;

&lt;p&gt;If you keep a copy of the schedule in a database, the obvious plan is to reload it when a new revision appears and compare the duty rates. I did that for three revisions in a row. Between Revision 21 and Revision 23, not a single code was added or removed, and not a single rate in chapters 1 to 97 changed. Eight rows did change. Each change was a few words of text, and two of them moved the start date of a 100% duty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the data
&lt;/h2&gt;

&lt;p&gt;USITC has a small REST API. &lt;code&gt;releaseList&lt;/code&gt; tells you which revision is current, and &lt;code&gt;exportList&lt;/code&gt; gives you the whole schedule as JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gzip&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;

&lt;span class="no"&gt;BASE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://hts.usitc.gov/reststop"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{BASE}/{path}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;180&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;get_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"releaseList"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"current"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"exportList?from=0101&amp;amp;to=9999&amp;amp;format=JSON&amp;amp;styles=false"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;gzip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{current['name']}.json.gz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"wt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save every revision you download. The export gives you the current revision only, so if you want to compare against last month you have to have kept last month yourself. Gzipped, one revision is about 640 KB.&lt;/p&gt;

&lt;p&gt;Some things to know about the rows before you compare them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Revision 23 has 35,805 rows, but only 29,861 have a code in &lt;code&gt;htsno&lt;/code&gt;. The other 5,944 are text rows such as &lt;code&gt;Horses:&lt;/code&gt; or &lt;code&gt;Cattle:&lt;/code&gt; that group the lines under them. Key by code and they disappear, so check them separately.&lt;/li&gt;
&lt;li&gt;Every row has a field called &lt;code&gt;addiitionalDuties&lt;/code&gt; (with the typo). It is &lt;code&gt;null&lt;/code&gt; in all 35,805 rows. The real field is &lt;code&gt;additionalDuties&lt;/code&gt;, set on 810 rows. If you pick fields by hand, pick the right one.&lt;/li&gt;
&lt;li&gt;The order of rows was identical in all three revisions, but I would not rely on that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A diff that does not assume which field matters
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;by_code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"htsno"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"htsno"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;by_code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;by_code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;changed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;changed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"added"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"removed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"changed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;changed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;text_rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"indent"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"htsno"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It compares every field, not only &lt;code&gt;general&lt;/code&gt; (the Column 1 General rate), and it reports which fields changed. &lt;code&gt;text_rows&lt;/code&gt; turns the rows without a code into a multiset, so an edited or missing heading text shows up even though it has no key.&lt;/p&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Comparison&lt;/th&gt;
&lt;th&gt;Added&lt;/th&gt;
&lt;th&gt;Removed&lt;/th&gt;
&lt;th&gt;Changed&lt;/th&gt;
&lt;th&gt;Text rows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Revision 21 → 22&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;identical&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Revision 22 → 23&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;identical&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All 8 changes are in chapter 99, and in all 8 the only changed field is &lt;code&gt;description&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed: a few words each
&lt;/h2&gt;

&lt;p&gt;A plain string comparison tells you that a 600-character description changed, which does not help much. A word-level diff shows what changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;difflib&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;word_changes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;old_text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ops&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;difflib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SequenceMatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_opcodes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;i2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;j2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;op&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j2&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ops&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;op&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"equal"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Revision 21 → 22 changed one heading, &lt;code&gt;9903.01.63&lt;/code&gt; (an additional duty on products of China). The text lost one sentence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("[Compiler's note: provision suspended. See 90 Fed. Reg. 50729.]", "")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its &lt;code&gt;general&lt;/code&gt; column read &lt;code&gt;The duty provided in the applicable subheading + 34%&lt;/code&gt; before and after. What that means legally is a question for the Federal Register notices, not for this post. But for a program the result is clear: the only sign of the change was a bracketed note that disappeared from a description.&lt;/p&gt;

&lt;p&gt;Revision 22 → 23 changed seven headings, and the word diff is the same for every one of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"November"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"January"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"2026,"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2027,"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;9903.88.69&lt;/code&gt; and &lt;code&gt;9903.88.70&lt;/code&gt; cover products of China with an exclusion granted by the U.S. Trade Representative. Their end date moved from November 9, 2026 to January 9, 2027.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;9903.91.12&lt;/code&gt; to &lt;code&gt;9903.91.16&lt;/code&gt; cover intermodal chassis and their parts, and ship-to-shore gantry cranes. Two of them carry &lt;code&gt;+ 100%&lt;/code&gt;. The text used to say "on or after November 10, 2026" and now says "on or after January 10, 2027".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;November 10, 2026 is also the date Revision 23 took effect. So a 100% duty that was due to start that day was moved two months later in the same revision, and in the data all of that is two words in a description field.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pull the dates out
&lt;/h2&gt;

&lt;p&gt;Once you know the dates are in the text, you can extract them. None of the 26,246 coded rows in chapters 1 to 97 has a date in its description. 168 rows do, and 166 of them are in chapter 99, which has 3,112 coded rows in total.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;

&lt;span class="no"&gt;MONTHS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"January|February|March|April|May|June|July|August|September|October|November|December"&lt;/span&gt;
&lt;span class="no"&gt;DATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\b("&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;MONTHS&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;") (\d\d?), (\d{4})"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dates_after&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finditer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;when&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strptime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%B %d %Y"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;when&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"htsno"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"general"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run on Revision 23 with &lt;code&gt;today = date(2026, 11, 14)&lt;/code&gt;, it finds this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Date&lt;/th&gt;
&lt;th&gt;Heading&lt;/th&gt;
&lt;th&gt;What the text says&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-11-30&lt;/td&gt;
&lt;td&gt;9903.54.02&lt;/td&gt;
&lt;td&gt;end of a window for 300,000 t of extra lean beef trimmings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-12-31&lt;/td&gt;
&lt;td&gt;9903.54.01&lt;/td&gt;
&lt;td&gt;end of a window for 80,000 t of extra lean beef trimmings from Argentina&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-12-31&lt;/td&gt;
&lt;td&gt;9819.11.12&lt;/td&gt;
&lt;td&gt;apparel from certain lesser developed countries, fabric of any origin, "through December 31, 2026, inclusive"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2027-01-09&lt;/td&gt;
&lt;td&gt;9903.88.69, 9903.88.70&lt;/td&gt;
&lt;td&gt;USTR exclusions for products of China end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2027-01-10&lt;/td&gt;
&lt;td&gt;9903.91.12 to 9903.91.16&lt;/td&gt;
&lt;td&gt;chassis and crane provisions start (100% on two of them)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2027-04-18&lt;/td&gt;
&lt;td&gt;9903.91.16&lt;/td&gt;
&lt;td&gt;entry deadline for cranes bought under contracts dated before April 17, 2025&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That is a small list, short enough to put in a calendar. It is also a list that the rate column never shows.&lt;/p&gt;

&lt;p&gt;The regex only catches dates written as "Month D, YYYY". &lt;code&gt;9819.11.12&lt;/code&gt; starts "on the date announced in a Federal Register notice", and no pattern finds that. Use the list as a reminder to read the text, not as the legal answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would do differently from the start
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Diff every field, then filter.&lt;/strong&gt; I started by comparing &lt;code&gt;general&lt;/code&gt; only. It reported "no changes" for both revisions, which was technically true and not useful.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep chapter 99 in scope.&lt;/strong&gt; It is easy to drop as "not real products", but it is where the time-limited duties live. A &lt;code&gt;Free&lt;/code&gt; rate in chapter 85 does not tell you what chapter 99 adds for a given country of origin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Archive everything.&lt;/strong&gt; Two revisions are enough to compare, but only if you kept the older one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expect quiet revisions.&lt;/strong&gt; Revisions 22 and 23 changed 1 and 7 rows out of 35,805. Most of the work is noticing that something changed at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I run this diff for every new revision and publish the result at &lt;a href="https://hs-code.net/changes/" rel="noopener noreferrer"&gt;hs-code.net/changes&lt;/a&gt;, next to the classification guides. If you build your own, start with the word diff. It took the 7 changed descriptions in Revision 23 from 7 long paragraphs down to one line each.&lt;/p&gt;

</description>
      <category>python</category>
      <category>data</category>
      <category>opendata</category>
      <category>automation</category>
    </item>
    <item>
      <title>"The square tube weight formula everyone writes first is up to 18% heavy: checking 207 sizes against EN 10219 corners"</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Thu, 19 Nov 2026 01:05:02 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/the-square-tube-weight-formula-everyone-writes-first-is-up-to-18-heavy-checking-207-sizes-161p</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/the-square-tube-weight-formula-everyone-writes-first-is-up-to-18-heavy-checking-207-sizes-161p</guid>
      <description>&lt;p&gt;If you need the weight of a square steel tube, the formula that comes to mind is the outer square minus the inner&lt;br&gt;
square:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;² − &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; − &lt;span class="mi"&gt;2t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;²          &lt;span class="c1"&gt;# a = side, t = wall, both in mm&lt;/span&gt;
&lt;span class="n"&gt;kg&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt; × &lt;span class="mf"&gt;0.00785&lt;/span&gt;          &lt;span class="c1"&gt;# 7850 kg/m³ × 1e-6 (mm² → m²)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is short, it is easy to test, and I had it in my own weight calculator for months. It is also wrong for real&lt;br&gt;
tube, because it assumes the corners are sharp. Cold-formed square and rectangular hollow sections (SHS / RHS)&lt;br&gt;
are bent from strip, and a bent corner has a radius. The steel that a sharp corner would have is simply not there.&lt;/p&gt;

&lt;p&gt;I found the problem when the generic calculator on my site and the SHS table on the same site disagreed by 4 % for&lt;br&gt;
50 × 50 × 3. The table used rounded corners; the calculator did not. So I measured how big the gap is over every&lt;br&gt;
size in that table.&lt;/p&gt;
&lt;h2&gt;
  
  
  The rounded-corner area
&lt;/h2&gt;

&lt;p&gt;EN 10219-2 (cold-formed structural hollow sections) gives the corner radii to use when calculating section&lt;br&gt;
properties. The outside radius depends only on the wall thickness:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;wall t&lt;/th&gt;
&lt;th&gt;outside radius ro&lt;/th&gt;
&lt;th&gt;inside radius ri&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;t ≤ 6 mm&lt;/td&gt;
&lt;td&gt;2t&lt;/td&gt;
&lt;td&gt;ro − t&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6 &amp;lt; t ≤ 10 mm&lt;/td&gt;
&lt;td&gt;2.5t&lt;/td&gt;
&lt;td&gt;ro − t&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t &amp;gt; 10 mm&lt;/td&gt;
&lt;td&gt;3t&lt;/td&gt;
&lt;td&gt;ro − t&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each of the four corners is a quarter ring instead of a square block. Compared with the sharp corner, one corner&lt;br&gt;
loses (1 − π/4)(ro² − ri²) of area, so all four lose (4 − π)(ro² − ri²):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;radii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mf"&gt;2.5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ro&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ro&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sharp_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;H&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;H&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;H&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rounded_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;H&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ro&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;radii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;H&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ro&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ro&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;ri&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kg_per_m&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;area_mm2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;area_mm2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.00785&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;2t(B + H − 2t)&lt;/code&gt; is the sharp-corner area written differently (expand &lt;code&gt;H·B − (H − 2t)(B − 2t)&lt;/code&gt; and you get the same&lt;br&gt;
thing), so the only difference between the two functions is the corner term.&lt;/p&gt;

&lt;p&gt;Two checks against published EN 10219 masses: 100 × 100 × 5 gives 14.410 kg/m (tables: 14.4), 40 × 40 × 2 gives&lt;br&gt;
2.306 (tables: 2.31). The sharp-corner formula gives 14.915 and 2.386, both 3.5 % high.&lt;/p&gt;
&lt;h2&gt;
  
  
  The gap over 207 sizes
&lt;/h2&gt;

&lt;p&gt;My SHS / RHS table lists 18 square sizes from 20 to 300 mm and 14 rectangular ones from 40 × 20 to 300 × 200, with&lt;br&gt;
walls from 1.6 to 12.5 mm, keeping only the walls between about 1/50 and 1/8 of the smaller side. That is 207 rows.&lt;br&gt;
For each one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sharp_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;H&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;rounded_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;H&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result, sharp-corner weight above the rounded-corner weight:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;gap&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0–1 %&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1–2 %&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2–4 %&lt;/td&gt;
&lt;td&gt;71&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4–6 %&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6–8 %&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;over 8 %&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The median is 3.5 %. The smallest gap is 0.9 % (200 × 100 × 2, a thin wall on a big section). The largest is&lt;br&gt;
&lt;strong&gt;18.1 %, for 100 × 100 × 12.5&lt;/strong&gt;. 62 of the 207 rows are more than 5 % off.&lt;/p&gt;

&lt;p&gt;Some rows people actually order:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;tube (mm)&lt;/th&gt;
&lt;th&gt;sharp corners kg/m&lt;/th&gt;
&lt;th&gt;EN 10219 corners kg/m&lt;/th&gt;
&lt;th&gt;gap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;20 × 20 × 1.6&lt;/td&gt;
&lt;td&gt;0.924&lt;/td&gt;
&lt;td&gt;0.873&lt;/td&gt;
&lt;td&gt;+5.9 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;50 × 50 × 3&lt;/td&gt;
&lt;td&gt;4.427&lt;/td&gt;
&lt;td&gt;4.245&lt;/td&gt;
&lt;td&gt;+4.3 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;80 × 40 × 4&lt;/td&gt;
&lt;td&gt;7.034&lt;/td&gt;
&lt;td&gt;6.710&lt;/td&gt;
&lt;td&gt;+4.8 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100 × 100 × 5&lt;/td&gt;
&lt;td&gt;14.915&lt;/td&gt;
&lt;td&gt;14.410&lt;/td&gt;
&lt;td&gt;+3.5 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100 × 100 × 8&lt;/td&gt;
&lt;td&gt;23.110&lt;/td&gt;
&lt;td&gt;21.385&lt;/td&gt;
&lt;td&gt;+8.1 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100 × 100 × 10&lt;/td&gt;
&lt;td&gt;28.260&lt;/td&gt;
&lt;td&gt;25.565&lt;/td&gt;
&lt;td&gt;+10.5 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;200 × 100 × 12.5&lt;/td&gt;
&lt;td&gt;53.969&lt;/td&gt;
&lt;td&gt;48.704&lt;/td&gt;
&lt;td&gt;+10.8 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;300 × 300 × 6&lt;/td&gt;
&lt;td&gt;55.390&lt;/td&gt;
&lt;td&gt;54.662&lt;/td&gt;
&lt;td&gt;+1.3 %&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A 6 m length of 100 × 100 × 10 is 153.4 kg, not 169.6 kg. On a 2-tonne order (by sharp-corner weight) of that size,&lt;br&gt;
about 191 kg of the steel you paid for on paper does not exist.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why thick walls are worse
&lt;/h2&gt;

&lt;p&gt;The lost area is proportional to t², because both radii scale with t:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ro² − ri² = t(2ro − t) = (2k − 1)·t²        with ro = k·t
loss      = (4 − π)(2k − 1)·t²  =  2.58 t² (k = 2),  3.43 t² (k = 2.5),  4.29 t² (k = 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sharp-corner area of a square tube is 4t(a − t), which grows with t only linearly. Dividing one by the other&lt;br&gt;
gives the gap exactly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gap = c·t / (4(a − t) − c·t)        c = 2.58, 3.43 or 4.29
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For 100 × 100 × 10: 3.43 × 10 / (360 − 34.3) = 10.5 %. So the error is roughly proportional to t/a, and it gets a&lt;br&gt;
step up each time the radius rule switches from 2t to 2.5t to 3t. That is why the worst row is the thickest wall on&lt;br&gt;
a mid-size tube, and why 300 × 300 × 6 is barely affected.&lt;/p&gt;
&lt;h2&gt;
  
  
  A property test that fails: thicker wall, lighter tube
&lt;/h2&gt;

&lt;p&gt;The obvious property test for a weight function is "more wall, more weight". With the EN 10219 radii it fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.0001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;10.0001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;kg_per_m&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;rounded_area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# 5.9      16.729&lt;/span&gt;
&lt;span class="c1"&gt;# 6.0      16.982&lt;/span&gt;
&lt;span class="c1"&gt;# 6.0001   16.74     &amp;lt;- thicker wall, 0.24 kg/m lighter&lt;/span&gt;
&lt;span class="c1"&gt;# 10.0     25.565&lt;/span&gt;
&lt;span class="c1"&gt;# 10.0001  24.891    &amp;lt;- 0.67 kg/m lighter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At t = 6 the outside radius jumps from 12 mm to 15 mm, and the extra corner loss is bigger than the extra wall.&lt;br&gt;
That is not a bug in the code; it is what a stepped rule does. It does not matter in practice, because tube is&lt;br&gt;
rolled in standard walls (6, then 6.3 or 8; 10, then 12.5), and over the standard walls the weight does rise. So the&lt;br&gt;
test I kept runs over the list of standard walls instead of arbitrary floats.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the rounded formula breaks
&lt;/h2&gt;

&lt;p&gt;The corner formula only makes sense while two outside radii fit across the side. For a 10 × 10 tube with a 3 mm&lt;br&gt;
wall, ro = 6 mm, so 2ro = 12 mm is wider than the tube. The formula still returns a number (60.8 mm² against a&lt;br&gt;
sharp-corner 84 mm²), but it describes no real shape. In the calculator I fall back to the sharp-corner area when&lt;br&gt;
&lt;code&gt;2 * ro &amp;gt; min(H, B)&lt;/code&gt;. Sizes like that are not in any table; they show up only when someone types them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a² − (a − 2t)²&lt;/code&gt; is a solid bar minus a solid bar. Real cold-formed tube is lighter by 0.9–18 % over common sizes,
median 3.5 %.&lt;/li&gt;
&lt;li&gt;Subtract (4 − π)(ro² − ri²) with the EN 10219-2 radii (2t / 2.5t / 3t) and you match published masses.&lt;/li&gt;
&lt;li&gt;The error grows with t/a; thick walls on small tubes are the worst case.&lt;/li&gt;
&lt;li&gt;Stepped rules in standards break "monotonic" property tests; test over the values the standard actually uses.&lt;/li&gt;
&lt;li&gt;Guard the formula for impossible sizes instead of returning a confident wrong number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The table I checked, with all 207 sizes, is the metric&lt;br&gt;
&lt;a href="https://steelweight-calculator.com/charts/ms-square-pipe-weight-chart/" rel="noopener noreferrer"&gt;square and rectangular tube weight chart&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>engineering</category>
      <category>testing</category>
      <category>math</category>
    </item>
    <item>
      <title>A second hidden modal ate my Buy button, and my checks looked in the wrong places for four days</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Tue, 17 Nov 2026 01:05:01 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/a-second-hidden-modal-ate-my-buy-button-and-my-checks-looked-in-the-wrong-places-for-four-days-53p4</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/a-second-hidden-modal-ate-my-buy-button-and-my-checks-looked-in-the-wrong-places-for-four-days-53p4</guid>
      <description>&lt;p&gt;I run a small AI song generator on Next.js 14. Free users get one song. After that, a pay modal offers song packs, and each "Buy" button is a plain link to a Stripe payment page.&lt;/p&gt;

&lt;p&gt;The site is small, so &lt;code&gt;begin_checkout&lt;/code&gt; in Google Analytics was never a big number, just a steady trickle. From November 6 it was zero, and stayed there. People still reached the paywall. Nobody clicked Buy, or so the numbers said.&lt;/p&gt;

&lt;p&gt;It took four days to find the cause. The cause was small. The useful lesson was why my checks kept missing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I checked first, and why it all came back fine
&lt;/h2&gt;

&lt;p&gt;A sudden drop to zero looks like something broke on the payment side, so that is where I looked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stripe account and charges: fine, no failed payments.&lt;/li&gt;
&lt;li&gt;The four payment links: active, all returned 200.&lt;/li&gt;
&lt;li&gt;The webhook endpoint: enabled, and fulfilment had worked for the last real purchases.&lt;/li&gt;
&lt;li&gt;Recent deploys: nothing that touched the pay modal or the buy links.&lt;/li&gt;
&lt;li&gt;Traffic: sign-ups and paywall views per day looked normal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every check passed because every check started at Stripe. Nothing reached Stripe at all. The break was one step earlier, in the browser, between the modal opening and the link being followed.&lt;/p&gt;

&lt;p&gt;What found it was the dull test: open the site on a phone-sized browser as a logged-in user with zero credits, let the pay modal open, tap "Buy 10 songs". The modal closed. No Stripe page. No error in the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Several modals, one boolean
&lt;/h2&gt;

&lt;p&gt;The pay modal is a component called &lt;code&gt;PayModal&lt;/code&gt;, built on Headless UI's &lt;code&gt;Dialog&lt;/code&gt; (v1.7). Its open state lives in shared React context, so any part of the page can open it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;showPayModal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setShowPayModal&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCommonContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Transition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Root&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="n"&gt;showPayModal&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dialog&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"relative z-30"&lt;/span&gt; &lt;span class="n"&gt;onClose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;setShowPayModal&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* packs, Buy links to Stripe */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Dialog&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Transition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Root&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over a few months, four components started rendering their own &lt;code&gt;&amp;lt;PayModal /&amp;gt;&lt;/code&gt;: the song generator form, the gift song form, the audio player and the "My songs" page. Each needed "open the pay modal" and the easy way was to drop the component in. They are never all on one page, but the main pages have at least two: the home page has the generator form plus the player of an example song, a song page has two players, and the birthday song page has a player per example song plus the gift form.&lt;/p&gt;

&lt;p&gt;They all read the same &lt;code&gt;showPayModal&lt;/code&gt;. So when one part of the page set it to true, every mounted copy opened at once: two or more identical dialogs stacked on top of each other. You only ever see the top one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a tap on Buy closed the modal
&lt;/h2&gt;

&lt;p&gt;Headless UI dialogs close on outside clicks. Each &lt;code&gt;Dialog&lt;/code&gt; registers document-level capture listeners. On desktop that is a &lt;code&gt;click&lt;/code&gt; listener; on mobile it handles &lt;code&gt;touchend&lt;/code&gt; instead. When a pointer event ends on an element that is not inside &lt;em&gt;this&lt;/em&gt; dialog's panel, the dialog calls &lt;code&gt;onClose&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sibling dialogs are not a stack. They were not nested in each other's React tree, so none of them knew the others were dialogs. For dialog #2 (and #3 and up, on pages with more), the panel of dialog #1 is simply "outside".&lt;/p&gt;

&lt;p&gt;So a tap on Buy in the top dialog went like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The top dialog sees a tap inside its own panel and does nothing.&lt;/li&gt;
&lt;li&gt;The other copies see a tap outside their panels and call &lt;code&gt;onClose&lt;/code&gt;, which is &lt;code&gt;setShowPayModal(false)&lt;/code&gt;. Headless UI's outside-click handler can also call &lt;code&gt;preventDefault()&lt;/code&gt; on that event, which cancels the link's default action.&lt;/li&gt;
&lt;li&gt;That is the same shared boolean, so the top dialog closes too.&lt;/li&gt;
&lt;li&gt;With the event cancelled and the modal gone, the browser never navigates to Stripe, and the &lt;code&gt;onClick&lt;/code&gt; that sends &lt;code&gt;begin_checkout&lt;/code&gt; never runs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No exception, no failed request, no log line. From the outside it looks exactly like a user who opened the modal, thought about it and left.&lt;/p&gt;

&lt;p&gt;I still can't say which change turned this from a latent bug into a total one. Several instances had existed for weeks while purchases came in. It doesn't matter much: once two dialogs can be open together, whether a click lands depends on timing and event order, and that can change with any re-render.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: only the first mounted instance renders
&lt;/h2&gt;

&lt;p&gt;I didn't want to change the four call sites and the shared context in a hurry while the site wasn't taking money. The smallest safe fix was to let every &lt;code&gt;PayModal&lt;/code&gt; mount, but render only one of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;owners&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;subscribers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;notify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useIsPrimaryModal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;owners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;owners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;owners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useSyncExternalStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;owners&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// server render: nothing&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;PayModal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;primary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useIsPrimaryModal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;useId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;primary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Transition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Root&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="n"&gt;showPayModal&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Transition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Root&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useId&lt;/code&gt; gives each instance a stable id. A module-level list records mount order, and &lt;code&gt;useSyncExternalStore&lt;/code&gt; re-renders the instances when the first one unmounts, so the next one takes over. On the server every instance renders nothing, which is fine for a modal that only opens after a user action.&lt;/p&gt;

&lt;p&gt;The cleaner long-term design is one &lt;code&gt;PayModal&lt;/code&gt; at the layout level and a &lt;code&gt;openPayModal(reason)&lt;/code&gt; function in context. The hook was a 20-line change I could verify the same morning, and it stops the next copy that someone drops into a component from breaking checkout again.&lt;/p&gt;

&lt;p&gt;Right after the deploy, the same phone-sized test opened the Stripe page on the first tap, and real purchases came back over the following days.&lt;/p&gt;

&lt;h2&gt;
  
  
  The analytics bug hiding behind it
&lt;/h2&gt;

&lt;p&gt;The modal also sends a &lt;code&gt;paywall_view&lt;/code&gt; event when it opens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;showPayModal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;trackEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'paywall_view'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;showPayModal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every mounted instance sent it, so each view was counted once per instance: twice on the home page, more on pages with several players. My funnel showed far more people reaching the paywall than really did, so the paywall-to-checkout rate looked far worse than it was. That made "people just don't want to buy" a believable story, which is exactly the wrong story when the button is broken.&lt;/p&gt;

&lt;p&gt;Now only the primary instance sends the event. Paywall numbers from before the fix can't be compared with numbers after it, and I wrote that down next to the chart so I don't trip on it in three months.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed besides the code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A real-browser purchase test runs after every deploy and once a day.&lt;/strong&gt; Playwright with a test account at zero credits opens the pay modal on a phone viewport, taps Buy and checks that the Stripe checkout page loads with the right price. It never pays. If it fails, I get an urgent mail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A watcher on the funnel itself.&lt;/strong&gt; A small daily script compares checkout starts and payments with the previous 14 days. It would have flagged this on day one instead of day four.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test from the user's side first.&lt;/strong&gt; All my early checks went from Stripe backwards. The one that worked went from the user forwards. When a step in a funnel drops to zero, I now start by doing that step myself in a real browser, on the device most visitors use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bug cost about four days of sales on a small site. It could have been worse: the same pattern (shared open state, several dialogs mounted, outside-click to close) is easy to build with any headless dialog library, and it fails silently.&lt;/p&gt;

&lt;p&gt;If you want to see the pay modal that now works on the first tap, it is on &lt;a href="https://aisong.fun/?utm_source=devto&amp;amp;utm_medium=article" rel="noopener noreferrer"&gt;AISong.Fun&lt;/a&gt;, after your first free song.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>webdev</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Our test orders were paying for real AI renders, so we made them dry runs</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Mon, 16 Nov 2026 01:05:02 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/our-test-orders-were-paying-for-real-ai-renders-so-we-made-them-dry-runs-50ji</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/our-test-orders-were-paying-for-real-ai-renders-so-we-made-them-dry-runs-50ji</guid>
      <description>&lt;p&gt;We run a small pay-per-render web app: you upload a selfie, pay once, and a few minutes later you get a 15-second meme video with your face in it. It is one Cloudflare Worker, Stripe Checkout, R2 for files, and a paid video-generation job per order.&lt;/p&gt;

&lt;p&gt;That last part is what this post is about. Every order starts two paid jobs (a main pass and a short segment pass that gets spliced in at the end). Together they cost a little over one US dollar per order. That is fine for a customer order. It is not fine for the orders we create ourselves.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three kinds of orders
&lt;/h2&gt;

&lt;p&gt;Once the site was live, orders came from three places:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Kind&lt;/th&gt;
&lt;th&gt;Who creates it&lt;/th&gt;
&lt;th&gt;Pays?&lt;/th&gt;
&lt;th&gt;Starts a render?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;real&lt;/td&gt;
&lt;td&gt;customers via Stripe Checkout&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;qa&lt;/td&gt;
&lt;td&gt;a headless browser check that opens checkout every day and stops there&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no (stays &lt;code&gt;awaiting_payment&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;test&lt;/td&gt;
&lt;td&gt;us, with an admin token that skips Stripe to test the whole flow&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;yes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;qa&lt;/code&gt; orders were harmless: they never get past checkout, and they carry an &lt;code&gt;@example.com&lt;/code&gt; address so reports can filter them out.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;test&lt;/code&gt; orders were the problem. In launch week they were genuinely useful. Our honest "how long does it take" numbers come from them: 5:11 and 5:20 for one face, 6:57 for all five seats. But the admin path called the same &lt;code&gt;start()&lt;/code&gt; as a paid order, so every "let's just check the order page after this deploy" also bought a real render. With scheduled jobs and post-deploy checks that each like to "test the whole flow", that adds up quietly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The guard
&lt;/h2&gt;

&lt;p&gt;The fix sits on the server, in the two places that start a render for an order. Simplified:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Internal test orders never start a paid render unless a human approved that one run.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;paidRunApproved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'x-allow-paid-run'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'approved-by-owner'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;dryRun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// reports and the order page can tell it apart&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'done'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// same terminal state as a real order&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exampleVideo&lt;/span&gt; &lt;span class="c1"&gt;// the template's published example clip&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;video&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'dry run (no example video)'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;putOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 1) new admin test order (skips Stripe)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isAdmin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'paid'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;paidRunApproved&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;dryRun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`/o/${order.id}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 2) re-using a credit from a multi-video pack&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;paidRunApproved&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;dryRun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`/o/${order.id}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thirteen lines in the real diff. A few decisions behind them:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guard where the money is spent, not where the tests live.&lt;/strong&gt; We could have told every test script to "please don't render". That works until the next script, or the next person, forgets. The Worker is the only place that can start a render, so the rule lives there and every caller gets it for free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make the expensive path opt-in, and make the opt-in loud.&lt;/strong&gt; A boolean like &lt;code&gt;?real=1&lt;/code&gt; is easy to paste into a script and forget. A header whose value reads like a sentence (&lt;code&gt;approved-by-owner&lt;/code&gt;) is hard to add by accident and easy to grep for in logs and code review. Our rule is that it is only sent after the owner says yes to that single run, for example when the generation pipeline itself changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep the state machine identical.&lt;/strong&gt; A dry order ends in &lt;code&gt;done&lt;/code&gt; with a playable video URL, exactly like a real one. So the order page, the video player, the download button and the &lt;code&gt;video_ready&lt;/code&gt; analytics event still run end to end. Only the render is skipped. The video is the example clip already shown on the homepage, so nobody can mistake it for a fresh result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flag it.&lt;/strong&gt; &lt;code&gt;dry: true&lt;/code&gt; on the order lets the daily order report count these separately from real and paid orders, so a dry run can never inflate the numbers we judge the site by.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Find every entry point.&lt;/strong&gt; The first version only guarded new test orders. The second path, re-using a credit from a 3- or 5-video pack, also starts a render, and a test pack would have rendered on every reuse. If your app has "redo", "retry" or "use another credit" buttons, each one is an entry point.&lt;/p&gt;

&lt;h2&gt;
  
  
  What dry runs don't test
&lt;/h2&gt;

&lt;p&gt;The render itself, obviously. We cover that from the other side:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;real orders are monitored: any order that ends in &lt;code&gt;failed&lt;/code&gt;, or a splice step that logs an error, raises an alert;&lt;/li&gt;
&lt;li&gt;when the pipeline changes, one paid test is run on purpose, with the header, after an explicit OK;&lt;/li&gt;
&lt;li&gt;the timings we publish come from those deliberate runs, not from dry ones.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A short checklist if you sell AI output per order
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;List every code path that starts a paid job (new order, retry, redo, pack credit, admin tools).&lt;/li&gt;
&lt;li&gt;Put one guard in front of all of them, on the server.&lt;/li&gt;
&lt;li&gt;Default to the cheap path for anything that is not a real paid order; make the paid path an explicit, greppable opt-in.&lt;/li&gt;
&lt;li&gt;Finish dry runs in the same terminal state as real orders, so the rest of the flow stays covered.&lt;/li&gt;
&lt;li&gt;Flag them, and filter them out of revenue and conversion reports.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The app this came from is &lt;a href="https://tinydesk-ai-video.com/" rel="noopener noreferrer"&gt;Tiny Desk AI Video&lt;/a&gt;, a fan-made parody tool for the "Outstanding" desk-concert meme (not affiliated with NPR, Tiny Desk or the performers). The guard went live on November 15, 2026.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>testing</category>
      <category>cloudflare</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How much of a county property tax ranking is sampling noise? Propagating ACS margins of error</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Sun, 15 Nov 2026 05:05:01 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/how-much-of-a-county-property-tax-ranking-is-sampling-noise-propagating-acs-margins-of-error-4g26</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/how-much-of-a-county-property-tax-ranking-is-sampling-noise-propagating-acs-margins-of-error-4g26</guid>
      <description>&lt;p&gt;A common way to compare property taxes across U.S. counties is the "effective rate": the median real estate tax owners pay, divided by the median home value. Both numbers come from the Census Bureau's American Community Survey (tables B25103 and B25077), and they exist for every county, which is why county rankings are built on them. I publish one of those rankings.&lt;/p&gt;

&lt;p&gt;The ACS is a sample survey, and every estimate ships with a margin of error. Rankings usually drop that column. I wanted to know how much of the ordering survives once it's put back in.&lt;/p&gt;

&lt;p&gt;Short answer: for big counties, nearly all of it. For small ones, very little. The median county's plausible rank spans &lt;strong&gt;760 places&lt;/strong&gt; out of 3,111.&lt;/p&gt;

&lt;h2&gt;
  
  
  The data
&lt;/h2&gt;

&lt;p&gt;The ACS 2020–2024 5-year summary files are pipe-delimited, one row per geography, with estimate (&lt;code&gt;E&lt;/code&gt;) and margin of error (&lt;code&gt;M&lt;/code&gt;) columns side by side. For B25103 the first two values are the median tax and its MOE; for B25077 the median value and its MOE.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"0500000US"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;          &lt;span class="c1"&gt;# 050 = county&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"|"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;

&lt;span class="n"&gt;tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"acsdt5y2024-b25103.dat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# [median tax, MOE]&lt;/span&gt;
&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"acsdt5y2024-b25077.dat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# [median value, MOE]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two kinds of rows need handling before any statistics. Tax medians of &lt;code&gt;10001&lt;/code&gt; ("$10,000 or more") and &lt;code&gt;199&lt;/code&gt; ("less than $200") are caps, not measurements; 24 counties have one, and their rate is a bound, so I left them out here. That leaves 3,111 counties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Carrying the error through a ratio
&lt;/h2&gt;

&lt;p&gt;ACS margins are 90% intervals (1.645 standard errors). The Census Bureau's handbook gives an approximation for the MOE of a ratio X/Y of two estimates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ratio_moe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mx&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fips&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;tax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fips&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fips&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;199&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fips&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;ratio_moe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It ignores the correlation between the two estimates (same sample, same homes), so it's an approximation. It's the one the Bureau documents for this case, and it's good enough to see the size of the problem.&lt;/p&gt;

&lt;p&gt;Typical relative margins: 6.5% on the median tax, 5.9% on the median value, &lt;strong&gt;9.2% on the rate&lt;/strong&gt;. Errors add up when you divide.&lt;/p&gt;

&lt;h2&gt;
  
  
  It depends almost entirely on county size
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Population&lt;/th&gt;
&lt;th&gt;Counties&lt;/th&gt;
&lt;th&gt;Median MOE of the rate&lt;/th&gt;
&lt;th&gt;Relative&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;under 10,000&lt;/td&gt;
&lt;td&gt;724&lt;/td&gt;
&lt;td&gt;±0.164 points&lt;/td&gt;
&lt;td&gt;18.9%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10,000–50,000&lt;/td&gt;
&lt;td&gt;1,404&lt;/td&gt;
&lt;td&gt;±0.074 points&lt;/td&gt;
&lt;td&gt;10.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;50,000–250,000&lt;/td&gt;
&lt;td&gt;710&lt;/td&gt;
&lt;td&gt;±0.040 points&lt;/td&gt;
&lt;td&gt;4.6%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;250,000 and up&lt;/td&gt;
&lt;td&gt;273&lt;/td&gt;
&lt;td&gt;±0.019 points&lt;/td&gt;
&lt;td&gt;2.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For Los Angeles County the rate is 0.680% ± 0.004. For a county of 3,000 people a typical margin is around ±0.16 points, which at a 0.8% rate is the difference between a cheap county and an average one.&lt;/p&gt;

&lt;p&gt;The extremes are not usable as single numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stonewall County, Texas&lt;/strong&gt; (pop. 1,227): median home value $48,600 ± $42,933. The rate comes out at 1.79% ± 1.59.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;De Baca County, New Mexico&lt;/strong&gt;: home value $187,500 ± $163,183, rate 0.60% ± 0.68. The interval runs past zero.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eureka County, Nevada&lt;/strong&gt;: $82,600 ± $77,687.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In 8.6% of counties the margin exceeds a quarter of the rate itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  From intervals to rank ranges
&lt;/h2&gt;

&lt;p&gt;A rank is harder to reason about than a rate, so I converted each interval into the range of ranks it is consistent with. Rank 1 is the highest rate. A county's best plausible rank counts only the counties whose &lt;em&gt;lower&lt;/em&gt; bound is above its upper bound; its worst counts every county whose upper bound reaches its lower bound.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;bisect&lt;/span&gt;

&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;lows&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;highs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rank_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;best&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;bisect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bisect_right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;worst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;bisect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bisect_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;highs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;worst&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Overlapping intervals aren't a formal significance test, so treat these as "can't be ruled out" ranges, not confidence intervals for the rank. They're still informative:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;County&lt;/th&gt;
&lt;th&gt;Rate&lt;/th&gt;
&lt;th&gt;Point rank&lt;/th&gt;
&lt;th&gt;Plausible ranks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Menominee County, WI (pop. 4,252)&lt;/td&gt;
&lt;td&gt;3.56% ± 0.91&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1–12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Salem County, NJ&lt;/td&gt;
&lt;td&gt;2.93% ± 0.12&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1–7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Camden County, NJ&lt;/td&gt;
&lt;td&gt;2.83% ± 0.05&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1–8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cook County, IL&lt;/td&gt;
&lt;td&gt;1.908% ± 0.013&lt;/td&gt;
&lt;td&gt;109&lt;/td&gt;
&lt;td&gt;77–191&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stonewall County, TX (pop. 1,227)&lt;/td&gt;
&lt;td&gt;1.79% ± 1.59&lt;/td&gt;
&lt;td&gt;146&lt;/td&gt;
&lt;td&gt;1–3,109&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harris County, TX&lt;/td&gt;
&lt;td&gt;1.623% ± 0.020&lt;/td&gt;
&lt;td&gt;234&lt;/td&gt;
&lt;td&gt;135–396&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Los Angeles County, CA&lt;/td&gt;
&lt;td&gt;0.680% ± 0.004&lt;/td&gt;
&lt;td&gt;1,940&lt;/td&gt;
&lt;td&gt;1,653–2,214&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maricopa County, AZ&lt;/td&gt;
&lt;td&gt;0.438% ± 0.003&lt;/td&gt;
&lt;td&gt;2,817&lt;/td&gt;
&lt;td&gt;2,571–2,940&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Los Angeles row is the surprising one. Its own margin is tiny, yet its rank still spans 561 places, because hundreds of small counties with wide intervals sit near its rate. A precise estimate doesn't buy a precise rank when the neighbours are noisy.&lt;/p&gt;

&lt;p&gt;The top of the table holds up reasonably well: New Jersey and upstate New York counties lead under any reading, and Menominee's wide margin still keeps it in the top 12. In the middle of the distribution, ranks mean little. The median county's range is 760 places wide, and 502 counties have an interval that includes the median county rate (0.804%). For those, "above or below average" isn't settled by the data.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Highest in the state" is often a tie
&lt;/h2&gt;

&lt;p&gt;State pages love a "highest property tax county in X" line. I tested the top two counties in each state with the Census difference test (the difference is significant at 90% if it exceeds &lt;code&gt;sqrt(moe_a**2 + moe_b**2)&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;differs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ra&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ma&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ra&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ma&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;mb&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In only &lt;strong&gt;15 of 50 states&lt;/strong&gt; is the #1 county significantly above #2. A few cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New Jersey: Salem 2.93 ± 0.12 vs Camden 2.83 ± 0.05. Not distinguishable.&lt;/li&gt;
&lt;li&gt;New York: Allegany 2.92 ± 0.14 vs Orleans 2.72 ± 0.16. Difference 0.20, threshold 0.21. Not quite.&lt;/li&gt;
&lt;li&gt;Texas: El Paso 2.03 ± 0.04 vs Menard 1.95 ± 0.65. Menard's margin covers almost every Texas county.&lt;/li&gt;
&lt;li&gt;Illinois: Lake 2.58 ± 0.04 vs DeKalb 2.45 ± 0.07. This one is real.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I took from it
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keep the MOE column through the pipeline.&lt;/strong&gt; It costs nothing to load, and once it's dropped nobody downstream can get it back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Population is a good proxy for trust.&lt;/strong&gt; Under ~10,000 people, read the rate as a range, not a number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefer statutory rates where they exist.&lt;/strong&gt; Texas, Florida and North Carolina publish adopted rates per taxing unit; those have no sampling error, only the transcription errors of a big spreadsheet. The ACS rate answers a different question anyway: what owners actually pay relative to what they think their home is worth, after exemptions and caps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Say "among the highest", not "the highest", unless the difference test passes.&lt;/strong&gt; It's 15 states out of 50.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rank ranges are a better headline than ranks for the middle of the table.&lt;/strong&gt; A county at #1,400 ± 380 is "about average", and that is the honest description.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The county figures on &lt;a href="https://countypropertytaxes.com/" rel="noopener noreferrer"&gt;countypropertytaxes.com&lt;/a&gt; are the ACS point estimates described here, labeled as estimates.&lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>opendata</category>
      <category>statistics</category>
    </item>
    <item>
      <title>Recasting a 15-second meme with five faces: why the last two seconds needed their own pass</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Sat, 14 Nov 2026 07:15:20 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/recasting-a-15-second-meme-with-five-faces-why-the-last-two-seconds-needed-their-own-pass-2l82</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/recasting-a-15-second-meme-with-five-faces-why-the-last-two-seconds-needed-their-own-pass-2l82</guid>
      <description>&lt;p&gt;The "Outstanding" desk-concert meme blew up on TikTok in October: a soul band squeezed behind an office desk, a singer in a headset going all in, a red keyboard, two backup singers and a sax solo to close. People wanted it with their own friends in it, so we built a small tool for that: upload up to five photos, get the same 15-second clip back with those faces in the five seats.&lt;/p&gt;

&lt;p&gt;This post is about the parts that were not obvious: building a clean base clip, getting five faces into the right seats, and one short shot the video model refused to handle.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Build the base clip once, not per order
&lt;/h2&gt;

&lt;p&gt;The viral original features real, recognisable people and team logos. We did not want to ship either, so the first step was a one-time "recast": generate a version of the clip with fictional people and plain clothes, keeping the exact shots, cuts, timing and audio.&lt;/p&gt;

&lt;p&gt;That base clip is what every order starts from. Two mistakes we made along the way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The group shot had too many people.&lt;/strong&gt; We passed one group photo as the reference for "everyone in the back", and the model happily put all five people from that photo into a shot that only has three. Fix: one single-person reference image per seat, and instructions that state the exact head count per shot ("the group shot has exactly three people; do not add anyone").&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A logo survived in the first second.&lt;/strong&gt; The opening shot is a side profile and the model kept the original patch on the hoodie. A second pass over the base clip with "remove every logo, badge and text from clothing" fixed it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because the base clip is generated once, those mistakes cost a few test runs, not one per customer.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Five seats, described by shot
&lt;/h2&gt;

&lt;p&gt;The clip has three camera shots:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Shot&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Who is visible&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Close-up&lt;/td&gt;
&lt;td&gt;0.0–1.3 s, 6.5–12.8 s&lt;/td&gt;
&lt;td&gt;lead singer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Group shot&lt;/td&gt;
&lt;td&gt;1.3–6.5 s&lt;/td&gt;
&lt;td&gt;keyboard player (front), two backup singers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Close-up&lt;/td&gt;
&lt;td&gt;12.8–15.0 s&lt;/td&gt;
&lt;td&gt;sax player (+ keyboard player at the edge)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first instructions described each seat by appearance ("the man in the blue shirt gets photo 3"). It mostly worked, except that features started leaking between seats: the keyboard player got one photo's beard and another photo's glasses and shirt.&lt;/p&gt;

&lt;p&gt;What helped was describing seats &lt;strong&gt;by where they appear&lt;/strong&gt;, not only by what they look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;keyboard seat = the person sitting in front at the red keyboard in the group shot,
who is also visible at the edge of the final sax shot → photo 2
...
each photo is a different person; never combine the face, hair, glasses
or clothes of two photos into one person
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that change, four of the five seats landed correctly on the first try.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The 2-second shot the model kept ignoring
&lt;/h2&gt;

&lt;p&gt;The fifth seat, the sax player, did not work. Three different instructions, three runs, and the sax close-up at the end always came back with the stand-in's face. Worse, the sax photo's features showed up on the keyboard player at the edge of that shot.&lt;/p&gt;

&lt;p&gt;Our reading: a 2.2-second shot at the very end of a 15-second clip simply carries too little weight in a single generation pass.&lt;/p&gt;

&lt;p&gt;So we stopped asking one pass to do everything:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;main pass&lt;/strong&gt; fills lead, keyboard and both backup singers, with the sax seat left out.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;segment pass&lt;/strong&gt; runs on a 3-second cut of the base clip (12.0–15.0 s) with only the sax photo (plus the keyboard photo, because he is visible at the edge). On a short clip where the sax player fills the frame, it works on the first try, and it costs a fraction of the main pass.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;splicer&lt;/strong&gt; puts them together: main video up to the cut at 12.833 s, then the segment from its own 0.833 s mark (the same cut point) to the end, with the main video's audio on top.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The segment starts 0.8 s before the cut on purpose: the model gets a little lead-in context, and we throw that part away.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The splicer: 60 lines of Python around ffmpeg
&lt;/h2&gt;

&lt;p&gt;The web app runs on a serverless platform with no ffmpeg, so the splice runs on a small server we already had. The flow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When both jobs finish, the order goes into a &lt;code&gt;splicing&lt;/code&gt; state and is added to a short queue.&lt;/li&gt;
&lt;li&gt;A cron job on the server runs every minute: it lists queued orders over an authenticated endpoint, downloads both videos, splices them and uploads the result. The order then flips to &lt;code&gt;done&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the server is down, the web app delivers the main video (without the sax swap) after 10 minutes, so no order gets stuck.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core of the splice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;norm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'scale={W}:{H}:force_original_aspect_ratio=increase,crop={W}:{H},setsar=1,fps={fps:.4f}'&lt;/span&gt;
&lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'[0:v]trim=start=0:end={at},setpts=PTS-STARTPTS,{norm}[p0]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'[1:v]trim=start={skip}:duration={D - at},setpts=PTS-STARTPTS,{norm}[s0]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'[p0][s0]concat=n=2:v=1:a=0,tpad=stop_mode=clone:stop_duration=0.2,trim=duration={D}[v]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;check_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'ffmpeg'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-y'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-i'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-i'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'-filter_complex'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;';'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'-map'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'[v]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-map'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0:a?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;            &lt;span class="c1"&gt;# audio always from the main video&lt;/span&gt;
    &lt;span class="s1"&gt;'-c:v'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'libx264'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-crf'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'20'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-pix_fmt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'yuv420p'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'-c:a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'aac'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-map_metadata'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;# drop container metadata&lt;/span&gt;
    &lt;span class="s1"&gt;'-movflags'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'+faststart'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-t'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'{D}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few details that mattered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Normalise both inputs&lt;/strong&gt; (size, SAR, fps) before &lt;code&gt;concat&lt;/code&gt;, even if they "should" match. Generated clips can differ by a pixel or a fraction of a frame rate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;tpad&lt;/code&gt; + &lt;code&gt;trim=duration&lt;/code&gt;&lt;/strong&gt; guarantees the video is exactly as long as the audio. Without it, the segment's slightly different length left a black frame or cut the last beat of the sax.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Take audio from one source only.&lt;/strong&gt; The original song runs across the cut; mixing two generated audio tracks would make the cut audible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-movflags +faststart&lt;/code&gt;&lt;/strong&gt; so the result starts playing on a phone before it is fully downloaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the server the splice takes about 14 seconds for a 15-second 480p clip. The whole order, from payment to download, came in at about six and a half minutes.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Results and what we would do again
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;One base clip, generated once, reused for every order. It is the single most important decision: a clean, logo-free base makes every order cleaner.&lt;/li&gt;
&lt;li&gt;Describe seats by shot and screen position, not just appearance, and say explicitly that images must not be mixed.&lt;/li&gt;
&lt;li&gt;When one short shot keeps failing, cut it out and give it its own small job instead of fighting the instructions.&lt;/li&gt;
&lt;li&gt;Keep the fallback boring: if the splice step is unavailable, deliver the main video after a timeout.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to see the result, the tool is live at &lt;a href="https://outstandingaivideo.net" rel="noopener noreferrer"&gt;outstandingaivideo.net&lt;/a&gt;: the example on the page was made from five AI-generated test portraits, one per seat, with nothing edited afterwards.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>video</category>
      <category>ffmpeg</category>
      <category>webdev</category>
    </item>
    <item>
      <title>"Stop shouldn't mean close: ending a live-caption session in a Manifest V3 extension without losing the last sentence"</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Sat, 14 Nov 2026 01:05:02 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/stop-shouldnt-mean-close-ending-a-live-caption-session-in-a-manifest-v3-extension-without-losing-37kn</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/stop-shouldnt-mean-close-ending-a-live-caption-session-in-a-manifest-v3-extension-without-losing-37kn</guid>
      <description>&lt;p&gt;I maintain a browser extension that shows live captions (and translated captions) on top of any tab: a video, a livestream, a web meeting. Audio is captured in the browser, streamed over a WebSocket to a server, and recognised text comes back sentence by sentence.&lt;/p&gt;

&lt;p&gt;Starting a session got all the attention. Stopping was one line: close the socket. That line was the reason a lot of first sessions ended with an empty screen. This post is about what "Stop" has to mean in a streaming pipeline, and the extra wrinkles a Manifest V3 extension adds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the last sentence disappears
&lt;/h2&gt;

&lt;p&gt;A streaming recogniser sends two kinds of results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;partial&lt;/strong&gt; results: fast guesses that keep changing, good for showing "the app is listening";&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;final&lt;/strong&gt; results: settled text. Only finals are split into sentences, translated and saved.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finals always lag behind the audio. The recogniser waits for a little right-hand context before it commits a word. So at the moment the user presses Stop, the sentence they just said exists &lt;strong&gt;only as a partial&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If Stop closes the socket, the server tells the recogniser "end of audio" after the client is already gone. The recogniser dutifully sends the final for the last sentence… to nobody. Someone who says one short sentence to test the tool and presses Stop sees nothing at all, and decides the tool doesn't work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop is a process: the handshake
&lt;/h2&gt;

&lt;p&gt;The fix is to separate &lt;em&gt;"stop sending audio"&lt;/em&gt; from &lt;em&gt;"close the connection"&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension                     server                         recogniser
  │ stop capture                │                                 │
  │── {type:'stop'} ──────────► │ stop accepting audio + billing  │
  │                             │── end of stream ──────────────► │
  │                             │ ◄────────── final of last sentence
  │ ◄── last sentence + translation                               │
  │                             │ ◄────────────── end of transcript
  │                             │ wait for in-flight translations │
  │ ◄──── {type:'stopped'} ──── │                                 │
  │ close socket                │                                 │
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the server that means two flags instead of one: &lt;code&gt;stopping&lt;/code&gt; (no new audio, but results are still processed) and &lt;code&gt;stopped&lt;/code&gt; (ignore everything). With a single flag there is no "draining" window, and the last sentence falls exactly into the gap. One more trap from my own code: the server ignored &lt;em&gt;all&lt;/em&gt; text frames so that control JSON could never leak into the audio stream — which also swallowed the &lt;code&gt;stop&lt;/code&gt; message. Parse text frames and handle the few control messages you support; keep binary frames as audio.&lt;/p&gt;

&lt;p&gt;The rest of this post is the client side, because in an extension "the client" is three separate contexts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where things live in an MV3 extension
&lt;/h2&gt;

&lt;p&gt;In Chrome and Edge, a Manifest V3 extension has no persistent background page. My layout:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;service worker&lt;/strong&gt; (background): handles the toolbar button and keyboard shortcut, gets a tab-capture stream id, creates and closes the offscreen document, stores session state;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;offscreen document&lt;/strong&gt;: the only place that can hold a &lt;code&gt;MediaStream&lt;/code&gt; and an &lt;code&gt;AudioContext&lt;/code&gt;; it captures the tab audio, plays it back (capturing a tab mutes it) and runs the caption engine with the WebSocket;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;overlay / popup / side panel&lt;/strong&gt;: show the captions and the state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The old Stop path in the offscreen document was: stop the audio, close the socket, report &lt;code&gt;stopped&lt;/code&gt;. The service worker reacted to &lt;code&gt;stopped&lt;/code&gt; by closing the offscreen document. Every step was correct on its own, and together they guaranteed that nothing arriving after Stop could ever be shown.&lt;/p&gt;

&lt;h2&gt;
  
  
  A &lt;code&gt;stopping&lt;/code&gt; state in the engine
&lt;/h2&gt;

&lt;p&gt;The engine now has a state between &lt;code&gt;listening&lt;/code&gt; and &lt;code&gt;stopped&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;STOP_WAIT_MS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// longer than the server's own waits added together&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;silent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// pushAudio() checks ws, so no more audio is sent&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readyState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OPEN&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;silent&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'stop'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;draining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;STOP_WAIT_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'stopping'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// keep the socket open&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;silent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;draining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;persistTranscript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'stopped'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only a &lt;strong&gt;normal&lt;/strong&gt; Stop drains. Errors (quota used up, network gone, capture failed) and a session being replaced still close at once — there is nothing useful to wait for.&lt;/p&gt;

&lt;p&gt;The message handler accepts results from the draining socket, and &lt;code&gt;stopped&lt;/code&gt; (or the socket closing) ends the drain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;mine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;draining&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;mine&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'stopped'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;onMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                  &lt;span class="c1"&gt;// last sentence + its translation still render&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onclose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;draining&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;draining&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...unexpected close: reconnect with backoff&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the identity checks against &lt;code&gt;sock&lt;/code&gt;, not the global &lt;code&gt;ws&lt;/code&gt;. Users press Stop and then Start again within a second. The new session gets a new socket; the old one may still be draining. Without the comparison, the old socket's &lt;code&gt;stopped&lt;/code&gt; would end the new session, or its fallback timer would close the new connection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Audio stops immediately, the socket doesn't
&lt;/h2&gt;

&lt;p&gt;In the offscreen document, Stop still tears down capture right away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'stop'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;stopAudio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;stopAudio()&lt;/code&gt; stops the tracks and closes the &lt;code&gt;AudioContext&lt;/code&gt;, so the tab is no longer captured (and it is audible again in the normal way), and no more audio is billed. Only the WebSocket lives on for a few seconds. Users see "Stopping…" and then the last sentence appears.&lt;/p&gt;

&lt;h2&gt;
  
  
  The service worker must not kill the offscreen document early
&lt;/h2&gt;

&lt;p&gt;The background now closes the offscreen document only on the final state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onEngineState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'stopped'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;closeOffscreen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// let the overlay get the final state&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;closeOffscreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'run'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'stopped'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'idle'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// a new session started meanwhile&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;hasOffscreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offscreen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;closeDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The re-check inside &lt;code&gt;closeOffscreen()&lt;/code&gt; matters for the same Stop-then-Start case: a late &lt;code&gt;stopped&lt;/code&gt; from the old session must not close the document that the new session is already using. When a new session starts while one exists, the background first sends a quiet &lt;code&gt;release&lt;/code&gt; (stop without a report) instead of a normal stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Firefox: the audio port disconnect
&lt;/h2&gt;

&lt;p&gt;Firefox MV3 has no offscreen documents; the background page runs the engine and a content script captures the media element and posts PCM through a runtime port. When the page closes or the capture ends, the port disconnects and the background stops the session. During &lt;code&gt;stopping&lt;/code&gt; that disconnect is expected — capture already ended because the user pressed Stop — so it must not turn into a second, immediate stop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onDisconnect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'idle'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'stopped'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'stopping'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus: telling "silent tab" from "quiet speaker"
&lt;/h2&gt;

&lt;p&gt;The other cause of empty first sessions wasn't the stop path at all: the user started captions on a tab that was paused or muted, or picked "tab audio" while talking into the microphone.&lt;/p&gt;

&lt;p&gt;A muted or paused tab doesn't produce quiet audio — it produces &lt;strong&gt;exact zeros&lt;/strong&gt;. Real silence from a microphone or a video still has a noise floor. So the engine tracks two clocks from the RMS of each audio frame:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;DIGITAL_SILENCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1e-5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// below this, the samples are (practically) all zero&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;NO_SOUND_HINT_MS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;pushAudio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pcm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rms&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;DIGITAL_SILENCE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lastSignalAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;notice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// sound came back: clear the hint&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notice&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lastSignalAt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;NO_SOUND_HINT_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;notice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;audio&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'mic'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'nosound-mic'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'nosound-tab'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The hint only appears while there are no captions yet, so it never nags in the middle of a long pause, and it names the fix for the chosen source ("play or unmute the tab, or switch to the microphone"). A separate, much longer timer with a higher threshold ends sessions where nothing has been heard for minutes, so a forgotten tab doesn't keep running.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to test whenever you touch Stop
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Say one short sentence and press Stop immediately → the sentence and its translation appear.&lt;/li&gt;
&lt;li&gt;Press Stop while the socket is still connecting → no error, no stuck button.&lt;/li&gt;
&lt;li&gt;Stop and Start repeatedly → no session closes another one.&lt;/li&gt;
&lt;li&gt;Close the captured tab during &lt;code&gt;stopping&lt;/code&gt; → the session ends cleanly.&lt;/li&gt;
&lt;li&gt;Throttle the network in DevTools → &lt;code&gt;stopped&lt;/code&gt; still arrives before the fallback timer.&lt;/li&gt;
&lt;li&gt;Start on a muted tab → the hint appears; unmute → it disappears.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Logging &lt;em&gt;why&lt;/em&gt; each session ended on the server (&lt;code&gt;end of transcript&lt;/code&gt;, &lt;code&gt;timeout&lt;/code&gt;, &lt;code&gt;recogniser closed&lt;/code&gt;) tells you whether the waits are too short for real recogniser latency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;In a streaming system, Stop is not an event but a short protocol: stop the input, tell the next layer, wait for it to finish, report back, then close. In an MV3 extension that protocol spans a socket, an offscreen document and a service worker — and every one of them has to know that "stopping" is not "stopped".&lt;/p&gt;

&lt;p&gt;The extension is part of LiveToka, which does live captions and translation in the browser: &lt;a href="https://livetoka.com/extension/" rel="noopener noreferrer"&gt;https://livetoka.com/extension/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>websocket</category>
      <category>extensions</category>
    </item>
    <item>
      <title>A 50-state car sales tax table doesn't fit in one "rate" column</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Fri, 13 Nov 2026 01:05:54 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/a-50-state-car-sales-tax-table-doesnt-fit-in-one-rate-column-mnl</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/a-50-state-car-sales-tax-table-doesnt-fit-in-one-rate-column-mnl</guid>
      <description>&lt;p&gt;I wanted a simple table: 50 US states, one row each, "how much tax do you pay when you buy a car". The first draft had two columns, &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;rate&lt;/code&gt;. It was wrong for roughly half the states, not because the numbers were off, but because the columns were.&lt;/p&gt;

&lt;p&gt;This post covers the schema I ended up with and the rules that forced each column. Every row is checked against the state's revenue department or DMV page, and the source URL is kept in the row.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The tax isn't always a "sales tax"
&lt;/h2&gt;

&lt;p&gt;Of 50 states, 22 either have no state sales tax on cars or call the tax on a car purchase something other than a plain "sales tax". A few examples from the table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;What it's called&lt;/th&gt;
&lt;th&gt;Rate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Georgia&lt;/td&gt;
&lt;td&gt;Title ad valorem tax (no sales tax)&lt;/td&gt;
&lt;td&gt;7% of fair market value, once at titling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;North Carolina&lt;/td&gt;
&lt;td&gt;Highway use tax&lt;/td&gt;
&lt;td&gt;3%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delaware&lt;/td&gt;
&lt;td&gt;Document fee (no sales tax)&lt;/td&gt;
&lt;td&gt;5.25%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Iowa&lt;/td&gt;
&lt;td&gt;Fee for new registration (no sales tax)&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;South Carolina&lt;/td&gt;
&lt;td&gt;Infrastructure maintenance fee&lt;/td&gt;
&lt;td&gt;5%, capped at $500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Oklahoma&lt;/td&gt;
&lt;td&gt;Excise tax + sales tax&lt;/td&gt;
&lt;td&gt;3.25% + 1.25%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kentucky&lt;/td&gt;
&lt;td&gt;Motor vehicle usage tax&lt;/td&gt;
&lt;td&gt;6%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This matters beyond naming. These are often separate taxes with their own base and their own exemptions, so a general sales tax rate table (the kind built for retail) gives the wrong answer for a car. Georgia is the clearest case: its general sales tax doesn't apply to the vehicle purchase at all; the 7% TAVT replaces it.&lt;/p&gt;

&lt;p&gt;So the first column became &lt;code&gt;tax&lt;/code&gt; — the name the state uses — next to &lt;code&gt;state_rate&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. "Is there local tax?" is a separate yes/no
&lt;/h2&gt;

&lt;p&gt;22 states add a county or city rate on top of the state rate. That's a boolean in the table (&lt;code&gt;local_tax&lt;/code&gt;), and the state rate alone is shown as the floor, not the answer.&lt;/p&gt;

&lt;p&gt;Which local rate applies is a rule of its own. In most of these states it's the rate of the address where the car is registered — Missouri, Nebraska, New York, Ohio and Wyoming all say so in some form. Nevada is the other way around: the rate of the county where the sale happens. Kansas uses the higher of the dealer's and your home rate. I kept those rules in a free-text &lt;code&gt;note&lt;/code&gt; rather than inventing an enum for them, because there are almost as many variants as states.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Trade-in credit needs six values, not two
&lt;/h2&gt;

&lt;p&gt;My first instinct was &lt;code&gt;trade_in_lowers_tax: boolean&lt;/code&gt;. The data disagreed:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;States&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;yes&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;35&lt;/td&gt;
&lt;td&gt;Tax on price minus trade-in&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;no&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;CA, OK, VA&lt;/td&gt;
&lt;td&gt;No credit; you pay tax on the full price&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dealer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;GA, ID, IL, MA&lt;/td&gt;
&lt;td&gt;Credit only when a dealer takes the trade-in in the same deal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OH&lt;/td&gt;
&lt;td&gt;Credit only on a new vehicle&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;capped&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;MI&lt;/td&gt;
&lt;td&gt;Credit limited ($12,000 in 2026)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;(empty)&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;AK, HI, MT, NH, OR, SC&lt;/td&gt;
&lt;td&gt;No state tax to reduce, or the rule doesn't apply&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On a $40,000 car with a $15,000 trade-in, the difference between &lt;code&gt;yes&lt;/code&gt; and &lt;code&gt;no&lt;/code&gt; at a 6% rate is $900. That's the single most common reason two "car tax calculators" disagree.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Caps and thresholds live inside the rate
&lt;/h2&gt;

&lt;p&gt;Several states have a percentage that only applies to part of the price:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Florida&lt;/strong&gt;: the county surtax (up to 2%) applies to the first $5,000 only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tennessee&lt;/strong&gt;: local tax applies only to the first $1,600, plus a 2.75% state tax on the next $1,600.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;South Carolina&lt;/strong&gt;: 5%, but never more than $500.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connecticut&lt;/strong&gt;: 6.35%, or 7.75% on vehicles over $50,000.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arkansas&lt;/strong&gt;: used vehicles under $4,000 are exempt; $4,000–$9,999 pay a lower 3.5% state rate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A rate column that says "Florida 6%–8%" is technically true and practically misleading — on a $30,000 car the county part adds at most $100. In the table these rules go into the note; in the calculator each one is code.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The taxable amount isn't always the price
&lt;/h2&gt;

&lt;p&gt;Some states tax a valuation, not what you paid, at least for private sales:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Texas&lt;/strong&gt;: private sales are taxed on at least 80% of the standard presumptive value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Massachusetts&lt;/strong&gt;: private sales on the higher of the price or the NADA trade-in value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vermont&lt;/strong&gt;: the higher of the price or the J.D. Power clean trade-in value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Mexico&lt;/strong&gt;: used cars on the higher of the price or 80% of NADA value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delaware&lt;/strong&gt;: the higher of the price or the NADA average trade-in value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Writing a low number on the bill of sale doesn't help in these states, and any estimate built only on the price undercounts.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Rates change more often than you'd think
&lt;/h2&gt;

&lt;p&gt;Since the start of 2025 alone: Louisiana's state rate rose to 5% (2025), Maryland's excise went from 6% to 6.5% (July 1, 2025), Delaware's document fee rose to 5.25% (October 1, 2025), and Washington added a 0.5% motor vehicle sales tax on January 1, 2026. Every row has an &lt;code&gt;as_of&lt;/code&gt; date, and the table as a whole carries the date it was last checked. A rate without a date is a guess about the present.&lt;/p&gt;

&lt;h2&gt;
  
  
  The final schema
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state, code, tax, state_rate, local_tax, trade_in_lowers_tax, note, as_of
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plus a source URL per row (in the JSON; the CSV keeps it short). Eight columns, one of which is free text, which feels unsatisfying from a modeling point of view. But every attempt to normalize the notes into more enums produced columns with one or two states each. For a reference table, a precise sentence beats a sparse column.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell someone building the same thing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Start from each state's official page, not from an aggregator. The general retail rate tables are the wrong source for vehicles in at least a third of states.&lt;/li&gt;
&lt;li&gt;Model the trade-in rule and the taxable base explicitly; that's where estimates diverge most.&lt;/li&gt;
&lt;li&gt;Show the state rate as a floor when local tax exists, and say which address decides the local rate.&lt;/li&gt;
&lt;li&gt;Date every row.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The table, with a source link in every row, is on &lt;a href="https://carsalestax.com/#all-states" rel="noopener noreferrer"&gt;carsalestax.com&lt;/a&gt;; the CSV is linked from the site's methodology page.&lt;/p&gt;

</description>
      <category>opendata</category>
      <category>datascience</category>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>"Rebuilding all 405 rows of a steel pipe weight table from one formula: which constant did they use?"</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Thu, 12 Nov 2026 01:05:22 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/rebuilding-all-405-rows-of-a-steel-pipe-weight-table-from-one-formula-which-constant-did-they-3hmg</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/rebuilding-all-405-rows-of-a-steel-pipe-weight-table-from-one-formula-which-constant-did-they-3hmg</guid>
      <description>&lt;p&gt;Steel pipe weight tables look like data: 52 nominal sizes from 1/8" to 108", up to 17 schedules each,&lt;br&gt;
405 rows with outside diameter, wall thickness and weight per foot and per metre (ASME B36.10M / B36.19M&lt;br&gt;
values, as tabulated in Wikipedia's "Nominal Pipe Size" article). I keep them as JSON&lt;br&gt;
for a weight calculator, and I wanted a test that every row is right, not just "close enough".&lt;/p&gt;

&lt;p&gt;A pipe is a ring, so the weight should follow from two numbers. The question was whether &lt;em&gt;all&lt;/em&gt; of the&lt;br&gt;
table follows from them, to the last printed digit. It does, but only once you use the same constants,&lt;br&gt;
and the same starting dimensions, as the people who made the table.&lt;/p&gt;
&lt;h2&gt;
  
  
  The formula
&lt;/h2&gt;

&lt;p&gt;The cross-section of a pipe with outside diameter D and wall t is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = π/4 × (D² − (D − 2t)²) = π × (D − t) × t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Weight per length is area × density. Carbon steel is 0.2836 lb/in³ (that is 7850 kg/m³, the same&lt;br&gt;
steel), and a foot is 12 in, so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lb/ft = 0.2836 × 12 × π × (D − t) × t = 10.6915 × (D − t) × t      (D, t in inches)
kg/m  = 7850 × π × 10⁻⁶ × (D − t) × t = 0.0246615 × (D − t) × t    (D, t in mm)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Test 1: does the table follow the textbook constant?
&lt;/h2&gt;

&lt;p&gt;The weights are printed to two decimals, so "exact" means: compute, round to 0.01, compare.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;

&lt;span class="n"&gt;pipes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pipe.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"pipes"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;pipes&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"schedules"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"lb_ft"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"od_in"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"t_in"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"t_in"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2836&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 202 of 405&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10.69&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                   &lt;span class="c1"&gt;# 405 of 405&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the physically "correct" constant 10.6915, only &lt;strong&gt;202 of 405&lt;/strong&gt; rows match. With the rounded&lt;br&gt;
&lt;strong&gt;10.69&lt;/strong&gt;, all &lt;strong&gt;405&lt;/strong&gt; do. The difference is 0.014 %, invisible in practice, but it moves the&lt;br&gt;
second decimal on half the larger sizes (e.g. 24" Sch 140: 483.57 with 10.69, 483.64 with 10.6915).&lt;/p&gt;

&lt;p&gt;That is not a coincidence or an error in the table: ASME B36.10M itself gives the plain-end weight as&lt;br&gt;
&lt;code&gt;10.69 × (D − t) × t&lt;/code&gt;. The table is generated from that formula, not measured. If your calculator uses&lt;br&gt;
10.6915 (or 0.2836 lb/in³ from a materials page), you will "disagree" with the standard on 203 rows.&lt;br&gt;
Use the standard's constant when you reproduce the standard's table.&lt;/p&gt;
&lt;h2&gt;
  
  
  Test 2: the metric column
&lt;/h2&gt;

&lt;p&gt;The table also prints D and t in millimetres, so the obvious check is the metric formula on the metric&lt;br&gt;
dimensions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;matches_mm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_inches&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="no"&gt;D&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"od_in"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;25.4&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;from_inches&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"od_mm"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"t_in"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;25.4&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;from_inches&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"t_mm"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"kg_m"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;D&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;matches_mm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0246615&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_inches&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 201 of 405&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;matches_mm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0246615&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_inches&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 404 of 405&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;matches_mm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.02466&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="n"&gt;from_inches&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 235 of 405&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the printed millimetres only &lt;strong&gt;201&lt;/strong&gt; rows match. The reason is that the printed mm values are&lt;br&gt;
rounded: a 3/8" wall is 9.525 mm, printed as 9.52; 12.75" OD is 323.85 mm, printed as 323.8.&lt;br&gt;
The kg/m column was computed &lt;strong&gt;from the exact inch dimensions converted to mm&lt;/strong&gt;, not from the mm&lt;br&gt;
numbers printed next to it. Do that and 404 rows match.&lt;/p&gt;

&lt;p&gt;The 405th (24" Sch 140) computes to 719.734987… and the table says 719.74. That is a floating-point&lt;br&gt;
tie: with the full constant 7850 × π × 10⁻⁶ = 0.02466150233 it is 719.73506, which rounds up. So all&lt;br&gt;
405 rows are reproducible. The commonly quoted short constant 0.02466 only gets 235.&lt;/p&gt;

&lt;p&gt;Two lessons in one column:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Metric pipe dimensions are conversions, not a separate design.&lt;/strong&gt; NPS pipe is an inch standard.
Store the inch values as the source of truth and derive millimetres for display.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never compute from a rounded display value&lt;/strong&gt; when the unrounded one exists. Here the error is
at most 0.14 % (on rows over 5 kg/m), small but enough to fail an exact test and to make two
calculators disagree.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Test 3: don't convert one weight column into the other
&lt;/h2&gt;

&lt;p&gt;A tempting shortcut is to store lb/ft only and show kg/m = lb/ft × 1.488164. For big pipes that is&lt;br&gt;
fine. For small ones it is not, because lb/ft is already rounded to 0.01:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;lb/ft (table)&lt;/th&gt;
&lt;th&gt;× 1.488164&lt;/th&gt;
&lt;th&gt;kg/m (table)&lt;/th&gt;
&lt;th&gt;Error&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1/8" Sch 30&lt;/td&gt;
&lt;td&gt;0.21&lt;/td&gt;
&lt;td&gt;0.313&lt;/td&gt;
&lt;td&gt;0.32&lt;/td&gt;
&lt;td&gt;2.3 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1/8" Sch 80&lt;/td&gt;
&lt;td&gt;0.31&lt;/td&gt;
&lt;td&gt;0.461&lt;/td&gt;
&lt;td&gt;0.47&lt;/td&gt;
&lt;td&gt;1.8 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1/4" Sch 40&lt;/td&gt;
&lt;td&gt;0.43&lt;/td&gt;
&lt;td&gt;0.640&lt;/td&gt;
&lt;td&gt;0.63&lt;/td&gt;
&lt;td&gt;1.6 %&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The rounding step of 0.01 lb/ft is 4 % of a 1/4 lb weight. Both columns have to be computed from the&lt;br&gt;
geometry, each in its own unit, and rounded once at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this means for a calculator
&lt;/h2&gt;

&lt;p&gt;If you build one from this table: keep OD and wall in inches as the stored values, compute lb/ft with&lt;br&gt;
10.69 and kg/m with 7850 × π × 10⁻⁶ from the converted dimensions, and add a test that walks all 405&lt;br&gt;
rows and requires an exact match at two decimals. When an imported row then fails, it is a&lt;br&gt;
transcription error, not a "rounding thing", and the test tells you which row.&lt;/p&gt;

&lt;p&gt;The same pattern works for any table generated from a formula (rebar, where lb/ft = 2.67 × d² holds&lt;br&gt;
within 0.2 % for #3 to #18, flat bar, sheet gauges): find the generating formula, find &lt;em&gt;their&lt;/em&gt;&lt;br&gt;
constant, find &lt;em&gt;their&lt;/em&gt; source unit, and the whole table becomes a one-line test.&lt;/p&gt;

&lt;p&gt;The full table, with a per-size calculator for plain-end weight and the formula worked through for&lt;br&gt;
each NPS, is on the &lt;a href="https://steelweight-calculator.com/pipe/" rel="noopener noreferrer"&gt;steel pipe schedule chart&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>engineering</category>
      <category>testing</category>
    </item>
    <item>
      <title>US duty rates are not numbers: parsing 747 rate formats from the tariff schedule in Python</title>
      <dc:creator>anders lee</dc:creator>
      <pubDate>Wed, 11 Nov 2026 01:05:04 +0000</pubDate>
      <link>https://dev.to/anders_lee_28dbcc2fd8e034/us-duty-rates-are-not-numbers-parsing-747-rate-formats-from-the-tariff-schedule-in-python-31p4</link>
      <guid>https://dev.to/anders_lee_28dbcc2fd8e034/us-duty-rates-are-not-numbers-parsing-747-rate-formats-from-the-tariff-schedule-in-python-31p4</guid>
      <description>&lt;p&gt;The duty rate column of the US tariff schedule looks like the easy part. It reads like a percentage: &lt;code&gt;6.5%&lt;/code&gt;, &lt;code&gt;3.7%&lt;/code&gt;, &lt;code&gt;Free&lt;/code&gt;. Try &lt;code&gt;float(rate.rstrip("%"))&lt;/code&gt; on it, though, and it fails on the very first declarable line, and special-casing &lt;code&gt;Free&lt;/code&gt; only gets you as far as cattle (&lt;code&gt;1¢/kg&lt;/code&gt;, still in chapter 1).&lt;/p&gt;

&lt;p&gt;The Harmonized Tariff Schedule (USITC, 2026 Revision 22) has 19,926 declarable lines in chapters 1 to 97. A declarable line is one with nothing more detailed under it, so it is what actually goes on a customs entry. Between them, those lines use &lt;strong&gt;747 different rate strings&lt;/strong&gt;. Here are a few, copied as published:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6.5%
4.4¢/kg
$1.104/kg + 14.9%
1¢ each + 2.8%
5.7¢/kg on drained weight +8%
44¢ each + 6% on the case + 14% on the strap, band or bracelet + 5.3% on the battery
The rate applicable to each garment in the ensemble if separately entered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is one column. If you are building a landed cost calculator, or just want to sort products by duty, this is the column you have to deal with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four kinds of rate, and everything else
&lt;/h2&gt;

&lt;p&gt;Customs has names for the normal cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ad valorem&lt;/strong&gt;: a percentage of the customs value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specific&lt;/strong&gt;: an amount per unit, such as cents per kilogram.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compound&lt;/strong&gt;: both added together.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Free&lt;/strong&gt;: exactly that, with a caveat at the end of this post.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The parser below handles those four and refuses everything else. The refusal is the important part. A rate that was read wrongly gives you a believable number, and nobody questions a believable number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="no"&gt;NUM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\d+(?:\.\d+)?"&lt;/span&gt;
&lt;span class="no"&gt;PCT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rf&lt;/span&gt;&lt;span class="s2"&gt;"({NUM})%"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;UNIT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"kg|liter|each|pr\.|doz\.|bbl|gross|t|m2|m3|head|article"&lt;/span&gt;
&lt;span class="no"&gt;SPECIFIC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rf&lt;/span&gt;&lt;span class="s2"&gt;"(\$?)({NUM})(¢?)\s*/?\s*({UNIT})"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_rate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Split a published rate into terms; None means a person has to read it."""&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Free"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;terms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\s*\+\s*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;PCT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fullmatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pct"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;SPECIFIC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fullmatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;usd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"per"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;terms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse_rate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"needs a human"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"free"&lt;/span&gt;
    &lt;span class="n"&gt;kinds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kinds&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"pct"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"ad valorem"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"specific"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kinds&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"per"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"compound"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here. &lt;code&gt;fullmatch&lt;/code&gt; is what makes the parser refuse: &lt;code&gt;5.7¢/kg on drained weight&lt;/code&gt; starts like a specific rate, and a plain &lt;code&gt;match&lt;/code&gt; would happily accept it. The units are a whitelist for the same reason. My first version took anything after the slash as the unit, and it counted &lt;code&gt;kg on lead content&lt;/code&gt; as ordinary kilograms.&lt;/p&gt;

&lt;p&gt;Running it over the schedule takes a few lines. The data is a flat CSV I built from the official USITC export, &lt;a href="https://huggingface.co/datasets/anderslee2020/us-harmonized-tariff-schedule-hts" rel="noopener noreferrer"&gt;on Hugging Face&lt;/a&gt; under CC0:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hts.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
             &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"declarable"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"yes"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"chapter"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"98"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"99"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"general_rate"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;most_common&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Kind&lt;/th&gt;
&lt;th&gt;Lines&lt;/th&gt;
&lt;th&gt;Share&lt;/th&gt;
&lt;th&gt;Distinct strings&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ad valorem&lt;/td&gt;
&lt;td&gt;10,300&lt;/td&gt;
&lt;td&gt;51.7%&lt;/td&gt;
&lt;td&gt;201&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;td&gt;7,343&lt;/td&gt;
&lt;td&gt;36.9%&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Specific&lt;/td&gt;
&lt;td&gt;1,335&lt;/td&gt;
&lt;td&gt;6.7%&lt;/td&gt;
&lt;td&gt;209&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Needs a human&lt;/td&gt;
&lt;td&gt;536&lt;/td&gt;
&lt;td&gt;2.7%&lt;/td&gt;
&lt;td&gt;159&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compound&lt;/td&gt;
&lt;td&gt;412&lt;/td&gt;
&lt;td&gt;2.1%&lt;/td&gt;
&lt;td&gt;177&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So 88.5% of lines really are a simple percentage or zero, which is why the naive version looks fine in a demo. The other 11.5% are not spread evenly. The 1,747 specific and compound lines cluster in vegetables (chapter 7: 292 lines), dairy (chapter 4: 142), fruit and nuts (chapter 8: 121), tobacco (chapter 24: 119) and meat (chapter 2: 102). If you sell food, the awkward rates are your normal case.&lt;/p&gt;

&lt;h2&gt;
  
  
  A rate is a function, not a value
&lt;/h2&gt;

&lt;p&gt;A specific or compound rate cannot be turned into one percentage, because the answer depends on the price. Take &lt;code&gt;0402.29.50.00&lt;/code&gt;, a sweetened milk powder line, at &lt;code&gt;$1.104/kg + 14.9%&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;duty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Duty in USD for a customs value and a quantity in the rate's own unit."""&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_unit&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"pct"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse_rate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"$1.104/kg + 14.9%"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;duty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# 1402.0  -&amp;gt; 70.1% of the value&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;duty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 2594.0  -&amp;gt; 25.9% of the value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is the same line and the same 1,000 kg. The cheap shipment pays 70.1% of its value and the expensive one 25.9%. So a &lt;code&gt;duty_percent&lt;/code&gt; field in your schema is wrong by design for these lines: store the terms, and calculate when you know value and quantity.&lt;/p&gt;

&lt;p&gt;It also means your data model needs a quantity in the rate's unit, which is not always the one you have. The &lt;code&gt;units&lt;/code&gt; column uses different spellings from the rates (&lt;code&gt;No.&lt;/code&gt; where the rate says &lt;code&gt;each&lt;/code&gt;, &lt;code&gt;prs.&lt;/code&gt; for &lt;code&gt;pr.&lt;/code&gt;), it has both &lt;code&gt;m2&lt;/code&gt; and &lt;code&gt;m²&lt;/code&gt;, and 92 lines are charged per kilogram but reported in &lt;code&gt;doz., kg&lt;/code&gt;, so two quantities go on the entry.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 536 lines that need a person
&lt;/h2&gt;

&lt;p&gt;It is tempting to treat the leftovers as parsing failures that a better regex would fix. Most of them are not. They are real legal logic written as prose:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What the text does&lt;/th&gt;
&lt;th&gt;Lines&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Points at another rate&lt;/td&gt;
&lt;td&gt;269&lt;/td&gt;
&lt;td&gt;&lt;code&gt;The rate applicable to each garment in the ensemble if separately entered&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses a base other than the shipment&lt;/td&gt;
&lt;td&gt;135&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;¢/kg on lead content&lt;/code&gt;, &lt;code&gt;on drained weight&lt;/code&gt;, &lt;code&gt;on the value of the rifle&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Splits a watch into components&lt;/td&gt;
&lt;td&gt;96&lt;/td&gt;
&lt;td&gt;&lt;code&gt;44¢ each + 6% on the case + …&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slides with a measurement&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;&lt;code&gt;… less 0.020668¢/kg for each degree under 100 degrees …&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Other units and oddities&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;¢/pf.liter&lt;/code&gt;, &lt;code&gt;$1.34/1000&lt;/code&gt;, &lt;code&gt;0.3¢/line/ gross + 4.6%&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each of these needs a fact the column does not hold: the rate of each garment in a set, the lead content of the ore, the separate values of a watch case, strap and battery. Teaching a regex to read them would only produce numbers that look right. Returning &lt;code&gt;None&lt;/code&gt; and showing the raw text is the honest output.&lt;/p&gt;

&lt;p&gt;The text is not perfectly clean either, so normalize before you compare strings. Some rates have no space around the plus sign (&lt;code&gt;40¢/kg +10.4%&lt;/code&gt;, &lt;code&gt;… and case+ 5.3% on the battery&lt;/code&gt;), proof liters appear as both &lt;code&gt;pf.liter&lt;/code&gt; and &lt;code&gt;pf. liter&lt;/code&gt;, and 11 lines read &lt;code&gt;in the in the ensemble&lt;/code&gt;. That typo is in the published schedule, so an exact string match on the garment rule misses them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things this column will not tell you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Free&lt;/code&gt; does not mean zero duty.&lt;/strong&gt; This is the Column 1 General rate only. Chapter 99 adds duties by country of origin (Section 301, Section 232 and others) on top, and antidumping duties are separate again. Those are not in this column.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The right rate depends on the right code.&lt;/strong&gt; Everything above assumes the line is correct, and classification comes down to facts about the goods, not this table. That part is what I write about at &lt;a href="https://hs-code.net/" rel="noopener noreferrer"&gt;hs-code.net&lt;/a&gt;, with the CBP rulings behind each answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The numbers are for one revision.&lt;/strong&gt; USITC revises the schedule many times a year, so recalculate instead of copying a table like the ones above.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you parse this column yourself, start by counting the shapes of the strings before you write any logic. Replacing every number with &lt;code&gt;N&lt;/code&gt; takes the 747 strings down to 84 shapes, a list you can read in a few minutes.&lt;/p&gt;

</description>
      <category>python</category>
      <category>data</category>
      <category>parsing</category>
      <category>opendata</category>
    </item>
  </channel>
</rss>
