<?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: theyplaygames</title>
    <description>The latest articles on DEV Community by theyplaygames (@theyplaygames).</description>
    <link>https://dev.to/theyplaygames</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%2F4219065%2F93142777-b58a-499b-9327-7b4d834f17d5.png</url>
      <title>DEV Community: theyplaygames</title>
      <link>https://dev.to/theyplaygames</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/theyplaygames"/>
    <language>en</language>
    <item>
      <title>A 2.6B model on an iPhone that always answers in valid JSON</title>
      <dc:creator>theyplaygames</dc:creator>
      <pubDate>Sat, 31 Oct 2026 13:26:22 +0000</pubDate>
      <link>https://dev.to/theyplaygames/a-26b-model-on-an-iphone-that-always-answers-in-valid-json-34ek</link>
      <guid>https://dev.to/theyplaygames/a-26b-model-on-an-iphone-that-always-answers-in-valid-json-34ek</guid>
      <description>&lt;p&gt;Small language models are good at one thing that apps need all the time: &lt;em&gt;read this text and fill these fields&lt;/em&gt;. A letter becomes sender, deadline and next steps; a voice note becomes a task list; a receipt becomes an expense entry. This post shows how we run that fully on an iPhone with llama.cpp and LFM2-2.6B, what it costs in seconds, and the five problems we had to solve on the way.&lt;/p&gt;

&lt;p&gt;Everything here runs offline on an &lt;strong&gt;iPhone 15&lt;/strong&gt; (A16, 6 GB RAM). That phone has no Apple Intelligence, which is exactly the case a local model is for.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Model:&lt;/strong&gt; &lt;a href="https://huggingface.co/LiquidAI/LFM2-2.6B-GGUF" rel="noopener noreferrer"&gt;LiquidAI/LFM2-2.6B-GGUF&lt;/a&gt;, Q4_K_M, 1.6 GB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime:&lt;/strong&gt; llama.cpp b11887 as an xcframework, Metal for all layers, &lt;code&gt;n_ctx = 4096&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sampling:&lt;/strong&gt; greedy, constrained by a GBNF grammar&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inputs:&lt;/strong&gt; Apple Vision for OCR, Apple's on-device speech recognition for audio&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Measured on the phone:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First model load&lt;/td&gt;
&lt;td&gt;~20 s (then it stays in memory)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Short text → 6-field JSON&lt;/td&gt;
&lt;td&gt;4–6 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Voice note → nested task list&lt;/td&gt;
&lt;td&gt;7–9 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scanned bill (~1,500 chars) → 7 fields&lt;/td&gt;
&lt;td&gt;10–14 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OCR of a page (Vision)&lt;/td&gt;
&lt;td&gt;~1 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  1. Make invalid JSON impossible
&lt;/h2&gt;

&lt;p&gt;Asking a 2B model to "answer in JSON" works most of the time. Every few hundred calls a bracket goes missing or a key is invented, and your parser throws. A &lt;strong&gt;GBNF grammar&lt;/strong&gt; fixes this at the sampling step: tokens that would break the shape are never allowed. Writing grammars by hand is tedious, so we describe the shape in Swift and generate it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Letter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sender"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Who sent it, short"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;"none"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"pay"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"reply"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sign"&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
    &lt;span class="kt"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"deadline"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Only if the letter names one"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"steps"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;max&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;grammar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gbnf&lt;/span&gt;   &lt;span class="c1"&gt;// pass to llama_sampler_init_grammar(vocab, grammar, "root")&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keys come out in the declared order, which also helps the model: put the fields it should "think about" first and the conclusion last. The generator is MIT licensed: &lt;a href="https://github.com/theyplay-games/swift-gbnf" rel="noopener noreferrer"&gt;swift-gbnf&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Long prompts abort the app
&lt;/h2&gt;

&lt;p&gt;Short tests worked, a real letter killed the app with &lt;code&gt;GGML_ASSERT(n_tokens_all &amp;lt;= cparams.n_batch)&lt;/code&gt;. The default batch is 512 tokens and we decoded the whole prompt in one call. Setting &lt;code&gt;n_batch = n_ctx&lt;/code&gt; (and leaving &lt;code&gt;n_ubatch&lt;/code&gt; at 512) fixed it without noticeable memory cost. Also check &lt;code&gt;prompt tokens + max new tokens &amp;lt;= n_ctx&lt;/code&gt; before decoding, and fail gracefully if it doesn't fit.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Don't let the model do numbers
&lt;/h2&gt;

&lt;p&gt;LFM2 reads a bill well, but on a Japanese electricity bill it picked a line item (8,120 yen) instead of the total (10,056 yen). We stopped asking the model for numbers we can read with rules: the amount next to &lt;em&gt;Total / Summe / ご請求金額 / 合計&lt;/em&gt;, dates, IBANs. The model writes the summary and the steps, the rules provide the facts, and the facts override the model.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Give it a calendar
&lt;/h2&gt;

&lt;p&gt;"Your check-up is on Tuesday" turned into random dates. Small models can't count weekdays. Adding one line to the prompt fixed it almost completely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Today is Friday 2026-10-30. Next days: Saturday 2026-10-31, Sunday 2026-11-01, Monday 2026-11-02, Tuesday 2026-11-03, ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Language: say it twice
&lt;/h2&gt;

&lt;p&gt;Given a German receipt and the instruction "answer in English" in the system prompt, the model often answered in German. Repeating the wish right after the input text, &lt;code&gt;(Write every text value in English.)&lt;/code&gt;, made it reliable. Reading a Japanese letter and explaining it in English works well once that's in place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: the Simulator
&lt;/h2&gt;

&lt;p&gt;The official llama.cpp xcframework has no iOS Simulator slice, so apps that link it don't build for the Simulator. We built the same release with llama.cpp's own &lt;code&gt;build-xcframework.sh&lt;/code&gt; and published it unchanged: &lt;a href="https://github.com/theyplay-games/llama-xcframework" rel="noopener noreferrer"&gt;llama-xcframework&lt;/a&gt;. In the Simulator, load with &lt;code&gt;n_gpu_layers = 0&lt;/code&gt;: its Metal lacks most kernels and is slower than the CPU.&lt;/p&gt;

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

&lt;p&gt;A 2.6B model on a phone is a very good &lt;em&gt;reader&lt;/em&gt;: summaries, classification, extraction, in many languages, private by design. Pair it with a grammar for the shape and plain rules for the numbers, and it behaves like a reliable component instead of a demo.&lt;/p&gt;

&lt;p&gt;Screen recordings of all of this on the iPhone: &lt;a href="https://theyplay.games/ondevice/#real" rel="noopener noreferrer"&gt;theyplay.games/ondevice&lt;/a&gt;. We packaged our full setup (Apple Intelligence first, the local model as fallback, background model download, OCR and speech helpers) as a Swift package there, but everything in this post works on its own.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>llm</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
