<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[ByteOrbit Dev Notes]]></title><description><![CDATA[ByteOrbit Dev Notes]]></description><link>https://byteorbit.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 29 Nov 2026 22:42:21 GMT</lastBuildDate><atom:link href="https://byteorbit.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Zod 4 required_error Removed: How to Customize Required and Invalid Type Messages]]></title><description><![CDATA[If you upgraded to Zod 4 and your "This field is required" messages turned into Invalid input: expected string, received undefined, you hit the removal of…]]></description><link>https://byteorbit.hashnode.dev/zod-4-required-error-removed-how-to-customize-required-and-invalid-type-messages</link><guid isPermaLink="true">https://byteorbit.hashnode.dev/zod-4-required-error-removed-how-to-customize-required-and-invalid-type-messages</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[zod]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[ByteOrbit]]></dc:creator><pubDate>Sun, 29 Nov 2026 07:42:51 GMT</pubDate><content:encoded><![CDATA[<p>If you upgraded to Zod 4 and your "This field is required" messages turned into <code>Invalid input: expected string, received undefined</code>, you hit the removal of <code>required_error</code>. This guide to <strong>Zod 4 required_error</strong> shows what happens at runtime, what TypeScript tells you (when you let it), and the replacements that actually work: the <code>error</code> parameter, shorthand strings, <code>.min(1)</code> for empty form fields, and a global <code>customError</code>.</p>
<p>Tested with Zod 4.6.7, TypeScript 5.9 and <code>tsx</code> on Node 24.22.1. Every output below was copied from a run of that code.</p>
<h2>What happened to Zod 4 required_error</h2>
<p>In Zod 3 you wrote this:</p>
<pre><code class="language-ts">z.string({
  required_error: 'Name is required',
  invalid_type_error: 'Name must be text',
});
</code></pre>
<p>Zod 4 dropped both parameters. The official migration guide says they "were hastily added years ago" and did not match Zod's real issue codes: there is no <code>required</code> issue code, a missing value is just an <code>invalid_type</code> issue whose input is <code>undefined</code>. They are replaced by a single <code>error</code> parameter.</p>
<p>The dangerous part is the failure mode. If something bypasses the type checker (an <code>any</code>, a JS file, a copied schema in a loosely typed codebase), Zod 4 does not throw. It ignores the keys. We forced the old params through with a cast:</p>
<pre><code class="language-text">old params, undefined -&gt; invalid_type: Invalid input: expected string, received undefined
old params, 42        -&gt; invalid_type: Invalid input: expected string, received number
</code></pre>
<p>Your custom messages vanish without any error, which is why this shows up as a UI regression instead of a build failure.</p>
<p>With strict types on, TypeScript does catch it:</p>
<pre><code class="language-text">error TS2769: No overload matches this call.
  Object literal may only specify known properties, and 'required_error' does not exist in type '$ZodStringParams'.
</code></pre>
<p>The same error appears for <code>errorMap</code>, which was also removed. A plain search of the repo for <code>required_error</code>, <code>invalid_type_error</code> and <code>errorMap</code> finds every spot.</p>
<h2>The replacement: the error parameter</h2>
<p>The unified <code>error</code> parameter takes either a string or a function. For the Zod 3 behaviour (different text for missing and wrong type), use a function and test <code>issue.input</code>:</p>
<pre><code class="language-ts">import { z } from 'zod';

const name = z.string({
  error: (issue) =&gt;
    issue.input === undefined ? 'Name is required' : 'Name must be text',
});
</code></pre>
<p>Results:</p>
<pre><code class="language-text">undefined -&gt; invalid_type: Name is required
42        -&gt; invalid_type: Name must be text
</code></pre>
<p>The function receives the issue, so you also get <code>issue.code</code>, <code>issue.path</code>, <code>issue.schema</code> and code-specific fields like <code>issue.minimum</code>. Return <code>undefined</code> to hand control to the next error source. We confirmed the fallback: a function that only handled <code>undefined</code> produced Zod's default message for <code>42</code>.</p>
<h3>Shorthand: when one message is enough</h3>
<p>Every Zod API also accepts a plain string, and the object form accepts <code>error: 'text'</code>:</p>
<pre><code class="language-ts">z.string('Name is required');
z.string({ error: 'Name is required' });
z.string().min(3, 'Too short');
</code></pre>
<p>A fixed string applies to every <code>invalid_type</code> issue on that schema, so <code>undefined</code> and <code>42</code> both gave <code>Name is required</code> in our run. That is fine for a form field, and wrong for an API where the client should know whether a value was missing or malformed.</p>
<h3>The deprecated message param still works</h3>
<p>If you have a lot of <code>.min(3, { message: '...' })</code> calls, they keep working in 4.6.7. We got <code>Too short (message)</code> back. The migration guide marks <code>message</code> as deprecated, so move to <code>error</code> when you touch the line.</p>
<h2>Form fields: empty strings are not undefined</h2>
<p>HTML forms and <code>FormData</code> send an empty string for a blank text input, not <code>undefined</code>. So the <code>input === undefined</code> trick never fires for them, and the type check passes. Add a length check with its own message:</p>
<pre><code class="language-ts">const form = z.object({
  name: z
    .string({ error: (i) =&gt; (i.input === undefined ? 'Name is required' : 'Name must be text') })
    .min(1, 'Name is required'),
});
</code></pre>
<p>We got <code>Name is required</code> for both <code>{}</code> and <code>{ name: '' }</code>. A check-level message wins over the schema-level function, so the <code>.min(1)</code> text is what a blank field shows.</p>
<p>For a schema-level function to cover a check, leave the check without its own message. In a test, <code>z.string({ error }).min(3)</code> with an <code>error</code> function that handled <code>too_small</code> returned our <code>SCHEMA-LEVEL too small</code> text for <code>'ab'</code>.</p>
<h2>The optional().required() trap</h2>
<p>This one is easy to miss. In Zod 3, <code>required_error</code> also covered <code>.required()</code>. In Zod 4, <code>.required()</code> is built on <code>.nonoptional()</code>, and a custom message on the inner string does not carry over:</p>
<pre><code class="language-ts">const opt = z.object({
  text: z.string({ error: 'Please type some text' }).optional(),
}).required();

opt.parse({});
// invalid_type: Invalid input: expected nonoptional, received undefined
</code></pre>
<p>Two fixes. Put the message on <code>.nonoptional()</code> directly (<code>z.string().optional().nonoptional('Required!')</code> gave <code>Required!</code>), or avoid the optional-then-required pattern altogether, which is what the Zod author suggested when this came up in the issue tracker (colinhacks/zod 4982): start from a schema with required fields, then use <code>pick</code> and <code>partial</code> to relax the ones you need. The mask form, <code>.required({ a: true })</code>, also returned the default <code>expected nonoptional</code> text in our run.</p>
<h2>Where messages come from: precedence</h2>
<p>We tested each level against the others. The more specific one always won:</p>
<table>
<thead>
<tr>
<th>Level</th>
<th>Example</th>
<th>Result in our test</th>
</tr>
</thead>
<tbody><tr>
<td>Check</td>
<td><code>.min(3, 'Check msg')</code></td>
<td>beats the schema-level function</td>
</tr>
<tr>
<td>Schema</td>
<td><code>z.string('Schema msg')</code></td>
<td>beats per-parse and global</td>
</tr>
<tr>
<td>Per-parse</td>
<td><code>schema.parse(x, { error: () =&gt; 'Per-parse' })</code></td>
<td>beats global and the default</td>
</tr>
<tr>
<td>Global</td>
<td><code>z.config({ customError })</code></td>
<td>beats the default locale text</td>
</tr>
<tr>
<td>Default</td>
<td>Zod's locale</td>
<td><code>Invalid input: expected string, received undefined</code></td>
</tr>
</tbody></table>
<p>Global is the right place for a project-wide "This field is required" rule:</p>
<pre><code class="language-ts">z.config({
  customError: (iss) =&gt;
    iss.code === 'invalid_type' &amp;&amp; iss.input === undefined
      ? 'This field is required'
      : undefined,
});
</code></pre>
<p>With that in place, <code>z.object({ a: z.string(), b: z.number() }).parse({})</code> returned <code>This field is required</code> for both fields. Two details from testing: <code>z.config</code> merges into existing config rather than replacing it, so calling <code>z.config(z.locales.en())</code> afterwards did not remove our <code>customError</code>, and you must set <code>customError: undefined</code> explicitly to reset it. That matters in test suites that share one process.</p>
<h2>Enums, numbers and coerced fields</h2>
<p>The same <code>error</code> pattern works on other schema types. These are the outputs we got, since the defaults are different from strings:</p>
<table>
<thead>
<tr>
<th>Schema</th>
<th>Input</th>
<th>Message</th>
</tr>
</thead>
<tbody><tr>
<td><code>z.enum(['free', 'pro'])</code></td>
<td><code>'gold'</code></td>
<td>Zod's default <code>Invalid option: expected one of ...</code> text (code <code>invalid_value</code>)</td>
</tr>
<tr>
<td><code>z.enum(['free', 'pro'], 'Pick free or pro')</code></td>
<td><code>'gold'</code></td>
<td><code>Pick free or pro</code></td>
</tr>
<tr>
<td><code>z.enum([...], { error: fn })</code> with the <code>input === undefined</code> check</td>
<td><code>undefined</code></td>
<td><code>Choose a plan</code></td>
</tr>
<tr>
<td><code>z.number({ error: fn })</code> with the same check</td>
<td><code>undefined</code></td>
<td><code>Age is required</code></td>
</tr>
<tr>
<td><code>z.number().min(18, { error: (i) =&gt; ... })</code></td>
<td><code>5</code></td>
<td><code>Need at least 18</code> (uses <code>issue.minimum</code>)</td>
</tr>
<tr>
<td><code>z.email('Enter a valid email')</code></td>
<td><code>'nope'</code></td>
<td><code>Enter a valid email</code> (code <code>invalid_format</code>)</td>
</tr>
</tbody></table>
<p>Note that a missing enum value is an <code>invalid_value</code> issue, not <code>invalid_type</code>, so a global <code>customError</code> that only looks at <code>invalid_type</code> will not catch it. Check <code>iss.input === undefined</code> without filtering on the code if you want one rule for every missing field.</p>
<p>One more form trap: <code>z.coerce.number()</code> turns an empty string into <code>0</code>, because it runs <code>Number('')</code> first. In our run <code>z.coerce.number().parse('')</code> returned <code>0</code> with no error, while <code>'abc'</code> and <code>undefined</code> both produced <code>Enter a number</code>. For a numeric text input, check for the empty string yourself (for example with <code>z.string().min(1)</code> followed by a transform) so a blank field is not silently saved as zero.</p>
<h2>Showing errors in a form</h2>
<p>Parse errors are good raw material for field messages. Use <code>safeParse</code> and the helpers that ship with Zod 4:</p>
<pre><code class="language-ts">const r = form.safeParse({ name: '' });
if (!r.success) {
  const { fieldErrors } = z.flattenError(r.error);
  // { name: ["Name is required"] }
}
</code></pre>
<p><code>z.flattenError</code> returned <code>{"formErrors":[],"fieldErrors":{"name":["Name is required"]}}</code>, which maps straight onto the state you return from a React action. <code>z.treeifyError</code> gives a nested structure for deep objects, and <code>z.prettifyError</code> prints a multi-line string with arrows for logs:</p>
<pre><code class="language-text">x Enter a valid email
  -&gt; at email
</code></pre>
<p>(The real output uses the Unicode cross and arrow characters.) If you are wiring this into a React 19 form, our <a href="https://byteorbit.tech/blog/react-19-useactionstate-typescript">useActionState TypeScript guide</a> shows the state shape to return, and the <a href="https://byteorbit.tech/blog/type-safe-route-handlers-zod">type-safe route handlers post</a> covers the server side of the same schemas. If a form component re-validates inside an Effect, <a href="https://byteorbit.tech/blog/react-useeffectevent">React useEffectEvent</a> keeps the latest messages available without restarting the Effect.</p>
<h2>Migration checklist</h2>
<ol>
<li>Search for <code>required_error</code>, <code>invalid_type_error</code>, <code>errorMap</code> and <code>message:</code> in your schemas.</li>
<li>Replace the first two with an <code>error</code> function that checks <code>issue.input === undefined</code>.</li>
<li>Replace <code>errorMap</code> with <code>error</code>. It can now return a plain string or <code>undefined</code>.</li>
<li>Add <code>.min(1, ...)</code> to text fields that come from <code>FormData</code>, and do not rely on <code>z.coerce.number()</code> for blank inputs.</li>
<li>Replace <code>.optional().required()</code> with <code>.nonoptional(msg)</code> or a <code>pick</code> plus <code>partial</code> structure.</li>
<li>Decide whether a global <code>customError</code> should carry your "required" text.</li>
<li>Run <code>tsc</code> with strict types on. It reports removed keys; runtime does not.</li>
</ol>
<h2>FAQ</h2>
<h3>What replaced required_error in Zod 4?</h3>
<p>The <code>error</code> parameter. For separate missing and wrong-type messages, pass a function and compare <code>issue.input</code> to <code>undefined</code>. For a single text, pass a string: <code>z.string('Name is required')</code>.</p>
<h3>Does Zod 4 still support invalid_type_error?</h3>
<p>No. It was removed along with <code>required_error</code>. At runtime the keys are silently ignored if types are bypassed, and TypeScript reports an unknown property when types are checked.</p>
<h3>Why does my custom required message not show up for empty inputs?</h3>
<p>A blank text input sends an empty string, which is a valid string. Add <code>.min(1, 'Required')</code> so the empty value fails with your message.</p>
<h3>Why do I get "expected nonoptional, received undefined"?</h3>
<p>You called <code>.required()</code> on a schema built with <code>.optional()</code>. Zod 4 implements it with <code>.nonoptional()</code>, so the inner message is lost. Pass the message to <code>.nonoptional()</code> or restructure with <code>pick</code> and <code>partial</code>.</p>
<h3>Is the message parameter deprecated in Zod 4?</h3>
<p>Yes, but it still works in Zod 4.6.7. Use <code>error</code> for new code.</p>
<hr />
<p>Official docs: <a href="https://zod.dev/v4/changelog">Zod 4 migration guide</a> and <a href="https://zod.dev/error-customization">Customizing errors</a>.</p>
<p>Originally published on <a href="https://byteorbit.tech/blog/zod-4-required-error">ByteOrbit</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Python 3.14 Free Threaded: Install, Benchmark and the Packages That Re-enable the GIL]]></title><description><![CDATA[Python 3.14 free threaded tested on 8 cores: install with uv, check the GIL, a 4-thread benchmark, a race condition, packages that re-enable the GIL…]]></description><link>https://byteorbit.hashnode.dev/python-3-14-free-threaded-install-benchmark-and-the-packages-that-re-enable-the-gil</link><guid isPermaLink="true">https://byteorbit.hashnode.dev/python-3-14-free-threaded-install-benchmark-and-the-packages-that-re-enable-the-gil</guid><category><![CDATA[Python]]></category><category><![CDATA[Docker]]></category><category><![CDATA[performance]]></category><category><![CDATA[GIL]]></category><dc:creator><![CDATA[ByteOrbit]]></dc:creator><pubDate>Sat, 28 Nov 2026 08:33:18 GMT</pubDate><content:encoded><![CDATA[<p>Python 3.14 is the first release where the free-threaded build is officially supported, not experimental. This guide to <strong>Python 3.14 free threaded</strong> shows how to install it with uv, how to check that the GIL is really off, what scaling we measured on 8 cores, a race condition that only appears without the GIL, and which common packages still switch it back on.</p>
<p>Tested with CPython 3.14.10 (both the default and the <code>3.14t</code> free-threaded builds) installed through uv 0.13.9, on an 8-core Linux x86-64 machine. The commands and outputs below are from that machine. We could not build a Docker image there, so the container section is based on the same uv commands plus the official image tag list.</p>
<h2>What "free threaded" means in Python 3.14</h2>
<p>A normal CPython build has a global interpreter lock (GIL): only one thread runs Python bytecode at a time, so CPU-bound threads do not speed up. The free-threaded build removes that lock. Threads run in parallel on separate cores, at the cost of a different ABI, extra memory use and a small single-thread slowdown.</p>
<p>The official Python docs describe it plainly: free-threaded execution allows full use of available processing power, "while not all software will benefit from this automatically", and some third-party packages with extension modules "will re-enable the GIL". On the pyperformance suite the docs put the average single-threaded overhead between about 1% on macOS aarch64 and 8% on x86-64 Linux.</p>
<p>It is a separate interpreter, not a flag on the normal one. The binary is called <code>python3.14t</code>. A wheel built for the normal interpreter (<code>cp314</code>) will not load there; it needs a <code>cp314t</code> build.</p>
<h2>Install Python 3.14 free threaded with uv</h2>
<p>uv knows the <code>t</code> suffix:</p>
<pre><code class="language-bash">uv python install 3.14t
uv run --python 3.14t python -VV
uv venv --python 3.14t .venv
</code></pre>
<p><code>python -VV</code> prints "free-threading build" in the version string. To check the GIL state in a running process, use the function the docs recommend:</p>
<pre><code class="language-python">import sys, sysconfig
print(sysconfig.get_config_var("Py_GIL_DISABLED"))  # 1 on a free-threaded build
print(sys._is_gil_enabled())                         # False when the GIL is off
</code></pre>
<p>The two answers differ on purpose. The config variable tells you what the build can do. <code>sys._is_gil_enabled()</code> tells you what the process is doing right now. On our box:</p>
<pre><code class="language-text">3.14.10  Py_GIL_DISABLED = 0  gil enabled = True     (default build)
3.14.10  Py_GIL_DISABLED = 1  gil enabled = False    (3.14t)
3.14.10  Py_GIL_DISABLED = 1  gil enabled = True     (3.14t with PYTHON_GIL=1)
</code></pre>
<p>You can force the lock back on with the <code>PYTHON_GIL=1</code> environment variable or <code>-X gil=1</code>, and off with <code>0</code>. We confirmed <code>python -X gil=1</code> reports the GIL as enabled.</p>
<h2>Python 3.14 free threaded benchmark</h2>
<p>The test is deliberately simple: a recursive <code>fib(32)</code> in N threads, with N from 1 to 4, timed with <code>time.perf_counter()</code>.</p>
<pre><code class="language-python">import threading, time

def fib(n):
    return n if n &lt; 2 else fib(n - 1) + fib(n - 2)

def run(nthreads, n=32):
    ts = [threading.Thread(target=fib, args=(n,)) for _ in range(nthreads)]
    start = time.perf_counter()
    for t in ts: t.start()
    for t in ts: t.join()
    return time.perf_counter() - start
</code></pre>
<p>Results (the free-threaded numbers are from two runs, which agreed within noise):</p>
<table>
<thead>
<tr>
<th>Build</th>
<th>1 thread</th>
<th>2 threads</th>
<th>4 threads</th>
<th>Throughput vs serial at 4 threads</th>
</tr>
</thead>
<tbody><tr>
<td>3.14 (GIL)</td>
<td>0.15 s</td>
<td>0.30 s</td>
<td>0.60 s</td>
<td>1.0x</td>
</tr>
<tr>
<td>3.14t (GIL off)</td>
<td>0.15 s</td>
<td>0.15 s</td>
<td>0.15 to 0.16 s</td>
<td>3.6x to 3.9x</td>
</tr>
<tr>
<td>3.14t, <code>PYTHON_GIL=1</code></td>
<td>0.15 s</td>
<td>0.30 s</td>
<td>0.62 s</td>
<td>1.0x</td>
</tr>
</tbody></table>
<p>Four threads on the normal build take four times as long as one, because they queue on the lock. On 3.14t they all finish in about the time of one. Setting <code>PYTHON_GIL=1</code> on the free-threaded interpreter brought the old behaviour straight back, which is a handy way to A/B test your own code.</p>
<p>Single-thread speed was the same to two decimal places in this one benchmark. Do not read that as "no overhead": the docs' 1% to 8% average is across many workloads, and a different benchmark will show a gap.</p>
<h3>Not everything scales</h3>
<p>We also ran a thread pool hashing 8 buffers with <code>hashlib.sha256</code> (300,000 rounds each, 64-byte inputs):</p>
<pre><code class="language-text">3.14   1 / 4 / 8 workers: 0.56 s / 0.57 s / 0.54 s
3.14t  1 / 4 / 8 workers: 0.62 s / 0.84 s / 0.87 s
</code></pre>
<p>Neither build scaled, and the free-threaded one was slower with more workers. We did not dig into why, and we are not claiming a cause. The lesson is practical: measure the real workload. A pure-Python loop scaled almost linearly, a workload dominated by one C library call did not.</p>
<h2>The race condition you meet only without the GIL</h2>
<p>On the normal build, <code>count += 1</code> from several threads usually looks fine, because a thread rarely gets switched out in the middle of the operation. Without the GIL it is a real race. We ran four threads that each did two million increments of a shared global:</p>
<pre><code class="language-text">3.14   gil_enabled=True   expected 8000000, got 8000000
3.14t  gil_enabled=False  expected 8000000, got 2238627, lost 5761373
3.14t  gil_enabled=False  expected 8000000, got 2248157, lost 5751843
</code></pre>
<p>The same code with <code>threading.Lock</code> around the increment returned the exact total on both builds (800,000 of 800,000 with fewer iterations). The Python docs say the same thing in softer words: built-in types such as <code>dict</code>, <code>list</code> and <code>set</code> have internal locks, but you should use <code>threading.Lock</code> or other synchronization primitives "instead of relying on the internal locks of built-in types, when possible."</p>
<p>If your code only worked because the GIL hid a race, free threading will expose it. Run your tests on 3.14t before you ship it.</p>
<h2>Which packages re-enable the GIL</h2>
<p>Import a C extension that has not declared free-threading support and Python turns the GIL back on for the whole process, with a warning. We built a tiny extension without the <code>Py_mod_gil</code> slot to see exactly what happens:</p>
<pre><code class="language-text">RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'legacyext',
which has not declared that it can run safely without the GIL. To override this behavior and keep
the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.
before: False
after okext: False        (module that declares Py_MOD_GIL_NOT_USED)
after legacyext: True
</code></pre>
<p>Setting <code>PYTHON_GIL=0</code> kept it off after the same import, which can crash if the module is really not thread-safe, so treat it as a debugging tool and not a fix.</p>
<p>For real packages, we created a 3.14t venv and installed with <code>--only-binary :all:</code> so uv would not compile anything:</p>
<table>
<thead>
<tr>
<th>Package</th>
<th>Result on 3.14t today</th>
</tr>
</thead>
<tbody><tr>
<td>numpy 2.5.5</td>
<td>installs, GIL stays off after import</td>
</tr>
<tr>
<td>pydantic 2.14.4 (pydantic-core 2.50.5)</td>
<td>installs, GIL stays off</td>
</tr>
<tr>
<td>fastapi 0.165.0, uvicorn 0.54.0</td>
<td>install, GIL stays off</td>
</tr>
<tr>
<td>httptools 0.9.0, uvloop 0.23.0</td>
<td>install, GIL stays off</td>
</tr>
<tr>
<td>orjson</td>
<td>no binary wheel found; uv could not resolve one, and a source build failed on our box because it needs a Rust toolchain</td>
</tr>
<tr>
<td>psycopg[binary]</td>
<td>no binary wheel found</td>
</tr>
</tbody></table>
<p>That is a snapshot from 28 November 2026 on Linux x86-64, not a promise. Two trackers keep the live picture: <a href="https://py-free-threading.github.io/tracking/">py-free-threading.github.io/tracking</a> and <a href="https://hugovk.github.io/free-threaded-wheels/">hugovk.github.io/free-threaded-wheels</a>. The most useful check is the cheapest one: import every dependency in a fresh 3.14t venv and print <code>sys._is_gil_enabled()</code> afterwards.</p>
<h2>Docker and Python 3.14 free threaded</h2>
<p>There is no official free-threaded tag in the Docker library. The official Python image lists <code>3.14-slim-trixie</code>, <code>3.14-trixie</code>, <code>3.14-alpine</code> and the other normal variants, but no <code>3.14t</code>. A request for 3.13t and 3.14t images is still open as docker-library/python issue 1082. So <code>FROM python:3.14-slim</code> gives you the GIL build, and setting <code>PYTHON_GIL=0</code> on it does not help: the default interpreter aborts at startup with <code>Fatal Python error: config_read_gil: Disabling the GIL is not supported by this build</code>, which is what we got on 3.14.10.</p>
<p>Instead, install the interpreter with uv in your own image. This is the same flow as our <a href="https://byteorbit.tech/blog/fastapi-dockerfile-uv">FastAPI Dockerfile with uv</a>:</p>
<pre><code class="language-dockerfile">FROM debian:trixie-slim
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
ENV UV_PYTHON_INSTALL_DIR=/opt/python
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --python 3.14t
COPY . .
CMD ["uv", "run", "--no-sync", "--python", "3.14t", "python", "-m", "app"]
</code></pre>
<p>We did not build this exact image because the box has no Docker daemon access, but we ran the same <code>uv lock</code>, <code>uv sync --frozen --no-dev --python 3.14t</code> and <code>uv run --no-sync --python 3.14t python -m app</code> steps on a small project that depends on fastapi and uvicorn, and the app printed <code>gil enabled: False</code>. Check your own build once. For a workspace with several packages, the <a href="https://byteorbit.tech/blog/uv-workspace">uv workspace guide</a> shows how to sync one member. A startup check is worth adding to the app itself:</p>
<pre><code class="language-python">import sys
assert not sys._is_gil_enabled(), "GIL got re-enabled by an import"
</code></pre>
<p>Put it after your imports in the entry point, so a dependency that silently re-enables the lock fails the deploy instead of the benchmark. In a FastAPI app the <a href="https://byteorbit.tech/blog/fastapi-lifespan">lifespan hook</a> is a natural place for it.</p>
<h2>When free threading is worth it</h2>
<p>Good fits: CPU-bound pure-Python work you currently split across processes (shared memory without pickling), thread pools doing parsing or transformation, and numerical code that calls into already-supported libraries from many threads.</p>
<p>Poor fits: I/O-bound services. Threads waiting on a network call already release the GIL, so <code>asyncio</code> or normal threads were fine before. If your app mostly calls a model endpoint, as in our <a href="https://byteorbit.tech/blog/docker-compose-models">Docker Compose models guide</a>, free threading will not make those calls faster. It also changes memory use: the docs warn that all interned strings become immortal, <code>None</code> takes 32 bytes instead of 16 on x86-64, and freed memory can be returned later because of deferred reclamation.</p>
<p>A sensible rollout: run your test suite on 3.14t in CI next to the normal build, add the GIL assertion, fix the locks it exposes, and only then benchmark production-shaped load.</p>
<h2>FAQ</h2>
<h3>How do I install Python 3.14 free threaded?</h3>
<p>With uv: <code>uv python install 3.14t</code>, then <code>uv run --python 3.14t python -VV</code> or <code>uv venv --python 3.14t</code>. On macOS and Windows the official installers have an optional free-threaded build, and the binary is named <code>python3.14t</code>.</p>
<h3>How do I check whether the GIL is disabled?</h3>
<p>Call <code>sys._is_gil_enabled()</code>. It returns <code>False</code> when the GIL is off. <code>sysconfig.get_config_var("Py_GIL_DISABLED")</code> only tells you the build supports it.</p>
<h3>Why did the GIL turn back on after an import?</h3>
<p>A C extension did not declare that it can run without the GIL, so Python enabled the lock for the whole process and printed a RuntimeWarning. Update the package, find a free-threaded wheel, or avoid it. <code>PYTHON_GIL=0</code> forces it off but is unsafe for modules that are not thread-safe.</p>
<h3>Is there a free-threaded official Docker image?</h3>
<p>Not at the time of writing. The official Python image has no <code>3.14t</code> tag and the request is still open. Install <code>3.14t</code> with uv in your own image.</p>
<h3>Is free-threaded Python faster?</h3>
<p>For CPU-bound threaded code, yes: our 4-thread test finished in the time of one on 3.14t and four times as long on the normal build. Single-threaded code can be slightly slower, and some workloads, like our hashing test, did not scale at all.</p>
<hr />
<p>Official docs: <a href="https://docs.python.org/3.14/howto/free-threading-python.html">Python support for free threading</a>.</p>
<p>Originally published on <a href="https://byteorbit.tech/blog/python-314-free-threaded">ByteOrbit</a>.</p>
]]></content:encoded></item></channel></rss>