<?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: EZENWA CHRISTIAN CHIMEREMEZE</title>
    <description>The latest articles on DEV Community by EZENWA CHRISTIAN CHIMEREMEZE (@ezenwa_christian_5243b406).</description>
    <link>https://dev.to/ezenwa_christian_5243b406</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%2F4223168%2Fb8087e52-7962-495c-8913-f1f4cefec2f6.jpeg</url>
      <title>DEV Community: EZENWA CHRISTIAN CHIMEREMEZE</title>
      <link>https://dev.to/ezenwa_christian_5243b406</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ezenwa_christian_5243b406"/>
    <language>en</language>
    <item>
      <title>Building Geminio OS: What I Learned When AI Became Part of My Development Process</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Wed, 25 Nov 2026 23:27:30 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/building-geminio-os-what-i-learned-when-ai-became-part-of-my-development-process-37b4</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/building-geminio-os-what-i-learned-when-ai-became-part-of-my-development-process-37b4</guid>
      <description>&lt;p&gt;&lt;strong&gt;What were you building, and why?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My journey into software development became serious on April 1, 2023. I started with Python, driven by an interest in software engineering, artificial intelligence, automation, and eventually robotics.&lt;/p&gt;

&lt;p&gt;As I learned more about technology, I became interested in a bigger question: What if AI could become more than a tool for generating code? What if it could become part of an entire software development ecosystem?&lt;/p&gt;

&lt;p&gt;That idea led me to start building Geminio OS, an AI-powered full-stack development ecosystem.&lt;/p&gt;

&lt;p&gt;My vision was to create an environment where developers could describe an idea, define requirements, generate and modify code, analyse implementations, debug errors, run tests, and prepare projects for deployment.&lt;/p&gt;

&lt;p&gt;I wanted to explore how AI could support different stages of software development rather than simply generate isolated code snippets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What did you expect to happen?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Initially, I expected AI-assisted development to make implementation significantly faster.&lt;/p&gt;

&lt;p&gt;I imagined describing a feature, providing the necessary context, and allowing an AI coding assistant to help implement it. I expected the generated code to fit into the existing architecture with relatively little additional work.&lt;/p&gt;

&lt;p&gt;I also expected that dividing a large project into smaller development stages would make the process straightforward.&lt;/p&gt;

&lt;p&gt;However, building a complex application introduced challenges that were not immediately obvious. Generating code was only one part of the process. Making sure that the code worked correctly with everything already implemented was another challenge entirely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The moment you realized something was off&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of my biggest realisations was that AI could generate an implementation that looked complete while still missing important context about the existing project.&lt;/p&gt;

&lt;p&gt;In a growing application, individual features are connected. Authentication can affect routing. Database changes can affect API operations. Frontend components depend on backend behaviour. A modification to one area can introduce problems somewhere else.&lt;/p&gt;

&lt;p&gt;I realised that a feature could appear successful in isolation without necessarily being correctly integrated into the wider application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This changed how I approached development&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I stopped thinking only about what AI could generate and started paying much more attention to what needed to be preserved, tested, reviewed, and verified.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Everything you tried (including what didn't work)&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I began organising Geminio OS development into structured sessions, with clearly defined requirements and responsibilities for each stage.&lt;/p&gt;

&lt;p&gt;Instead of treating the application as one enormous implementation task, I separated major capabilities and approached them incrementally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I became more deliberate about:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Defining project requirements before implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preserving existing functionality when introducing new features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Maintaining a consistent application architecture.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Designing database structures and backend integrations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reviewing authentication and security-related functionality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testing API behaviour and frontend integration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checking that new functionality worked with previously implemented features.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the database architecture, I chose Supabase and PostgreSQL, with Drizzle ORM as part of the development stack.&lt;/p&gt;

&lt;p&gt;The project has also involved authentication, session management, multi-factor authentication, persistent memory, workspace functionality, and AI integration.&lt;/p&gt;

&lt;p&gt;One important lesson was that simply asking an AI assistant to regenerate or rewrite an implementation was not always the right solution. Replacing existing work could introduce new problems instead of solving the original ones.&lt;/p&gt;

&lt;p&gt;I learned to be more specific about the intended changes, the existing implementation, and the expected results.&lt;/p&gt;

&lt;p&gt;Testing and verification became essential rather than optional steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who did you ask for help?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI assistants became an important part of my development and learning process.&lt;/p&gt;

&lt;p&gt;I used them to explore technical approaches, reason about architecture, investigate implementation problems, understand unfamiliar concepts, and work through development tasks.&lt;/p&gt;

&lt;p&gt;However, I also learned not to treat AI-generated answers as automatically correct.&lt;/p&gt;

&lt;p&gt;I needed to evaluate suggestions, understand the code being generated, and verify whether an implementation actually met the requirements.&lt;/p&gt;

&lt;p&gt;This experience taught me that using AI effectively requires technical judgement. The developer must remain responsible for understanding and evaluating the final result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where things stand now&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Geminio OS remains an ongoing development project&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Its architecture and capabilities have grown beyond my original idea. The project has progressed through structured development stages involving authentication, database integration, AI functionality, persistent memory, workspace features, and other application capabilities.&lt;/p&gt;

&lt;p&gt;I have become much more conscious of software quality, maintainability, security, integration, and testing.&lt;/p&gt;

&lt;p&gt;I am also learning to approach AI-assisted development as an engineering discipline rather than simply a faster way to write code.&lt;/p&gt;

&lt;p&gt;There is still more work to do, but the experience has fundamentally changed how I plan and approach complex software projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anything else you want our guests to weigh in on?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I would particularly like to hear how experienced developers and AI engineering teams approach context management when working with AI coding agents on large, existing codebases.&lt;/p&gt;

&lt;p&gt;How do they prevent AI-generated changes from breaking previously implemented functionality?&lt;/p&gt;

&lt;p&gt;What practices help maintain architectural consistency across multiple development sessions?&lt;/p&gt;

&lt;p&gt;How do engineering teams decide when to trust generated code, when to request changes, and when to reject an implementation entirely?&lt;/p&gt;

&lt;p&gt;I am also interested in how developers can build reliable AI development environments where code generation, debugging, testing, architecture, and deployment work together without sacrificing software quality.&lt;/p&gt;

&lt;p&gt;My biggest takeaway from building Geminio OS is that AI can accelerate development, but acceleration alone does not guarantee quality.&lt;/p&gt;

&lt;p&gt;The developer still needs to understand what is being built, why it is being built, and how to verify that it genuinely works.&lt;/p&gt;

&lt;p&gt;Sometimes the most important part of building with AI is not getting the code generated. It is knowing how to evaluate it, integrate it, test it, and make sure it genuinely works.&lt;/p&gt;

&lt;p&gt;That is the experience I would like to share with the CodeNewbie community.&lt;/p&gt;

</description>
      <category>codenewbiestory</category>
      <category>codenewbie</category>
      <category>ai</category>
      <category>beginners</category>
    </item>
    <item>
      <title>API Testing with FastAPI and Pytest: A Practical Guide for Beginners</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Tue, 24 Nov 2026 00:10:39 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/api-testing-with-fastapi-and-pytest-a-practical-guide-for-beginners-kc3</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/api-testing-with-fastapi-and-pytest-a-practical-guide-for-beginners-kc3</guid>
      <description>&lt;p&gt;&lt;em&gt;API Testing with FastAPI and Pytest: A Practical Guide for Beginners&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkkyxgs76d2qhxh1l6rr9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkkyxgs76d2qhxh1l6rr9.png" alt=" " width="800" height="835"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;By Ezenwa Christian Chimeremeze&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Introduction to API Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let me tell you how I used to test my APIs.&lt;/p&gt;

&lt;p&gt;I would start the server, open my browser, go to &lt;code&gt;/docs&lt;/code&gt;, click "Try it out", type something, and see if it worked. If it returned 200, I would say "it works" and move on.&lt;/p&gt;

&lt;p&gt;Then one day I changed one small thing in my code, and something else broke quietly. No error on my screen. No crash. Just wrong data coming back. My users noticed before I did.&lt;/p&gt;

&lt;p&gt;That is when I learnt this: manual testing is not testing. It is checking.&lt;/p&gt;

&lt;p&gt;API testing means you write code that calls your API for you, checks the response, and tells you immediately if something is wrong. You do not have to remember what to test. The test remembers for you.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will test a FastAPI app with Pytest. No big theory. Just practical tests you can run again and again.&lt;/p&gt;

&lt;p&gt;If you followed my last article on Airbyte, PostgreSQL and FastAPI, this is the next step. Building the API is one job. Proving it works is another job.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What is FastAPI?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FastAPI is a Python framework for building APIs fast. And when I say fast, I mean two things: fast to write, and fast to run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I like about it:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You write normal Python functions, and FastAPI turns them into endpoints.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It validates your input automatically using Pydantic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It gives you free interactive docs at &lt;code&gt;/docs&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is built on Starlette and Pydantic, so it is solid underneath.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a small example we will test in this article. Create &lt;code&gt;main.py&lt;/code&gt;:&lt;br&gt;
from fastapi import FastAPI, HTTPException&lt;br&gt;
from pydantic import BaseModel&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Beginner Testing API"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;
    &lt;span class="n"&gt;in_stock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;

&lt;span class="n"&gt;items_db&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="nd"&gt;@app&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;"/health"&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;health_check&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="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"healthy"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/items"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;201&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;create_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;item_id&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;items_db&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;items_db&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&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;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_dump&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="nd"&gt;@app&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;"/items/{item_id}"&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;get_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&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;item_id&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items_db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Item not found"&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;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;items_db&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item_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;model_dump&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;That is our whole API for this tutorial. Three endpoints: health check, create item, get item. Small enough to understand, big enough to test properly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Understanding Pytest&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pytest is a testing framework for Python. People like it because it is simple.&lt;/p&gt;

&lt;p&gt;You do not need to write classes. You do not need to remember plenty of setup. You write a function that starts with &lt;code&gt;test_&lt;/code&gt;, you use plain &lt;code&gt;assert&lt;/code&gt;, and Pytest does the rest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compare it to this thinking:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Without Pytest:&lt;/em&gt; You manually call the API, look at the screen, and decide if it looks correct.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;With Pytest:&lt;/em&gt; You write &lt;code&gt;assert response.status_code == 200&lt;/code&gt; and Pytest decides for you.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Three words you need to know:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Test:&lt;/em&gt; A function that checks one behaviour.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Assertion:&lt;/em&gt; A line like &lt;code&gt;assert response.status_code == 201&lt;/code&gt; that must be true, otherwise the test fails.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Fixture:&lt;/em&gt; A reusable setup. For example, a fresh test client for every test, so one test does not pollute another.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;We will use all three in this article.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Setting Up a FastAPI Testing Environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us set up properly, so we do not have stress later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create your environment&lt;/strong&gt;&lt;br&gt;
python -m venv .venv&lt;br&gt;
source .venv/bin/activate&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Install dependencies&lt;/strong&gt;&lt;br&gt;
pip install fastapi uvicorn pytest httpx&lt;br&gt;
Why &lt;code&gt;httpx&lt;/code&gt;? FastAPI's &lt;code&gt;TestClient&lt;/code&gt; is built on top of it. Without it, your tests will not run.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 3: Project structure&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Keep it simple:&lt;br&gt;
my_api/&lt;br&gt;
  main.py&lt;br&gt;
  test_main.py&lt;br&gt;
That is it. &lt;code&gt;main.py&lt;/code&gt; is your app. &lt;code&gt;test_main.py&lt;/code&gt; is your tests. Do not put tests inside &lt;code&gt;main.py&lt;/code&gt;. Keep them separate.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 4: Create your TestClient&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At the top of &lt;code&gt;test_main.py&lt;/code&gt;, add:&lt;br&gt;
from fastapi.testclient import TestClient&lt;br&gt;
from main import app&lt;/p&gt;

&lt;p&gt;client = TestClient(app)&lt;br&gt;
&lt;code&gt;TestClient&lt;/code&gt; lets you call your API without actually starting a server. It is fast, and it is perfect for beginners. You do not need to run &lt;code&gt;uvicorn&lt;/code&gt; to test.&lt;/p&gt;

&lt;p&gt;But there is one problem with this simple setup: our &lt;code&gt;items_db&lt;/code&gt; dictionary will keep data between tests. If one test creates an item, the next test will see it. That can cause confusing failures.&lt;/p&gt;

&lt;p&gt;We will fix that in Section 9 with fixtures. For now, just know it is coming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Writing Your First API Test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us write the simplest test possible. The health check.&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="nc"&gt;In&lt;/span&gt; `&lt;span class="n"&gt;test_main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&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;test_health_check&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&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;"/health"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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="o"&gt;==&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="s2"&gt;"healthy"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;Run&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="n"&gt;pytest&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;
&lt;span class="nc"&gt;You&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;see&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="n"&gt;test_main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;test_health_check&lt;/span&gt; &lt;span class="no"&gt;PASSED&lt;/span&gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is your first automated test. One small thing, but it is powerful. From now on, every time you change your code, you can run &lt;code&gt;pytest&lt;/code&gt; and know in seconds if your health endpoint still works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let me break down what happened:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;client.get("/health")&lt;/code&gt; calls your API, like a browser would.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;response.status_code&lt;/code&gt; is the HTTP code — 200 means OK.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;response.json()&lt;/code&gt; is the body as a Python dictionary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;assert&lt;/code&gt; says "this must be true, or fail the test."&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can understand this one test, you can understand all the others. They are the same pattern, just with different endpoints and data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Testing Successful and Unsuccessful Responses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A good test does not only check the happy path. It also checks what happens when things go wrong.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Test a successful creation:&lt;/em&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;test_create_item_success&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"/items"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Laptop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;250000.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"in_stock"&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="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Laptop"&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;250000.0&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s2"&gt;"id"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;successful&lt;/span&gt; &lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_item_success&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;# First create an item&lt;/span&gt;
    &lt;span class="n"&gt;create_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"/items"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Phone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;150000.0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;item_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_response&lt;/span&gt;&lt;span class="p"&gt;.&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="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# Then fetch it&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&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;f&lt;/span&gt;&lt;span class="s2"&gt;"/items/{item_id}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Phone"&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;unsuccessful&lt;/span&gt; &lt;span class="n"&gt;fetch&lt;/span&gt; — &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_item_not_found&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&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;"/items/9999"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Item not found"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This last one is important. Many beginners only test success. But users will request items that do not exist. Your API must return 404, not 500, not an empty 200. Your test proves it does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Testing Request Validation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where FastAPI shines, and where many beginners get surprised.&lt;/p&gt;

&lt;p&gt;FastAPI validates your input automatically because of Pydantic. If you send a wrong type, it will not crash. It will return &lt;code&gt;422 Unprocessable Entity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let us test that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test missing required field:&lt;/strong&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;test_create_item_missing_name&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"/items"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1000.0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;422&lt;/span&gt;
&lt;span class="nc"&gt;We&lt;/span&gt; &lt;span class="n"&gt;did&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt; `&lt;span class="n"&gt;name&lt;/span&gt;`&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;reject&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="n"&gt;wrong&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_create_item_wrong_price_type&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"/items"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Book"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"not-a-number"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;422&lt;/span&gt;
&lt;span class="nc"&gt;Price&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;We&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;converted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;reject&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="n"&gt;extra&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; — &lt;span class="n"&gt;default&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_create_item_default_in_stock&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"/items"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Pen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;500.0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"in_stock"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We did not send &lt;code&gt;in_stock&lt;/code&gt;, but our model says &lt;code&gt;in_stock: bool = True&lt;/code&gt;. So FastAPI should fill it in. This test proves your defaults work.&lt;/p&gt;

&lt;p&gt;Validation testing is not boring. It is how you prove your API will not accept rubbish data from the frontend or from a bad client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Testing API Error Handling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Error handling is not just about status codes. It is about what your API tells the client when something goes wrong.&lt;/p&gt;

&lt;p&gt;In our API, we have two main errors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;404:&lt;/em&gt; Item not found&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;422:&lt;/em&gt; Validation failed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We already tested both. Let us add one more layer: check the structure of the error.&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;test_not_found_error_structure&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&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;"/items/9999"&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="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s2"&gt;"detail"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nb"&gt;isinstance&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="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Why&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;structure&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Because&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;frontend&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;depend&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;If&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;frontend&lt;/span&gt; &lt;span class="n"&gt;expects&lt;/span&gt; `&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;` &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; `&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;` &lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;frontend&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;Your&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;catch&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="n"&gt;early&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more practical tip: never expose internal details in your error responses. A good error says "Item not found". A bad error says "KeyError: 9999 in items_db at line 24 in &lt;a href="http://main.py" rel="noopener noreferrer"&gt;http://main.py&lt;/a&gt;". The first helps the user. The second helps an attacker.&lt;/p&gt;

&lt;p&gt;In our test, we assert the detail is a clean string. That is a small way to enforce good error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Organizing and Running Automated Tests&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let us fix the problem I mentioned in Section 4 — tests polluting each other.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;items_db&lt;/code&gt; is global. If &lt;code&gt;test_create_item_success&lt;/code&gt; runs first and creates an item with id 1, then &lt;code&gt;test_get_item_success&lt;/code&gt; will create an item with id 2, not id 1. That is confusing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use a fixture to reset the database before each test.&lt;/p&gt;

&lt;p&gt;Update &lt;code&gt;test_main.py&lt;/code&gt; to this clean version:&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;pytest&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;testclient&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;TestClient&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items_db&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TestClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@pytest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fixture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autouse&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;def&lt;/span&gt; &lt;span class="nf"&gt;reset_db&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;# Runs before each test&lt;/span&gt;
    &lt;span class="n"&gt;items_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;
    &lt;span class="c1"&gt;# Runs after each test&lt;/span&gt;
    &lt;span class="n"&gt;items_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
`&lt;span class="nd"&gt;@pytest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fixture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autouse&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means "run this automatically before every test". Now every test starts with an empty database. No pollution. No guessing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to run your tests:&lt;/strong&gt;&lt;br&gt;
Run all tests&lt;br&gt;
pytest&lt;/p&gt;

&lt;p&gt;Run with details&lt;br&gt;
pytest -v&lt;/p&gt;

&lt;p&gt;Run only one file&lt;br&gt;
pytest test_main.py -v&lt;/p&gt;

&lt;p&gt;Run only tests with a keyword&lt;br&gt;
pytest -k "not_found" -v&lt;/p&gt;

&lt;p&gt;See coverage (optional but useful)&lt;br&gt;
pip install pytest-cov&lt;br&gt;
pytest --cov=main -v&lt;br&gt;
&lt;em&gt;How to organize when you grow:&lt;/em&gt;&lt;br&gt;
my_api/&lt;br&gt;
  main.py&lt;br&gt;
  models.py&lt;br&gt;
  database.py&lt;br&gt;
  tests/&lt;br&gt;
    test_items.py&lt;br&gt;
    test_health.py&lt;br&gt;
One file per feature. &lt;code&gt;test_items.py&lt;/code&gt; for items, &lt;code&gt;test_health.py&lt;/code&gt; for health. When you have 50 tests, you will thank yourself for organizing early.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Common Mistakes and Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are mistakes I have made myself, and I see beginners make them often.&lt;br&gt;
Mistake What To Do Instead&lt;br&gt;
Only testing success cases  Always test 404, 422, and invalid input&lt;br&gt;
Hardcoding IDs that depend on test order    Use fixtures to reset state, create fresh data in each test&lt;br&gt;
Testing with a real database in unit tests  Use TestClient and in-memory data for unit tests, use a separate test database for integration tests&lt;br&gt;
Checking only status code   Also check response body, fields, and error structure&lt;br&gt;
Writing one giant test that checks everything   One test = one behaviour. Small tests are easier to debug&lt;br&gt;
Not running tests after every change    Run &lt;code&gt;pytest&lt;/code&gt; before you commit, every time&lt;br&gt;
Exposing internal errors    Return clean error messages, log the real error internally&lt;br&gt;
&lt;em&gt;Best practices I follow now:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;One test, one behaviour.&lt;/strong&gt; If a test fails, you should know immediately what broke.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Use descriptive names.&lt;/em&gt; &lt;code&gt;test_get_item_not_found&lt;/code&gt; is better than &lt;code&gt;test_2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Test validation.&lt;/em&gt; If FastAPI gives you free validation, test that it works.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Keep tests fast.&lt;/em&gt; If tests are slow, you will stop running them. TestClient is fast. Use it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Do not test FastAPI itself.&lt;/em&gt; You do not need to test that FastAPI can return JSON. Test &lt;em&gt;your&lt;/em&gt; logic: your validation, your 404, your business rules.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;11. Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API testing is not something you do after you finish building. It is part of building.&lt;/p&gt;

&lt;p&gt;FastAPI gives you the API. Pytest gives you the proof that it works. Together, they give you confidence. You can change your code, run &lt;code&gt;pytest&lt;/code&gt;, and know in seconds if you broke something.&lt;/p&gt;

&lt;p&gt;In this tutorial, you learnt how to set up a testing environment, write your first test, test success and failure, test validation, test error handling, and organize your tests so they stay clean as you grow.&lt;/p&gt;

&lt;p&gt;That is the foundation. The next step is to apply it to a real project — like the FastAPI + PostgreSQL API from my last article. Replace the in-memory &lt;code&gt;items_db&lt;/code&gt; with a real database, use a test database, and write the same kinds of tests for your &lt;code&gt;/records&lt;/code&gt; endpoints.&lt;/p&gt;

&lt;p&gt;That is how I build now. One working endpoint, one passing test at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Official Technical References&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FastAPI Tutorial — &lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;FastAPI Testing — &lt;a href="https://fastapi.tiangolo.com/tutorial/testing/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/tutorial/testing/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Pytest Documentation — &lt;a href="https://docs.pytest.org/" rel="noopener noreferrer"&gt;https://docs.pytest.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Pydantic Documentation — &lt;a href="https://docs.pydantic.dev/" rel="noopener noreferrer"&gt;https://docs.pydantic.dev/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;HTTPX Documentation — &lt;a href="https://www.python-httpx.org/" rel="noopener noreferrer"&gt;https://www.python-httpx.org/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;I am Ezenwa Christian Chimeremeze, a Technical Writer and Python Engineer based in Imo State, Nigeria. I write about FastAPI, PostgreSQL, and AI systems — from what I have built, broken, fixed, and shipped myself, often from my phone using Termux.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>testing</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Database Migrations at Scale: How to Change Production Databases Without Breaking Applications</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Wed, 18 Nov 2026 03:38:58 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/database-migrations-at-scale-how-to-change-production-databases-without-breaking-applications-4705</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/database-migrations-at-scale-how-to-change-production-databases-without-breaking-applications-4705</guid>
      <description>&lt;p&gt;&lt;em&gt;Database Migrations at Scale: How to Change Production Databases Without Breaking Applications&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;By Ezenwa Christian Chimeremeze&lt;/p&gt;

&lt;p&gt;An engineering deep dive into PostgreSQL, schema evolution, backward compatibility, zero-downtime deployments, data integrity, and production database reliability.&lt;/p&gt;

&lt;p&gt;A database migration can look deceptively simple.&lt;/p&gt;

&lt;p&gt;A developer adds a column, renames a field, changes a data type, creates an index, or introduces a new relationship between tables. The application works locally, the migration runs successfully, and the feature appears ready for deployment.&lt;/p&gt;

&lt;p&gt;But production is a different reality.&lt;/p&gt;

&lt;p&gt;An application may have thousands of active users, several running application instances, background workers, scheduled jobs, external integrations, and millions of existing database records. Not every component updates at the same moment.&lt;/p&gt;

&lt;p&gt;A database migration that works perfectly in development can therefore cause application errors, unexpected locks, inconsistent data, or deployment failures in production.&lt;/p&gt;

&lt;p&gt;This is why database migrations deserve more attention than simply writing SQL scripts.&lt;/p&gt;

&lt;p&gt;They are an important part of software architecture, deployment engineering, and application reliability.&lt;/p&gt;

&lt;p&gt;In this article, I will explore how database migrations work, why production migrations are difficult, and how engineering teams can introduce structural and data changes while keeping applications available.&lt;/p&gt;

&lt;p&gt;The examples primarily use PostgreSQL, although many of the underlying principles apply to other relational database systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Understanding Database Migrations
&lt;/h3&gt;

&lt;p&gt;A database migration is a controlled change to a database's structure or stored data.&lt;/p&gt;

&lt;p&gt;These changes may include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating tables&lt;/li&gt;
&lt;li&gt;Adding or removing columns&lt;/li&gt;
&lt;li&gt;Changing column types&lt;/li&gt;
&lt;li&gt;Creating indexes&lt;/li&gt;
&lt;li&gt;Introducing foreign-key relationships&lt;/li&gt;
&lt;li&gt;Modifying constraints&lt;/li&gt;
&lt;li&gt;Moving data between tables&lt;/li&gt;
&lt;li&gt;Updating existing records&lt;/li&gt;
&lt;li&gt;Removing obsolete structures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider an application that initially stores a user's name in a single column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&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;email&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NOT&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;created_at&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later, the product requires separate first and last names.&lt;/p&gt;

&lt;p&gt;A developer might initially consider replacing the &lt;code&gt;name&lt;/code&gt; column with two new columns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&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="p"&gt;,&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&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="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;This&lt;/span&gt; &lt;span class="n"&gt;looks&lt;/span&gt; &lt;span class="n"&gt;straightforward&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, what happens if the currently deployed application still expects &lt;code&gt;users.name&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Its queries may immediately start failing.&lt;/p&gt;

&lt;p&gt;This illustrates an important principle:&lt;/p&gt;

&lt;p&gt;A database schema is not independent of the application that uses it.&lt;/p&gt;

&lt;p&gt;The database and application form a connected system. Changing one can affect the other.&lt;/p&gt;

&lt;p&gt;A migration must therefore consider application compatibility, deployment order, existing data, and operational behaviour.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Why Production Database Migrations Are Difficult
&lt;/h3&gt;

&lt;p&gt;Development databases usually have fewer records, fewer concurrent connections, and fewer dependent services.&lt;/p&gt;

&lt;p&gt;Production databases are different.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Existing data&lt;/em&gt;&lt;br&gt;
A production database may contain millions of records accumulated over several years.&lt;/p&gt;

&lt;p&gt;Adding a new field is not necessarily difficult, but deciding what value existing records should receive can be complicated.&lt;/p&gt;

&lt;p&gt;For example, introducing a mandatory &lt;code&gt;account_status&lt;/code&gt; field requires determining the correct status for existing accounts.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Application compatibility&lt;/em&gt;&lt;br&gt;
During a rolling deployment, old and new application versions may run simultaneously.&lt;/p&gt;

&lt;p&gt;The old version may expect one schema while the new version expects another.&lt;/p&gt;

&lt;p&gt;If the migration breaks the old version before the deployment finishes, active application instances can fail.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Database locks&lt;/em&gt;&lt;br&gt;
Some schema operations require locks that temporarily restrict access to database objects.&lt;/p&gt;

&lt;p&gt;Depending on the operation and database version, a migration may block queries or wait behind long-running transactions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Data integrity&lt;/em&gt;&lt;br&gt;
Changing existing records can introduce inconsistencies if relationships, constraints, or transformation rules are not carefully considered.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Deployment coordination&lt;/em&gt;&lt;br&gt;
A migration may affect web servers, background workers, scheduled tasks, reporting services, and external integrations.&lt;/p&gt;

&lt;p&gt;Updating only the main application is not always sufficient.&lt;/p&gt;

&lt;p&gt;These challenges explain why production migrations require planning rather than simply executing SQL.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Schema Migrations and Data Migrations Are Different
&lt;/h3&gt;

&lt;p&gt;It is useful to distinguish between two major categories.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Schema migrations&lt;/em&gt;&lt;br&gt;
Schema migrations change the database structure.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;br&gt;
A&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;LTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;phone_number&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&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;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;projects&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&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;owner_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;users&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Data migrations&lt;/em&gt;&lt;br&gt;
Data migrations transform existing records.&lt;/p&gt;

&lt;p&gt;For example, an application may need to convert a legacy status value into a newer format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'enabled'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;In&lt;/span&gt; &lt;span class="n"&gt;small&lt;/span&gt; &lt;span class="n"&gt;databases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt; &lt;span class="n"&gt;may&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;straightforward&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In larger databases, updating every record in one transaction can create operational problems.&lt;/p&gt;

&lt;p&gt;Long-running transactions can increase resource consumption, delay maintenance, and create substantial database load.&lt;/p&gt;

&lt;p&gt;Data migrations should therefore be designed around the volume of records and the application's availability requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. The Expand-and-Contract Migration Pattern
&lt;/h3&gt;

&lt;p&gt;One of the most useful strategies for backward-compatible database changes is the expand-and-contract pattern.&lt;/p&gt;

&lt;p&gt;The principle is simple:&lt;/p&gt;

&lt;p&gt;Instead of replacing an existing database structure immediately, introduce the new structure alongside the old one, migrate the application gradually, and remove the old structure only after it is no longer needed.&lt;/p&gt;

&lt;p&gt;Consider the earlier example involving a user's full name.&lt;/p&gt;

&lt;p&gt;The original database contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="sb"&gt;`name VARCHAR(255)`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new design requires:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="sb"&gt;`first_name VARCHAR(100)`&lt;/span&gt;
&lt;span class="sb"&gt;`last_name VARCHAR(100)`&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A safer migration can be divided into stages.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stage 1: Expand the schema&lt;/em&gt;&lt;br&gt;
Introduce the new columns without removing the existing column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&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="p"&gt;,&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&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="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="n"&gt;continues&lt;/span&gt; &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="sb"&gt;`name`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database now supports both representations.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stage 2: Update application behaviour&lt;/em&gt;&lt;br&gt;
Deploy an application version that understands both structures.&lt;/p&gt;

&lt;p&gt;For example, the application can read the new fields when available while retaining compatibility with existing records.&lt;/p&gt;

&lt;p&gt;During the transition, writes must be handled consistently.&lt;/p&gt;

&lt;p&gt;One possible approach is to temporarily maintain both representations through carefully controlled application logic.&lt;/p&gt;

&lt;p&gt;However, dual writes introduce consistency risks. If one write succeeds and the other fails, the values can diverge.&lt;/p&gt;

&lt;p&gt;For important data, the application should use appropriate transactions, reconciliation, or a deliberately designed synchronisation mechanism.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stage 3: Backfill existing records&lt;/em&gt;&lt;br&gt;
Existing records need to be converted.&lt;/p&gt;

&lt;p&gt;For a simple illustrative example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt;
    &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;split_part&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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="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="n"&gt;last_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nb"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="n"&gt;name&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="p"&gt;)&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WHERE first_name IS NULL;&lt;br&gt;
This example assumes a relatively simple naming format. Real-world names are not reliably separable using a single-space rule.&lt;/p&gt;

&lt;p&gt;A production migration should use business-approved transformation logic and preserve ambiguous records for review.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stage 4: Verify the new representation&lt;/em&gt;&lt;br&gt;
Check how many records still require migration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*)
FROM users
WHERE first_name IS NULL;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additional validation should compare old and new representations and identify inconsistent records.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stage 5: Move application reads to the new fields&lt;/em&gt;&lt;br&gt;
Once the new representation is populated and verified, update the application so that it no longer depends on the old column.&lt;/p&gt;

&lt;p&gt;All relevant application versions, workers, and integrations must be checked.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stage 6: Contract the schema&lt;/em&gt;&lt;br&gt;
Only after confirming that the old column is no longer used should it be removed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This final step should happen in a separate deployment, not as an immediate part of the initial migration.&lt;/p&gt;

&lt;p&gt;The expand-and-contract pattern creates a controlled transition rather than forcing the database and application to change simultaneously.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Understanding PostgreSQL Locks and Migration Safety
&lt;/h3&gt;

&lt;p&gt;PostgreSQL provides powerful transactional capabilities, but database operations have different locking requirements.&lt;/p&gt;

&lt;p&gt;A migration that appears harmless can still interfere with production traffic.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;phone_number&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&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;This operation is generally metadata-oriented when adding a nullable column without a default, but it still requires a table lock.&lt;/p&gt;

&lt;p&gt;The actual impact depends on existing transactions, lock contention, table activity, and the PostgreSQL version.&lt;/p&gt;

&lt;p&gt;Developers should understand the locking behaviour of the operations they introduce.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Using lock timeouts&lt;/em&gt;&lt;br&gt;
A migration should not wait indefinitely for a required lock.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="nc"&gt;PostgreSQL&lt;/span&gt; &lt;span class="n"&gt;provides&lt;/span&gt; &lt;span class="sb"&gt;`lock_timeout`&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;lock_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'5s'&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 sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;phone_number&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&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;If the required lock cannot be acquired within the configured period, the statement fails instead of waiting indefinitely.&lt;/p&gt;

&lt;p&gt;This allows deployment systems to detect the problem and retry under appropriate conditions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Using statement timeouts&lt;/em&gt;&lt;br&gt;
A statement timeout can also limit how long a database operation is allowed to execute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;statement_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'60s'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The appropriate values depend on the migration, environment, and operational requirements.&lt;/p&gt;

&lt;p&gt;Timeouts are safeguards, not substitutes for understanding the migration's behaviour.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Creating indexes safely&lt;/em&gt;&lt;br&gt;
Indexes can improve query performance, but creating them on heavily used tables requires care.&lt;/p&gt;

&lt;p&gt;PostgreSQL supports concurrent index creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="no"&gt;CONCURRENTLY&lt;/span&gt; &lt;span class="n"&gt;idx_users_email&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nf"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&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;This allows normal inserts, updates, and deletes to continue during much of the index-building process.&lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; cannot run inside a transaction block.&lt;/p&gt;

&lt;p&gt;It also has additional operational considerations, including longer execution time and the possibility of an invalid index after a failed operation.&lt;/p&gt;

&lt;p&gt;Migration frameworks must account for this rather than assuming every migration can run inside one transaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Safely Migrating Large Datasets
&lt;/h3&gt;

&lt;p&gt;One of the biggest mistakes in database engineering is assuming that an operation tested against a small dataset will behave identically against production data.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This may work perfectly with a few hundred records.&lt;/p&gt;

&lt;p&gt;But imagine millions of records.&lt;/p&gt;

&lt;p&gt;A large update can produce significant write activity, generate substantial WAL, hold locks for an extended period, and increase replication lag.&lt;/p&gt;

&lt;p&gt;A better approach may be to process records in batches.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Batch-based migration&lt;/em&gt;&lt;br&gt;
A simplified PostgreSQL approach uses a common table expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;batch&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
    &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;batch&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;batch&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updates a limited number of records in one execution.&lt;/p&gt;

&lt;p&gt;A migration worker can repeat the operation until no eligible records remain.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SKIP LOCKED&lt;/code&gt; can be useful when multiple workers process independent batches, but it is not necessary for every migration.&lt;/p&gt;

&lt;p&gt;The implementation must also account for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retry behaviour&lt;/li&gt;
&lt;li&gt;Transaction boundaries&lt;/li&gt;
&lt;li&gt;Progress tracking&lt;/li&gt;
&lt;li&gt;Failed batches&lt;/li&gt;
&lt;li&gt;Records updated concurrently by the application&lt;/li&gt;
&lt;li&gt;Final verification&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Make migrations resumable&lt;/em&gt;&lt;br&gt;
A robust large-data migration should be able to continue after interruption.&lt;/p&gt;

&lt;p&gt;For example, the migration process can track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total records expected&lt;/li&gt;
&lt;li&gt;Records processed&lt;/li&gt;
&lt;li&gt;Records remaining&lt;/li&gt;
&lt;li&gt;Last successfully processed identifier&lt;/li&gt;
&lt;li&gt;Failed records&lt;/li&gt;
&lt;li&gt;Completion status&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It should not assume that the entire operation will always finish in one execution.&lt;/p&gt;

&lt;p&gt;Resumability is especially valuable for large production systems.&lt;/p&gt;
&lt;h3&gt;
  
  
  7. Transactions, Atomicity, and Migration Design
&lt;/h3&gt;

&lt;p&gt;Transactions are central to relational database reliability.&lt;/p&gt;

&lt;p&gt;They allow related operations to succeed or fail together.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&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="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the operation fails before commit, the transaction can be rolled back.&lt;/p&gt;

&lt;p&gt;Database migrations can also benefit from transactions.&lt;/p&gt;

&lt;p&gt;However, not every migration operation is transaction-compatible.&lt;/p&gt;

&lt;p&gt;PostgreSQL operations such as &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; cannot run inside an ordinary transaction block.&lt;/p&gt;

&lt;p&gt;Large data transformations may also be better handled through multiple bounded transactions rather than one enormous transaction.&lt;/p&gt;

&lt;p&gt;The correct strategy depends on the operation.&lt;/p&gt;

&lt;p&gt;A useful rule is to understand whether a migration requires atomic execution, incremental progress, or a specialised deployment procedure.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Versioning and Migration History
&lt;/h3&gt;

&lt;p&gt;A database should have a clear migration history.&lt;/p&gt;

&lt;p&gt;Without one, developers may struggle to understand which schema changes have already been applied.&lt;/p&gt;

&lt;p&gt;Most migration frameworks maintain a migration tracking table.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;001_create_users
002_add_account_status
003_create_projects
004_add_project_owner
005_create_project_indexes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each migration represents a known change to the database.&lt;/p&gt;

&lt;p&gt;The migration system records which changes have been executed.&lt;/p&gt;

&lt;p&gt;Popular migration tools include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alembic for SQLAlchemy applications&lt;/li&gt;
&lt;li&gt;Django migrations&lt;/li&gt;
&lt;li&gt;Flyway&lt;/li&gt;
&lt;li&gt;Liquibase&lt;/li&gt;
&lt;li&gt;Prisma Migrate&lt;/li&gt;
&lt;li&gt;Drizzle ORM migrations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The correct choice depends on the framework and architecture.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why migration history matters&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Migration history provides several benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reproducible database environments&lt;/li&gt;
&lt;li&gt;Consistent development and production schemas&lt;/li&gt;
&lt;li&gt;Traceability of structural changes&lt;/li&gt;
&lt;li&gt;Better deployment automation&lt;/li&gt;
&lt;li&gt;Easier investigation of schema-related failures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Migration files should generally be treated as version-controlled application assets.&lt;/p&gt;

&lt;p&gt;Once a migration has been applied to shared or production environments, modifying its historical contents can create inconsistencies.&lt;/p&gt;

&lt;p&gt;A new corrective migration is usually safer than silently rewriting an already-applied migration.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Rollbacks Are Not Always Simple
&lt;/h3&gt;

&lt;p&gt;Many migration frameworks provide rollback commands.&lt;/p&gt;

&lt;p&gt;However, reversing a database change is not necessarily equivalent to restoring the original state.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;middle_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the column is removed, its previous data is no longer available in that table.&lt;/p&gt;

&lt;p&gt;A rollback migration can recreate the column, but it cannot automatically reconstruct the deleted values.&lt;/p&gt;

&lt;p&gt;This is why rollback strategies must be designed before deployment.&lt;/p&gt;

&lt;p&gt;Possible recovery approaches include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Restoring from verified backups&lt;/li&gt;
&lt;li&gt;Point-in-time recovery&lt;/li&gt;
&lt;li&gt;Reversing compatible schema changes&lt;/li&gt;
&lt;li&gt;Re-running corrective migrations&lt;/li&gt;
&lt;li&gt;Restoring data from an external source&lt;/li&gt;
&lt;li&gt;Rolling back the application while keeping the expanded schema&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For destructive migrations, backups and recovery procedures must be verified rather than merely assumed to exist.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Roll-forward versus rollback&lt;/em&gt;&lt;br&gt;
In some production systems, rolling forward with a corrective migration is safer than attempting to reverse a partially completed data transformation.&lt;/p&gt;

&lt;p&gt;For example, if a new field has been populated incorrectly, a corrective migration may repair the affected records without reverting the entire application.&lt;/p&gt;

&lt;p&gt;The choice depends on the failure, available recovery points, and data integrity requirements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frad2fswj4zkf2l1u5mnv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frad2fswj4zkf2l1u5mnv.jpg" alt=" " width="718" height="395"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  10. Testing Database Migrations Before Deployment
&lt;/h3&gt;

&lt;p&gt;Migration testing should be part of the normal development process.&lt;/p&gt;

&lt;p&gt;At minimum, test against representative versions of the database schema and realistic data.&lt;/p&gt;

&lt;p&gt;Important test categories include:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fresh database tests&lt;/em&gt;&lt;br&gt;
Can a new database be created by applying the complete migration history?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Upgrade tests&lt;/em&gt;&lt;br&gt;
Can an existing database move from the previous schema to the new schema?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Data integrity tests&lt;/em&gt;&lt;br&gt;
Do existing records retain their expected relationships and values?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Compatibility tests&lt;/em&gt;&lt;br&gt;
Can the old application version continue operating during the compatibility window?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Performance tests&lt;/em&gt;&lt;br&gt;
Does the migration introduce excessive execution time, locking, or resource consumption?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Failure tests&lt;/em&gt;&lt;br&gt;
What happens if a migration fails halfway through?&lt;/p&gt;

&lt;p&gt;A migration that works only when everything goes perfectly is not sufficiently reliable for critical production environments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example verification query&lt;/em&gt;&lt;br&gt;
Suppose a migration introduces a new required account status.&lt;/p&gt;

&lt;p&gt;You can inspect remaining records:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS total_users,
    COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_status&lt;/span&gt; &lt;span class="k"&gt;IS&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;AS&lt;/span&gt; &lt;span class="n"&gt;missing_status&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The migration should not proceed to a stage requiring non-null values until the missing records have been appropriately handled.&lt;/p&gt;

&lt;h3&gt;
  
  
  11. Integrating Database Migrations into CI/CD
&lt;/h3&gt;

&lt;p&gt;A database migration should be part of the application's deployment process.&lt;/p&gt;

&lt;p&gt;A simplified pipeline might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Developer submits changes
  |
  v
Automated code review
  |
  v
Unit and integration tests
  |
  v
Migration validation
  |
  v
Staging deployment
  |
  v
Migration verification
  |
  v
Production deployment
  |
  v
Post-deployment checks
  |
  v
Monitoring and reporting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sequence is illustrative. The exact order of application and schema deployment depends on the migration strategy.&lt;/p&gt;

&lt;p&gt;For an expand-and-contract migration, for example, the schema expansion may need to happen before deploying the application version that uses the new columns.&lt;/p&gt;

&lt;p&gt;The contraction happens later, after old application versions and dependencies have stopped using the old structure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Deployment responsibilities&lt;/em&gt;&lt;br&gt;
A reliable deployment system should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevent conflicting migration executions&lt;/li&gt;
&lt;li&gt;Record migration results&lt;/li&gt;
&lt;li&gt;Capture execution logs&lt;/li&gt;
&lt;li&gt;Apply appropriate timeouts&lt;/li&gt;
&lt;li&gt;Support safe retries&lt;/li&gt;
&lt;li&gt;Detect partial failures&lt;/li&gt;
&lt;li&gt;Verify the resulting schema&lt;/li&gt;
&lt;li&gt;Alert operators when migration steps fail&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Database migrations should not run independently on every application instance during startup unless the migration system explicitly provides safe coordination.&lt;/p&gt;

&lt;p&gt;Otherwise, multiple instances may attempt to execute the same migration simultaneously.&lt;/p&gt;

&lt;p&gt;A dedicated migration job or controlled deployment step is often easier to reason about.&lt;/p&gt;
&lt;h3&gt;
  
  
  12. Observability: Knowing Whether a Migration Is Healthy
&lt;/h3&gt;

&lt;p&gt;Executing a migration successfully is only part of the process.&lt;/p&gt;

&lt;p&gt;Developers also need visibility into what happens afterward.&lt;/p&gt;

&lt;p&gt;Useful metrics include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Migration execution duration&lt;/li&gt;
&lt;li&gt;Lock acquisition time&lt;/li&gt;
&lt;li&gt;Database CPU and memory utilisation&lt;/li&gt;
&lt;li&gt;Active database connections&lt;/li&gt;
&lt;li&gt;Query latency&lt;/li&gt;
&lt;li&gt;Replication lag&lt;/li&gt;
&lt;li&gt;Number of records processed&lt;/li&gt;
&lt;li&gt;Number of failed records&lt;/li&gt;
&lt;li&gt;Application error rates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL provides useful diagnostic views.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;pid&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;wait_event_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;wait_event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_activity&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'idle'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can help identify active queries and sessions waiting on database resources.&lt;/p&gt;

&lt;p&gt;For production operations, developers should also investigate long-running transactions, blocked sessions, and replication health where relevant.&lt;/p&gt;

&lt;p&gt;Monitoring allows teams to distinguish between a migration that technically completed and one that has introduced operational problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  13. Common Database Migration Mistakes
&lt;/h3&gt;

&lt;p&gt;Several mistakes repeatedly create problems in production environments.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Removing old columns too early&lt;/em&gt; - The application may still depend on them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Running enormous data updates without planning&lt;/em&gt; - Large transactions can create unnecessary database pressure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Ignoring lock behaviour&lt;/em&gt; - A migration can block application traffic even if the SQL statement looks simple.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Assuming rollback restores deleted data&lt;/em&gt; - Recreating a column does not restore the values that were removed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Skipping realistic testing&lt;/em&gt; - A migration tested against ten records may behave differently against ten million.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Running migrations independently on every server&lt;/em&gt; - Uncoordinated execution can create deployment conflicts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Forgetting background workers&lt;/em&gt; - Workers may continue using old queries after the main application has been updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Failing to verify data transformations&lt;/em&gt; - A migration can finish successfully while producing incorrect values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Combining expansion and destruction in one deployment&lt;/em&gt; - This removes the opportunity for a controlled compatibility transition.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Treating database migrations as an afterthought&lt;/em&gt; - Schema changes are part of application engineering and deserve the same planning and review as source-code changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  14. A Practical Production Migration Checklist
&lt;/h3&gt;

&lt;p&gt;Before deploying a database migration, consider the following.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Architecture&lt;/em&gt;&lt;br&gt;
Does the new schema support the intended feature?&lt;br&gt;
Have all dependent application components been identified?&lt;br&gt;
Is the change backward-compatible?&lt;br&gt;
Are background workers and external integrations considered?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Data&lt;/em&gt;&lt;br&gt;
Is existing data preserved?&lt;br&gt;
Are transformation rules clearly defined?&lt;br&gt;
Can the migration resume after interruption?&lt;br&gt;
Are validation queries available?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Performance&lt;/em&gt;&lt;br&gt;
Could the migration acquire restrictive locks?&lt;br&gt;
Is an index operation suitable for concurrent creation?&lt;br&gt;
Are timeouts configured appropriately?&lt;br&gt;
Has the migration been tested against realistic data volumes?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Security&lt;/em&gt;&lt;br&gt;
Are permissions appropriate?&lt;br&gt;
Are sensitive values protected?&lt;br&gt;
Are backup and recovery procedures available?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Deployment&lt;/em&gt;&lt;br&gt;
Is migration execution coordinated?&lt;br&gt;
Is the deployment sequence correct?&lt;br&gt;
Are application versions compatible during the transition?&lt;br&gt;
Is there a recovery plan?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Verification&lt;/em&gt;&lt;br&gt;
Have migration tests passed?&lt;br&gt;
Has the resulting schema been inspected?&lt;br&gt;
Are data integrity checks available?&lt;br&gt;
Will application and database health be monitored afterward?&lt;/p&gt;

&lt;p&gt;This checklist is not a replacement for database-specific documentation, but it provides a useful foundation for engineering reviews.&lt;/p&gt;

&lt;h3&gt;
  
  
  15. Final Thoughts: Database Migrations Are Part of Software Reliability
&lt;/h3&gt;

&lt;p&gt;As software applications grow, their databases must evolve.&lt;/p&gt;

&lt;p&gt;New features introduce new relationships, changing business requirements demand different data structures, and increasing workloads create new performance requirements.&lt;/p&gt;

&lt;p&gt;The challenge is not simply making these changes.&lt;/p&gt;

&lt;p&gt;It is making them without unnecessarily disrupting the systems that already depend on the database.&lt;/p&gt;

&lt;p&gt;That requires a combination of architectural planning, backward compatibility, transaction management, data validation, deployment coordination, testing, and observability.&lt;/p&gt;

&lt;p&gt;The expand-and-contract pattern offers a practical way to manage many structural changes. Batch processing helps make large data transformations more manageable. Migration versioning improves consistency, while monitoring and recovery planning help protect production systems.&lt;/p&gt;

&lt;p&gt;There is no universal migration strategy that works for every database operation.&lt;/p&gt;

&lt;p&gt;The correct approach depends on the database engine, schema, data volume, application architecture, deployment model, and availability requirements.&lt;/p&gt;

&lt;p&gt;But one principle remains important:&lt;/p&gt;

&lt;p&gt;A successful database migration is not merely one that executes without errors. It is one that preserves data integrity, maintains application compatibility, and leaves the system in a healthy, verifiable state.&lt;/p&gt;

&lt;p&gt;Database engineering is not just about storing information. It is about ensuring that information remains reliable as software evolves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;###About Me&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'm Ezenwa Christian Chimeremeze, a developer, technical writer, and founder of Geminio OS, an AI-first full-stack development ecosystem. I'm passionate about software engineering, database architecture, artificial intelligence, developer documentation, and automation. I enjoy exploring complex technical challenges, building practical solutions, and documenting what I learn through real-world development experiences.&lt;/p&gt;

&lt;p&gt;You can connect with me:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- GitHub:&lt;/strong&gt; &lt;a href="https://github.com/ezenwachristianchimeremeze" rel="noopener noreferrer"&gt;https://github.com/ezenwachristianchimeremeze&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;- Portfolio:&lt;/strong&gt; &lt;a href="https://ezenwa-christian.netlify.app" rel="noopener noreferrer"&gt;https://ezenwa-christian.netlify.app&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;- Medium:&lt;/strong&gt; &lt;a href="https://ezenwachristianchimeremeze.medium.com/" rel="noopener noreferrer"&gt;https://ezenwachristianchimeremeze.medium.com/&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;LinkedIn:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/ezenwa-christian-chimeremeze-b77115440" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/ezenwa-christian-chimeremeze-b77115440&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>database</category>
      <category>sql</category>
    </item>
    <item>
      <title>The Future of Software Development: How AI Is Transforming the Way We Build, Test, and Document Software</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Mon, 16 Nov 2026 23:31:37 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/the-future-of-software-development-how-ai-is-transforming-the-way-we-build-test-and-document-anl</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/the-future-of-software-development-how-ai-is-transforming-the-way-we-build-test-and-document-anl</guid>
      <description>&lt;p&gt;&lt;strong&gt;By Ezenwa Christian Chimeremeze&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Software development is entering a new era.&lt;/p&gt;

&lt;p&gt;For decades, building software required developers to translate ideas into technical specifications, design system architectures, write code, configure databases, implement authentication, test individual components, document APIs, and deploy applications. Each stage demanded time, technical knowledge, and considerable coordination.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Artificial intelligence is changing this process.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Today, developers can use AI to explore architectural decisions, generate code, identify potential defects, write tests, explain unfamiliar systems, and produce technical documentation. More importantly, AI-powered agents are beginning to move beyond answering questions and assisting with isolated programming tasks. They can participate in longer development workflows involving multiple files, tools, and stages of implementation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;However, this transformation raises an important question:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If AI can help build software, what should the next generation of software development environments look like?&lt;/p&gt;

&lt;p&gt;I have been exploring this question through my work on Geminio OS, an AI-first full-stack development ecosystem designed around the idea of bringing intelligent development capabilities into a unified environment.&lt;/p&gt;

&lt;p&gt;This article examines how AI is transforming software engineering, the opportunities and limitations of autonomous development, and why the future will depend on collaboration between human developers and intelligent systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Understanding the Traditional Software Development Lifecycle&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To understand the significance of AI, it is useful to examine how software development traditionally works.&lt;/p&gt;

&lt;p&gt;A conventional software project generally follows several stages.&lt;/p&gt;

&lt;p&gt;Planning and requirements gathering&lt;/p&gt;

&lt;p&gt;Every successful software project begins with a problem that needs to be solved.&lt;/p&gt;

&lt;p&gt;Developers and product teams identify the intended users, define functionality, establish technical requirements, and determine the expected outcome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example,&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;building a project management application might require:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User registration and authentication.&lt;/li&gt;
&lt;li&gt;Project creation and management.&lt;/li&gt;
&lt;li&gt;Task assignment.&lt;/li&gt;
&lt;li&gt;Notifications.&lt;/li&gt;
&lt;li&gt;Database persistence.&lt;/li&gt;
&lt;li&gt;Administrative controls.&lt;/li&gt;
&lt;li&gt;Reporting and analytics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These requirements eventually become technical specifications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Architecture and system design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once requirements are established, developers determine how the application should work internally.&lt;/p&gt;

&lt;p&gt;This involves selecting programming languages, frameworks, databases, authentication mechanisms, APIs, hosting infrastructure, and architectural patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example,&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;a modern application might use:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React or Next.js for its frontend.&lt;/li&gt;
&lt;li&gt;Node.js or FastAPI for its backend.&lt;/li&gt;
&lt;li&gt;PostgreSQL for relational data.&lt;/li&gt;
&lt;li&gt;Redis for caching.&lt;/li&gt;
&lt;li&gt;REST APIs for communication.&lt;/li&gt;
&lt;li&gt;Cloud infrastructure for deployment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These decisions influence scalability, security, maintainability, and long-term development costs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implementation is where developers translate technical specifications into working software.&lt;/p&gt;

&lt;p&gt;They create components, implement business logic, configure databases, integrate external services, and connect different parts of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This stage often involves considerable repetition.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A developer might create similar forms, API endpoints, validation rules, database operations, and authentication workflows across multiple projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing and debugging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Software that compiles successfully is not necessarily software that works correctly.&lt;/p&gt;

&lt;p&gt;Developers must verify that individual components, integrated services, and complete user workflows behave as expected.&lt;/p&gt;

&lt;p&gt;Testing includes unit tests, integration tests, end-to-end tests, security checks, and performance evaluations.&lt;/p&gt;

&lt;p&gt;Debugging then involves investigating failures, understanding their causes, implementing corrections, and verifying that existing functionality remains intact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documentation and deployment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The final stages involve explaining how the software works and making it available to users.&lt;/p&gt;

&lt;p&gt;Documentation may include installation instructions, API references, architecture explanations, troubleshooting guides, and developer onboarding materials.&lt;/p&gt;

&lt;p&gt;Deployment introduces another set of responsibilities involving environment configuration, database migrations, monitoring, release management, and operational security.&lt;/p&gt;

&lt;p&gt;The traditional development lifecycle remains valuable. However, many of its activities can now be supported or partially automated by AI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The Rise of AI-Assisted Software Development&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI-assisted development is more than autocomplete.&lt;/p&gt;

&lt;p&gt;Earlier programming assistants primarily helped developers complete code snippets or identify syntax errors. Modern AI systems can interpret natural-language instructions, reason about existing code, explain technical concepts, and assist with changes across multiple files.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Consider a developer who wants to create a registration system.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Traditionally, the developer would manually design the registration interface, implement validation, create the backend endpoint, connect the database, configure password security, and write tests.&lt;/p&gt;

&lt;p&gt;With an AI development assistant, the developer can describe the intended functionality and receive help with several of these tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"Create a secure email and password registration system using PostgreSQL. Include server-side validation, password hashing, duplicate-account prevention, appropriate error handling, and automated tests."&lt;/p&gt;

&lt;p&gt;An AI assistant can use this specification to propose an implementation, generate relevant code, and explain the decisions involved.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;However, generating code is only one part of the process.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A production-ready implementation still needs to be checked against the application's existing architecture, security requirements, database constraints, and expected user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This distinction is important.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generating code is not the same as engineering a reliable software system.&lt;/p&gt;

&lt;p&gt;The real value of AI-assisted development comes from reducing repetitive work while helping developers make better-informed technical decisions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. AI Coding Assistants Versus AI Development Agents&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most important developments in modern software engineering is the emergence of AI agents.&lt;/p&gt;

&lt;p&gt;Although AI coding assistants and AI development agents are sometimes discussed interchangeably, they represent different levels of capability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI coding assistants&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A coding assistant generally responds to developer instructions.&lt;/p&gt;

&lt;p&gt;It can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explain programming concepts.&lt;/li&gt;
&lt;li&gt;Generate functions.&lt;/li&gt;
&lt;li&gt;Suggest improvements.&lt;/li&gt;
&lt;li&gt;Review code.&lt;/li&gt;
&lt;li&gt;Identify potential errors.&lt;/li&gt;
&lt;li&gt;Generate documentation.&lt;/li&gt;
&lt;li&gt;Help developers understand unfamiliar repositories.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The developer typically controls the overall workflow and decides what happens next.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI development agents&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An AI development agent can participate in a longer sequence of tasks.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Depending on its tools and permissions, it may:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Interpret a project requirement.&lt;/li&gt;
&lt;li&gt;Examine the existing codebase.&lt;/li&gt;
&lt;li&gt;Identify relevant files.&lt;/li&gt;
&lt;li&gt;Propose an implementation strategy.&lt;/li&gt;
&lt;li&gt;Modify source files.&lt;/li&gt;
&lt;li&gt;Run available tests.&lt;/li&gt;
&lt;li&gt;Investigate failures.&lt;/li&gt;
&lt;li&gt;Apply corrections.&lt;/li&gt;
&lt;li&gt;Report what changed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The distinction is not simply how much code an AI can generate. It is how effectively it can coordinate work across a complete development process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example,&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;consider the instruction:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;"Add a project favourites feature."&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A basic coding assistant might generate a favourites component.&lt;/p&gt;

&lt;p&gt;A more capable development agent could investigate the existing project architecture, determine where favourites should be stored, implement the required database changes, create backend endpoints, connect the frontend, and test the complete workflow.&lt;/p&gt;

&lt;p&gt;Of course, the quality of this result depends on the agent's tools, context, permissions, and verification capabilities.&lt;/p&gt;

&lt;p&gt;An agent that can modify files but cannot properly inspect the application or execute tests has significant limitations.&lt;/p&gt;

&lt;p&gt;This is why the next generation of AI development environments needs to focus on more than code generation.&lt;/p&gt;

&lt;p&gt;It needs to support the entire engineering workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Natural Language Is Becoming a New Development Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Programming languages remain essential, but natural language is becoming an increasingly important interface for expressing software requirements.&lt;/p&gt;

&lt;p&gt;A developer can describe an intended outcome before deciding precisely how every component should be implemented.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"Build a dashboard that allows users to manage projects, assign tasks, monitor progress, and view recent activities."&lt;/p&gt;

&lt;p&gt;An AI system can interpret this description and help transform it into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functional requirements.&lt;/li&gt;
&lt;li&gt;User interface components.&lt;/li&gt;
&lt;li&gt;Database entities.&lt;/li&gt;
&lt;li&gt;API endpoints.&lt;/li&gt;
&lt;li&gt;Authentication requirements.&lt;/li&gt;
&lt;li&gt;Testing scenarios.&lt;/li&gt;
&lt;li&gt;Deployment considerations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This changes the relationship between ideas and implementation.&lt;/p&gt;

&lt;p&gt;Instead of beginning every task by writing code, developers can begin by describing the problem and reviewing a proposed technical approach.&lt;/p&gt;

&lt;p&gt;However, natural-language instructions introduce another challenge: ambiguity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider the instruction:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;"Build a secure dashboard."&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does secure mean?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Does the application require role-based access control? Should users only access their own records? Are administrative permissions required? Should sessions expire? Is multi-factor authentication necessary?&lt;/p&gt;

&lt;p&gt;An AI system should not silently invent answers to important architectural questions.&lt;/p&gt;

&lt;p&gt;It should identify missing requirements, make assumptions explicit, and request clarification when necessary.&lt;/p&gt;

&lt;p&gt;Good software engineering still requires precise specifications, even when those specifications are written in natural language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. The Importance of Context-Aware Development Environments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One limitation of isolated AI coding tools is that they may not understand the complete application.&lt;/p&gt;

&lt;p&gt;A developer might ask an AI assistant to modify a feature without providing sufficient information about the existing database schema, authentication implementation, shared components, or application architecture.&lt;/p&gt;

&lt;p&gt;The resulting code might look reasonable but conflict with existing functionality.&lt;/p&gt;

&lt;p&gt;This is where context-aware development environments become important.&lt;/p&gt;

&lt;p&gt;A context-aware environment should understand relevant parts of a project, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Directory structure.&lt;/li&gt;
&lt;li&gt;Programming languages.&lt;/li&gt;
&lt;li&gt;Frameworks.&lt;/li&gt;
&lt;li&gt;Dependencies.&lt;/li&gt;
&lt;li&gt;Database schemas.&lt;/li&gt;
&lt;li&gt;Existing API contracts.&lt;/li&gt;
&lt;li&gt;Authentication architecture.&lt;/li&gt;
&lt;li&gt;Testing conventions.&lt;/li&gt;
&lt;li&gt;Configuration requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It should also understand the difference between creating something new and modifying something that already exists.&lt;/p&gt;

&lt;p&gt;For established applications, this distinction is particularly important.&lt;/p&gt;

&lt;p&gt;Developers should not have to rebuild an entire application whenever they introduce a new feature.&lt;/p&gt;

&lt;p&gt;Instead, AI systems should inspect the existing implementation, identify the appropriate integration points, and make focused changes.&lt;/p&gt;

&lt;p&gt;This approach supports incremental development and reduces unnecessary architectural disruption.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Geminio OS: Exploring an AI-First Development Ecosystem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My interest in this transformation led me to develop Geminio OS.&lt;/p&gt;

&lt;p&gt;Geminio OS is an AI-first full-stack development ecosystem built around a broader vision: bringing intelligent development, automation, and software creation into a unified environment.&lt;/p&gt;

&lt;p&gt;Rather than treating AI as an isolated chat interface, the project explores how AI capabilities can connect with different parts of the development experience.&lt;/p&gt;

&lt;p&gt;The underlying idea is straightforward.&lt;/p&gt;

&lt;p&gt;A developer should be able to describe an idea, explore its requirements, work through implementation, inspect the resulting application, test its functionality, and prepare it for deployment without constantly switching between disconnected experiences.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From an idea to a software project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A useful AI development environment should begin with understanding the user's objective.&lt;/p&gt;

&lt;p&gt;Suppose someone wants to build an online learning platform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The initial instruction might be:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"I want to create an online learning platform where instructors can publish courses and students can enrol, complete lessons, and track their progress."&lt;/p&gt;

&lt;p&gt;An AI-powered workflow should help transform that idea into a structured project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It should identify:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Project requirements&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instructor accounts.&lt;/li&gt;
&lt;li&gt;Student accounts.&lt;/li&gt;
&lt;li&gt;Course management.&lt;/li&gt;
&lt;li&gt;Lesson publishing.&lt;/li&gt;
&lt;li&gt;Student enrolment.&lt;/li&gt;
&lt;li&gt;Learning progress tracking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Technical architecture&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frontend framework.&lt;/li&gt;
&lt;li&gt;Backend services.&lt;/li&gt;
&lt;li&gt;Database structure.&lt;/li&gt;
&lt;li&gt;Authentication.&lt;/li&gt;
&lt;li&gt;File storage.&lt;/li&gt;
&lt;li&gt;API design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation plan&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the project foundation.&lt;/li&gt;
&lt;li&gt;Implement authentication.&lt;/li&gt;
&lt;li&gt;Build course management.&lt;/li&gt;
&lt;li&gt;Add student enrolment.&lt;/li&gt;
&lt;li&gt;Implement progress tracking.&lt;/li&gt;
&lt;li&gt;Create automated tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The developer should be able to review this plan before implementation begins.&lt;/p&gt;

&lt;p&gt;This is important because an incorrect architectural decision made at the beginning can create unnecessary problems throughout the project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI Builder and interactive application previews&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another area I consider important is the relationship between generated code and the resulting application.&lt;/p&gt;

&lt;p&gt;A development environment should not simply produce source files and expect the developer to understand everything from the code alone.&lt;/p&gt;

&lt;p&gt;It should provide an interactive preview where the developer can inspect the application as it develops.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Imagine a workspace with two primary areas.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The first area displays the application preview. The second provides the development conversation, tools, and project controls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A developer could request:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"Change the dashboard navigation to include Projects, Analytics, Settings, and Team Members."&lt;/p&gt;

&lt;p&gt;The AI system would process the request, make the relevant source-code changes, and refresh the preview.&lt;/p&gt;

&lt;p&gt;The developer could then interact with the application, navigate between screens, and test the resulting experience.&lt;/p&gt;

&lt;p&gt;This creates a more direct feedback loop between requirements, implementation, and verification.&lt;/p&gt;

&lt;p&gt;Importantly, an interactive preview should reflect actual application code rather than a disconnected visual mock-up.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;The importance of preserving existing work&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One principle I consider essential when designing AI-powered development tools is preservation.&lt;/p&gt;

&lt;p&gt;Developers often work on applications containing hundreds or thousands of files, established database structures, existing integrations, and previously tested functionality.&lt;/p&gt;

&lt;p&gt;An AI agent should not assume that a project needs to be rebuilt whenever a new feature is requested.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instead, it should:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inspect the existing implementation.&lt;/li&gt;
&lt;li&gt;Identify the relevant components.&lt;/li&gt;
&lt;li&gt;Understand dependencies.&lt;/li&gt;
&lt;li&gt;Plan the smallest appropriate change.&lt;/li&gt;
&lt;li&gt;Modify the required files.&lt;/li&gt;
&lt;li&gt;Run relevant tests.&lt;/li&gt;
&lt;li&gt;Report affected functionality.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This makes AI-assisted development more practical for real-world projects.&lt;/p&gt;

&lt;p&gt;The objective is not to replace an existing engineering foundation with newly generated code.&lt;/p&gt;

&lt;p&gt;It is to improve that foundation without unnecessarily disrupting it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why integration matters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An AI development ecosystem becomes more useful when its capabilities work together.&lt;/p&gt;

&lt;p&gt;For example, an AI chat interface could act as a central entry point.&lt;/p&gt;

&lt;p&gt;If a user requests a website, the system could direct the user into the website-building workflow.&lt;/p&gt;

&lt;p&gt;If the user requests an image, it could open the image-generation experience.&lt;/p&gt;

&lt;p&gt;If the user requests a video, it could direct them to the appropriate video-generation or editing environment.&lt;/p&gt;

&lt;p&gt;If the user requests documentation, it could initiate a technical-writing workflow.&lt;/p&gt;

&lt;p&gt;This creates a universal AI interface that connects specialised tools rather than forcing users to discover every feature independently.&lt;/p&gt;

&lt;p&gt;That is one of the broader ideas behind Geminio OS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. AI-Powered Testing and Quality Assurance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most promising applications of AI in software engineering is testing.&lt;/p&gt;

&lt;p&gt;Generating an application quickly is useful, but generating an application that behaves correctly is considerably more important.&lt;/p&gt;

&lt;p&gt;AI can support quality assurance in several ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test generation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given a function or API endpoint, an AI assistant can propose test cases covering expected behaviour and potential edge cases.&lt;/p&gt;

&lt;p&gt;For example, a registration endpoint might require tests for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Valid registration.&lt;/li&gt;
&lt;li&gt;Invalid email addresses.&lt;/li&gt;
&lt;li&gt;Missing required fields.&lt;/li&gt;
&lt;li&gt;Duplicate accounts.&lt;/li&gt;
&lt;li&gt;Weak passwords.&lt;/li&gt;
&lt;li&gt;Unexpected server errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These tests help establish whether the implementation behaves as intended.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error analysis&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a test fails, AI can help interpret the error message, inspect relevant code, and identify possible causes.&lt;/p&gt;

&lt;p&gt;However, an error message does not always reveal the actual underlying problem.&lt;/p&gt;

&lt;p&gt;A failed database test, for example, might originate from an incorrect query, a missing migration, an environment configuration issue, or an unexpected test fixture.&lt;/p&gt;

&lt;p&gt;The agent must investigate rather than immediately assume that the first apparent cause is correct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Regression testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Regression testing verifies that existing functionality continues working after changes are introduced.&lt;/p&gt;

&lt;p&gt;This is particularly important for AI-generated modifications.&lt;/p&gt;

&lt;p&gt;A new authentication feature should not accidentally break account recovery.&lt;/p&gt;

&lt;p&gt;A new dashboard component should not disrupt existing navigation.&lt;/p&gt;

&lt;p&gt;A database migration should not unintentionally remove or invalidate existing records.&lt;/p&gt;

&lt;p&gt;A reliable AI development workflow should therefore run relevant tests after implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why human verification remains necessary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Passing automated tests does not prove that an application is completely free of defects.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Tests only verify the scenarios they cover.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An application may pass its test suite while still containing usability problems, security weaknesses, performance bottlenecks, or untested edge cases.&lt;/p&gt;

&lt;p&gt;AI should strengthen the testing process, not create false confidence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Security Must Remain a First-Class Engineering Requirement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The growth of AI-generated software introduces important security considerations.&lt;/p&gt;

&lt;p&gt;An AI-generated authentication system may appear complete while missing important protections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example,&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;developers must consider:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Secure password hashing.&lt;/li&gt;
&lt;li&gt;Session management.&lt;/li&gt;
&lt;li&gt;Authorization checks.&lt;/li&gt;
&lt;li&gt;Input validation.&lt;/li&gt;
&lt;li&gt;Rate limiting.&lt;/li&gt;
&lt;li&gt;Protection against common web vulnerabilities.&lt;/li&gt;
&lt;li&gt;Secure handling of environment variables.&lt;/li&gt;
&lt;li&gt;Database access controls.&lt;/li&gt;
&lt;li&gt;Logging and monitoring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI systems can help identify security concerns, but their recommendations must be reviewed and tested.&lt;/p&gt;

&lt;p&gt;A particularly important distinction is the difference between authentication and authorization.&lt;/p&gt;

&lt;p&gt;Authentication establishes who a user is.&lt;/p&gt;

&lt;p&gt;Authorization determines what that user is allowed to do.&lt;/p&gt;

&lt;p&gt;An application can successfully authenticate users while still exposing data because its authorization rules are incomplete.&lt;/p&gt;

&lt;p&gt;For example, a student should not automatically gain access to another student's private learning records simply because both users have valid accounts.&lt;/p&gt;

&lt;p&gt;AI-generated backend code must enforce these boundaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Protecting secrets and credentials&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI development environments may interact with repositories, external services, deployment systems, and databases.&lt;/p&gt;

&lt;p&gt;These integrations introduce additional risks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;API keys,&lt;/strong&gt; database credentials, access tokens, and private configuration values must never be exposed unnecessarily.&lt;/p&gt;

&lt;p&gt;Development agents should use controlled permissions, secure secret management, and clear approval boundaries for sensitive operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The principle should be simple:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An AI agent should receive only the access it needs to complete an authorised task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. AI and the Future of Developer Documentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Documentation is often treated as a secondary activity in software development.&lt;/p&gt;

&lt;p&gt;However, documentation is essential to the long-term usability and maintainability of software.&lt;/p&gt;

&lt;p&gt;A technically excellent application can still be difficult to use if developers cannot understand its architecture, APIs, configuration, or intended behaviour.&lt;/p&gt;

&lt;p&gt;This is one reason I believe AI-assisted documentation deserves more attention.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI can help generate:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API reference documentation.&lt;br&gt;
Installation guides.&lt;br&gt;
Configuration instructions.&lt;br&gt;
Architecture explanations.&lt;br&gt;
Code examples.&lt;br&gt;
Troubleshooting guides.&lt;br&gt;
Developer onboarding materials.&lt;br&gt;
Release notes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example,&lt;/strong&gt; when a backend endpoint is introduced, an AI system can help document its purpose, accepted parameters, authentication requirements, response structure, and possible errors.&lt;/p&gt;

&lt;p&gt;However, generated documentation must accurately reflect the implementation.&lt;/p&gt;

&lt;p&gt;An API document describing an endpoint that does not exist is worse than having no documentation at all.&lt;/p&gt;

&lt;p&gt;The same applies to code examples, configuration instructions, and architectural explanations.&lt;/p&gt;

&lt;p&gt;A strong documentation workflow should derive information from the actual implementation, validate examples where possible, and flag assumptions that require human review.&lt;/p&gt;

&lt;p&gt;Documentation as part of development&lt;br&gt;
Rather than waiting until the end of a project, documentation should be integrated into the development lifecycle.&lt;/p&gt;

&lt;p&gt;When a developer changes an API, the associated documentation should be reviewed.&lt;/p&gt;

&lt;p&gt;When a database schema changes, relevant architecture descriptions should be updated.&lt;/p&gt;

&lt;p&gt;When a configuration requirement changes, installation instructions should reflect the new requirement.&lt;br&gt;
This approach reduces documentation drift and makes software easier to maintain.&lt;/p&gt;

&lt;p&gt;For developers working with complex systems, documentation is not merely a publishing activity.&lt;br&gt;
It is part of the engineering process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. The Challenges of Autonomous Software Engineering&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Despite its potential, AI-driven development still has important limitations.&lt;/p&gt;

&lt;p&gt;Understanding these limitations is necessary for building reliable systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hallucinated implementations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI models can produce code that looks convincing but contains incorrect assumptions, nonexistent APIs, or unsuitable implementation patterns.&lt;/p&gt;

&lt;p&gt;Developers must verify generated code against actual libraries, framework documentation, and application requirements.&lt;/p&gt;

&lt;p&gt;Architectural inconsistency&lt;br&gt;
An AI agent working without sufficient project context may introduce competing patterns.&lt;br&gt;
One feature might use a particular validation library while another introduces an unnecessary alternative.&lt;/p&gt;

&lt;p&gt;Over time, these inconsistencies increase maintenance complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incomplete testing&lt;/strong&gt;&lt;br&gt;
An AI agent may report that an implementation is complete even when important workflows have not been tested.&lt;/p&gt;

&lt;p&gt;A successful build is not equivalent to a successful application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uncontrolled modifications&lt;/strong&gt;&lt;br&gt;
Giving an agent unrestricted access to a large codebase can create unnecessary changes.&lt;/p&gt;

&lt;p&gt;Agents should operate with clear scopes, controlled permissions, change tracking, and review mechanisms.&lt;/p&gt;

&lt;p&gt;Dependency and version problems&lt;br&gt;
AI-generated implementations can rely on outdated packages, incompatible versions, or incorrect configuration assumptions.&lt;/p&gt;

&lt;p&gt;Developers must verify dependency compatibility and monitor relevant security advisories.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overdependence on automation&lt;/strong&gt;&lt;br&gt;
Developers who rely entirely on generated solutions without understanding the underlying systems may struggle to identify problems when something goes wrong.&lt;/p&gt;

&lt;p&gt;AI should make developers more capable, not remove the need to understand software engineering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11. The Human Developer Is Still Essential&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is considerable discussion about whether AI will replace software developers.&lt;br&gt;
I believe a more useful question is how the developer's responsibilities will change.&lt;/p&gt;

&lt;p&gt;As AI handles more repetitive implementation work, developers can devote more attention to:&lt;/p&gt;

&lt;p&gt;Understanding user problems.&lt;br&gt;
Designing system architecture.&lt;br&gt;
Evaluating technical trade-offs.&lt;br&gt;
Reviewing security.&lt;br&gt;
Improving performance.&lt;br&gt;
Managing complexity.&lt;br&gt;
Validating application behaviour.&lt;br&gt;
Communicating technical decisions.&lt;br&gt;
These responsibilities require judgment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example,&lt;/strong&gt; two different database architectures might both satisfy the immediate requirements of a project.&lt;/p&gt;

&lt;p&gt;Choosing between them requires considering future growth, operational complexity, expected traffic, team expertise, cost, and maintenance requirements.&lt;/p&gt;

&lt;p&gt;AI can help compare these options, but the final decision must be grounded in the project's actual needs.&lt;/p&gt;

&lt;p&gt;The most effective developers will not necessarily be those who generate the largest amount of code.&lt;br&gt;
They will be those who understand how to use intelligent tools while maintaining control over technical quality.&lt;/p&gt;

&lt;p&gt;The importance of learning fundamentals&lt;br&gt;
AI does not eliminate the importance of programming fundamentals.&lt;br&gt;
Understanding variables, data structures, algorithms, databases, networking, HTTP, security, and system architecture remains valuable.&lt;/p&gt;

&lt;p&gt;Without this knowledge, developers may find it difficult to evaluate generated implementations.&lt;/p&gt;

&lt;p&gt;The ability to read code, understand errors, and reason about system behaviour becomes even more important when AI is involved.&lt;/p&gt;

&lt;p&gt;AI can accelerate learning, but it cannot replace the value of developing a genuine technical foundation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;12. What the Next Generation of Development Platforms Should Look Like&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Looking ahead, I believe AI-first development platforms will increasingly combine capabilities that are currently distributed across multiple tools.&lt;/p&gt;

&lt;p&gt;A mature platform could bring together the following components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Intelligent project planning&lt;/strong&gt;&lt;br&gt;
The system should transform natural-language ideas into structured requirements, identify missing information, and propose implementation plans.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Context-aware code generation&lt;/strong&gt;&lt;br&gt;
Generated code should follow the existing project's architecture, dependencies, conventions, and design patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Interactive development&lt;/strong&gt; environments&lt;br&gt;
Developers should be able to inspect and test actual application previews while changes are being implemented.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Integrated testing&lt;/strong&gt;&lt;br&gt;
Testing should be part of the development workflow rather than an optional activity performed only before deployment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Automated documentation support&lt;/strong&gt;&lt;br&gt;
Documentation should evolve alongside the codebase and remain consistent with the actual implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Secure integrations&lt;/strong&gt;&lt;br&gt;
External services, databases, APIs, and deployment tools should operate through controlled permissions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Project history and collaboration&lt;/strong&gt;&lt;br&gt;
Developers should be able to understand what changed, why it changed, which files were affected, and whether the changes were verified.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Deployment and operational visibility&lt;/strong&gt;&lt;br&gt;
Development should connect naturally to release management, deployment checks, monitoring, and troubleshooting.&lt;/p&gt;

&lt;p&gt;These capabilities describe a broader direction for software engineering.&lt;/p&gt;

&lt;p&gt;Instead of using AI merely as a tool that writes code, developers can work with environments that coordinate multiple stages of software creation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. A Practical Workflow for Developers Adopting AI Today&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Developers do not need to wait for fully autonomous development platforms to benefit from AI.&lt;br&gt;
There are practical ways to integrate AI into existing workflows.&lt;/p&gt;

&lt;p&gt;Here is a process worth considering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Describe the problem clearly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explain&lt;/strong&gt; what the application or feature should accomplish. Identify users, expected behaviour, and important constraints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Request a technical plan.&lt;/p&gt;

&lt;p&gt;Ask the AI assistant to propose the architecture, affected components, database changes, and implementation sequence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Review the plan.&lt;br&gt;
Check whether the proposed solution fits the existing application. Correct incorrect assumptions before implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Implement incrementally.&lt;br&gt;
Avoid requesting an entire complex application in one instruction. Break the work into manageable features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Review generated code.&lt;br&gt;
Inspect security, dependencies, validation, error handling, and consistency with existing conventions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Run tests.&lt;br&gt;
Execute the relevant automated tests and inspect the results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; Verify the application.&lt;br&gt;
Use the actual application to test important workflows. Confirm that the interface, backend, and database work together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; Update documentation.&lt;br&gt;
Ensure that the documentation accurately reflects the implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; Prepare deployment.&lt;br&gt;
Review environment variables, migrations, security settings, and deployment configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 10:&lt;/strong&gt; Maintain the project.&lt;br&gt;
Continue monitoring errors, dependencies, performance, and user feedback.&lt;/p&gt;

&lt;p&gt;This workflow allows developers to benefit from AI without surrendering engineering control.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjofnah7dg2vsmx1qshzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjofnah7dg2vsmx1qshzc.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;14. Final Thoughts: Software Engineering Is Becoming More Intelligent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The future of software development will not be defined by code generation alone.&lt;/p&gt;

&lt;p&gt;It will be defined by how effectively intelligent systems understand requirements, work with existing codebases, coordinate development activities, verify implementations, and support developers throughout the software lifecycle.&lt;/p&gt;

&lt;p&gt;AI is changing how we approach programming, but the fundamental objectives of software engineering remain the same: building reliable, secure, maintainable, and useful systems.&lt;/p&gt;

&lt;p&gt;My work on Geminio OS has reinforced my interest in this direction. Exploring an AI-first development ecosystem has made me think beyond individual coding assistants and toward environments where planning, implementation, testing, documentation, and deployment can operate as connected parts of one development experience.&lt;br&gt;
There are still difficult problems to solve. Reliability, security, architectural consistency, context management, and meaningful human oversight remain essential.&lt;br&gt;
But the opportunity is significant.&lt;br&gt;
We are moving toward a development environment in which developers can spend less time coordinating repetitive tasks and more time understanding problems, designing solutions, and creating meaningful technology.&lt;/p&gt;

&lt;p&gt;I do not believe the future belongs to AI working without developers.&lt;/p&gt;

&lt;p&gt;I believe it belongs to developers who understand how to work effectively with AI.&lt;/p&gt;

&lt;p&gt;The future of software development is not simply about writing code faster. It is about building better software through intelligent collaboration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the Author&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ezenwa Christian Chimeremeze&lt;/strong&gt; I am a developer, technical writer, and founder of Geminio OS, an AI-first full-stack development ecosystem. I Has interesting include artificial intelligence, software engineering, developer documentation, automation, and the future of intelligent development environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connect with me:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/ezenwachristianchimeremeze" rel="noopener noreferrer"&gt;https://github.com/ezenwachristianchimeremeze&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Portfolio:&lt;/strong&gt; &lt;a href="https://ezenwa-christian.netlify.app" rel="noopener noreferrer"&gt;https://ezenwa-christian.netlify.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Medium:&lt;/strong&gt; &lt;a href="https://ezenwachristianchimeremeze.medium.com/" rel="noopener noreferrer"&gt;https://ezenwachristianchimeremeze.medium.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LinkedIn profile&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/ezenwa-christian-chimeremeze-b77115440" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/ezenwa-christian-chimeremeze-b77115440&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;X profile&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://x.com/EZENWACHRIoy" rel="noopener noreferrer"&gt;https://x.com/EZENWACHRIoy&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
      <category>python</category>
    </item>
    <item>
      <title>Building a Production-Ready AI Agent with FastAPI and Google Gemini</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sun, 15 Nov 2026 12:54:04 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/building-a-production-ready-ai-agent-with-fastapi-and-google-gemini-mp1</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/building-a-production-ready-ai-agent-with-fastapi-and-google-gemini-mp1</guid>
      <description>&lt;p&gt;Most tutorials teach you how to build a simple AI chatbot. But building an AI agent that can take real actions, use tools, and maintain state in production requires a completely different architecture. Here is how I built one.&lt;/p&gt;

&lt;p&gt;Whe&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcgvlxbrqddx5479r5zwh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcgvlxbrqddx5479r5zwh.jpg" alt=" " width="800" height="447"&gt;&lt;/a&gt;n I set out to build my AI Career Agency app, I quickly realized that standard LLM wrappers (like a simple ChatGPT clone) were not enough.&lt;/p&gt;

&lt;p&gt;I needed an agent. I needed an AI that could read a job description, decide if it was a good fit, use a tool to fetch my database records, and generate a tailored cover letter.&lt;/p&gt;

&lt;p&gt;To achieve this, I used FastAPI for the backend, Google Gemini for the reasoning engine, and SQLite for state management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;##Here is the complete architectural breakdown of how to build a production-ready AI agent.##&lt;/strong&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;The Architecture: From Chatbot to Agent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A standard chatbot is stateless and relies entirely on the prompt. An agent is stateful and uses tools.&lt;/p&gt;

&lt;p&gt;In our architecture, the flow looks like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user sends a query to our FastAPI endpoint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FastAPI&lt;/strong&gt; retrieves the conversation history from SQLite.&lt;/li&gt;
&lt;li&gt;The history and the new query are sent to the Gemini API.&lt;/li&gt;
&lt;li&gt;If Gemini determines it needs external data (e.g., "What jobs are in my database?"), it uses Function Calling to trigger a Python function in our backend.&lt;/li&gt;
&lt;li&gt;FastAPI executes the function, returns the data to Gemini, and Gemini generates the final answer.&lt;/li&gt;
&lt;li&gt;The new conversation is saved back to SQLite.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Here is how to implement this step-by-step.&lt;/strong&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Step 1: Setting up the Environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, we need our dependencies. We will use fastapi, uvicorn, and the official Google GenAI SDK.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install fastapi uvicorn google-genai python-dotenv sqlite3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a .env file to store your Gemini API key securely. Never hardcode this in your Python file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GEMINI_API_KEY="your_api_key_here"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Step 2: Defining the Database (State Management)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An AI agent needs memory. If you don't store the conversation history, the AI will forget what you just said. For a mobile or lightweight deployment, SQLite is perfect.&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;sqlite3&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;init_db&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;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'agent_memory.db'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'''
        CREATE TABLE IF NOT EXISTS chat_history (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            session_id TEXT,
            role TEXT,
            content TEXT
        )
    '''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&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="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'agent_memory.db'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"INSERT INTO chat_history (session_id, role, content) VALUES (?, ?, ?)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&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="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_history&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'agent_memory.db'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"SELECT role, content FROM chat_history WHERE session_id = ? ORDER BY id ASC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session_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="n"&gt;history&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&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="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;history&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Step 3: Defining the Tools (Function Calling)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where the magic happens. We need to create a Python function that the AI is allowed to use.&lt;/p&gt;

&lt;p&gt;Let's create a fake tool that checks the status of a job application in our database.&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;check_job_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;company_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""
    Checks the application status for a specific company.
    """&lt;/span&gt;
    &lt;span class="c1"&gt;# In a real app, this queries your SQLite database.&lt;/span&gt;
    &lt;span class="c1"&gt;# For this example, we use a mock response.&lt;/span&gt;
    &lt;span class="n"&gt;mock_database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"google"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Applied - Waiting for response"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"airbyte"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Interview Scheduled"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"smashing magazine"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Offer Received"&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="n"&gt;mock_database&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;company_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&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="n"&gt;status&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;f&lt;/span&gt;&lt;span class="s2"&gt;"The status for {company_name} is: {status}."&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="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"No application found for {company_name}."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Step 4: Integrating Gemini with FastAPI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's connect the Gemini SDK to our FastAPI endpoint. We will pass the check_job_status function to Gemini so it knows the tool exists.&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;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseModel&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;genai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;load_dotenv&lt;/span&gt;

&lt;span class="nf"&gt;load_dotenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"GEMINI_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChatRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chat_with_agent&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="nc"&gt;ChatRequest&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. Save the user's message to SQLite&lt;/span&gt;
    &lt;span class="nf"&gt;save_message&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="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"user"&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="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# 2. Retrieve history&lt;/span&gt;
    &lt;span class="n"&gt;history&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_history&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="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# 3. Format history for Gemini&lt;/span&gt;
    &lt;span class="n"&gt;contents&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;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Gemini uses "user" and "model" roles&lt;/span&gt;
        &lt;span class="n"&gt;gemini_role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"model"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"assistant"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;
        &lt;span class="n"&gt;contents&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="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;gemini_role&lt;/span&gt;&lt;span class="p"&gt;,&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;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Part&lt;/span&gt;&lt;span class="p"&gt;(&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;content&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# 4. Call Gemini with the tool defined&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'gemini-2.5-flash'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;check_job_status&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Pass the function!&lt;/span&gt;
                &lt;span class="n"&gt;system_instruction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"You are a helpful career assistant. Use the tool provided to check job statuses if the user asks."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.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. Save the AI's response to SQLite&lt;/span&gt;
        &lt;span class="n"&gt;ai_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;
        &lt;span class="nf"&gt;save_message&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="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"assistant"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ai_message&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;"response"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ai_message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;str&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="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Step 5: The Secret Sauce (Asynchronous Streaming)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are building an AI agent, waiting 10 seconds for a full response feels terrible to the user. You need to stream the response chunk-by-chunk, just like ChatGPT does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;##Here is how you modify the FastAPI endpoint to stream the Gemini response:##&lt;/strong&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;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;responses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;StreamingResponse&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/chat/stream"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chat_with_agent_stream&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="nc"&gt;ChatRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;save_message&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="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"user"&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="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;history&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_history&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="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"model"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"assistant"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&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;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Part&lt;/span&gt;&lt;span class="p"&gt;(&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;content&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;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;history&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_response&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;full_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
        &lt;span class="c1"&gt;# Use the async streaming method&lt;/span&gt;
        &lt;span class="n"&gt;response_stream&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;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content_stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'gemini-2.5-flash'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;check_job_status&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;async&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response_stream&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;chunk&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="n"&gt;full_response&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;
                &lt;span class="c1"&gt;# Yield the chunk to the frontend immediately&lt;/span&gt;
                &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;

        &lt;span class="c1"&gt;# Save the complete message to the database after streaming finishes&lt;/span&gt;
        &lt;span class="nf"&gt;save_message&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="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"assistant"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;full_response&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;StreamingResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generate_response&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;media_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"text/event-stream"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This Architecture Works in Production&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Deterministic Math:&lt;/strong&gt; Because Gemini uses the Python tool check_job_status to fetch the data, the AI doesn't have to guess or hallucinate the status. It relies on the Python code to return the truth.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Stateful Memory:&lt;/strong&gt; SQLite ensures that even if the server restarts, the user's chat history remains intact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Low Latency:&lt;/strong&gt; The StreamingResponse makes the application feel instantly fast, even if Gemini takes a few seconds to finish thinking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Building an AI agent is not about writing a perfect prompt. It is about building a solid backend architecture where the LLM acts as a reasoning engine, while Python does the heavy lifting.&lt;/p&gt;

&lt;p&gt;By combining FastAPI, Gemini, and SQLite, you can build agents that are fast, reliable, and ready for real-world users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;##Have you started building AI agents yet? Let me know your thoughts in the comments below!##&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>fastapi</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>📝How to Dockerize a FastAPI Application</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sun, 15 Nov 2026 12:36:32 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/how-to-dockerize-a-fastapi-application-4bop</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/how-to-dockerize-a-fastapi-application-4bop</guid>
      <description>&lt;p&gt;Have you ever heard a developer say, "But it works on my machine!"&lt;/p&gt;

&lt;p&gt;That is the exact problem Docker was built to solve. If you are building a FastAPI application, you need a way to package it with all its dependencies so it runs exactly the same way on your laptop, your teammate's laptop, and the production server.&lt;/p&gt;

&lt;p&gt;Here is how to Dockerize a FastAPI app in three simple steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create your requirements.txt&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, make sure your project has a requirements.txt file listing all your Python packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fastapi==0.100.0
uvicorn==0.23.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Write the Dockerfile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Dockerfile is a text document that contains all the commands to assemble an image. Create a file named Dockerfile in the root of your project (no file extension).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use the official lightweight Python image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; python:3.10-slim

&lt;span class="c"&gt;# Set the working directory in the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /code

&lt;span class="c"&gt;# Copy the requirements file first (this helps with caching)&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; ./requirements.txt /code/requirements.txt

&lt;span class="c"&gt;# Install the dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN&lt;/span&gt; pip install --no-cache-dir --upgrade -r /code/requirements.txt

&lt;span class="c"&gt;# Copy the rest of the application code&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; ./app /code/app

&lt;span class="c"&gt;# Command to run the application using Uvicorn&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"uvicorn"&lt;/span&gt;, &lt;span class="s2"&gt;"app.main:app"&lt;/span&gt;, &lt;span class="s2"&gt;"--host"&lt;/span&gt;, &lt;span class="s2"&gt;"0.0.0.0"&lt;/span&gt;, &lt;span class="s2"&gt;"--port"&lt;/span&gt;, &lt;span class="s2"&gt;"80"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Build and Run&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that you have your Dockerfile, you can build the image and run the container. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build -t my-fastapi-app .
docker run -d --name my-running-app -p 8080:80 my-fastapi-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;· -d runs the container in the background.&lt;br&gt;
· -p 8080:80 maps port 8080 on your machine to port 80 inside the container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is a game-changer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you look closely at the Dockerfile, the --host 0.0.0.0 flag is crucial. If you leave it as 127.0.0.1, the server will only listen to traffic inside the container, and your browser won't be able to reach it.&lt;/p&gt;

&lt;p&gt;Once you start using Docker, you never have to worry about dependency conflicts again.&lt;/p&gt;

&lt;p&gt;Have you started using Docker for your Python projects yet? Let me know!&lt;/p&gt;

</description>
      <category>backend</category>
      <category>docker</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>📝 How to Dockerize a FastAPI Application (A Beginner's Guide)</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sun, 15 Nov 2026 12:21:40 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/how-to-dockerize-a-fastapi-application-a-beginners-guide-3p78</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/how-to-dockerize-a-fastapi-application-a-beginners-guide-3p78</guid>
      <description>&lt;p&gt;Have you ever heard a developer say, "But it works on my machine!"&lt;/p&gt;

&lt;p&gt;That is the exact problem Docker was built to solve. If you are building a FastAPI application, you need a way to package it with all its dependencies, environment variables, and OS libraries so it runs exactly the same way on your laptop, your teammate's laptop, and the production server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is how to Dockerize a FastAPI app in three simple steps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create your requirements.txt&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, make sure your project has a requirements.txt file listing all your Python packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fastapi==0.100.0
uvicorn==0.23.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Write the Dockerfile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Create a file named Dockerfile in the root of your project (no file extension).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use the official lightweight Python image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; python:3.10-slim

&lt;span class="c"&gt;# Set the working directory in the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /code

&lt;span class="c"&gt;# Copy the requirements file first (this helps with caching)&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; ./requirements.txt /code/requirements.txt

&lt;span class="c"&gt;# Install the dependencies&lt;/span&gt;
&lt;span class="k"&gt;RUN&lt;/span&gt; pip install --no-cache-dir --upgrade -r /code/requirements.txt

&lt;span class="c"&gt;# Copy the rest of the application code&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; ./app /code/app

&lt;span class="c"&gt;# Command to run the application using Uvicorn&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"uvicorn"&lt;/span&gt;, &lt;span class="s2"&gt;"app.main:app"&lt;/span&gt;, &lt;span class="s2"&gt;"--host"&lt;/span&gt;, &lt;span class="s2"&gt;"0.0.0.0"&lt;/span&gt;, &lt;span class="s2"&gt;"--port"&lt;/span&gt;, &lt;span class="s2"&gt;"80"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Build and Run&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that you have your Dockerfile, you can build the image and run the container. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build -t my-fastapi-app .
docker run -d --name my-running-app -p 8080:80 my-fastapi-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;· -d runs the container in the background.&lt;br&gt;
· -p 8080:80 maps port 8080 on your machine to port 80 inside the container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is a game-changer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you look closely at the Dockerfile, the --host 0.0.0.0 flag is crucial. If you leave it as 127.0.0.1, the server will only listen to traffic inside the container, and your browser won't be able to reach it.&lt;/p&gt;

&lt;p&gt;Once you start using Docker, you never have to worry about dependency conflicts again.&lt;/p&gt;

&lt;p&gt;Have you started using Docker for your Python projects yet? Let me know!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>docker</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Handle API Rate Limits in Python Like a Pro (with Exponential Backoff)</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sun, 15 Nov 2026 12:11:07 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff-2k7</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/how-to-handle-api-rate-limits-in-python-like-a-pro-with-exponential-backoff-2k7</guid>
      <description>&lt;p&gt;If you are building applications that rely on external APIs (like OpenAI, Gemini, GitHub, or even just fetching job data), you have probably run into the dreaded 429 Too Many Requests error.&lt;/p&gt;

&lt;p&gt;When I was building my AI backend, my server kept crashing because I was hammering the Gemini API too fast.&lt;/p&gt;

&lt;p&gt;If you just write a try/except block and instantly try again, you will get banned. You need Exponential Backoff. Here is how it works and how to build it in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Exponential Backoff?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of retrying a failed request immediately, you wait a little bit. If it fails again, you wait twice as long. If it fails again, you wait twice as long as that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· Attempt 1 fails -&amp;gt; Wait 1 second.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;· Attempt 2 fails -&amp;gt; Wait 2 seconds.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;· Attempt 3 fails -&amp;gt; Wait 4 seconds.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;· Attempt 4 fails -&amp;gt; Wait 8 seconds.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This gives the API server time to breathe and clear its queue, making your request much more likely to succeed on the next try.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Python Code (Using the tenacity Library)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You could write this logic from scratch using a while loop, but why do that when Python has an incredible library called tenacity?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, install it:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install tenacity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now, let's build a robust API request function:&lt;/strong&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;import&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tenacity&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop_after_attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wait_exponential&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry_if_exception_type&lt;/span&gt;

&lt;span class="c1"&gt;# We only want to retry if we hit a 429 (Rate Limit) or 500 (Server Error)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_rate_limit_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exception&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;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HTTPStatusError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&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;429&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="nd"&gt;@retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;retry&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;retry_if_exception_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HTTPStatusError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;wait_exponential&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiplier&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;min&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="nb"&gt;max&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;stop_after_attempt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fetch_external_data&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="kt"&gt;str&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;f&lt;/span&gt;&lt;span class="s2"&gt;"Attempting to fetch: {url}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AsyncClient&lt;/span&gt;&lt;span class="p"&gt;(&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;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&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;client&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;url&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="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# Raise an exception if the status code is 4xx or 5xx&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise_for_status&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;response&lt;/span&gt;&lt;span class="p"&gt;.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What does this code do?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· wait_exponential(multiplier=1, min=2, max=10):&lt;/strong&gt; This is the magic. It will wait 2s, then 4s, then 8s, capping at 10s so you don't wait forever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· stop_after_attempt(5):&lt;/strong&gt; It will try 5 times before finally giving up and raising the error to your main application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus Tip: Respect the Retry-After Header&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If an API is really nice, it will send a Retry-After header in the 429 response telling you exactly how many seconds to wait. Tenacity can be configured to read this, but even with a standard exponential backoff, your server will stop crashing and start handling failures gracefully.&lt;/p&gt;

&lt;p&gt;Have you ever had your server crash from an API rate limit? Let me know below!&lt;/p&gt;

</description>
      <category>api</category>
      <category>programming</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>📝 3 FastAPI Mistakes I Made (So You Don't Have To)</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sat, 14 Nov 2026 23:01:09 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/3-fastapi-mistakes-i-made-so-you-dont-have-to-3ejg</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/3-fastapi-mistakes-i-made-so-you-dont-have-to-3ejg</guid>
      <description>&lt;p&gt;When I built my first production backend with FastAPI, I thought I was doing everything right. The code ran locally, the endpoints returned 200 OK, and the server started up without any errors.&lt;/p&gt;

&lt;p&gt;But when I deployed it, things broke. FastAPI is incredibly powerful, but its async nature means it's easy to shoot yourself in the foot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are three mistakes I made(and how you can avoid them).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: Using time.sleep() instead of asyncio.sleep()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was my biggest mistake. I was writing an endpoint that had to wait for an external API to respond, so I used time.sleep(2).&lt;/p&gt;

&lt;p&gt;What happened? The entire server froze. FastAPI is built on asynchronous Python. When you use blocking code like time.sleep(), it blocks the event loop, meaning no other user can make a request to your API while that one request is waiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Fix:&lt;/strong&gt;&lt;br&gt;
Always use asyncio.sleep() inside an async def route. Better yet, use an async HTTP client like httpx instead of the standard requests library.&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;asyncio&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;

&lt;span class="nd"&gt;@app&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;"/external-data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_data&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;# Use async HTTP requests&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AsyncClient&lt;/span&gt;&lt;span class="p"&gt;(&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;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&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;client&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;"https://api.example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Use asyncio.sleep, not time.sleep&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mistake 2: Not using Pydantic for Output Validation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When I first started, I just returned raw dictionaries from my endpoints.&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="c1"&gt;# Don't do this&lt;/span&gt;
&lt;span class="nd"&gt;@app&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;"/user"&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;get_user&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="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Ezenwa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"secret123"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem is that you might accidentally leak sensitive data (like passwords) or return the wrong data types, which breaks your frontend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Fix:&lt;/strong&gt;&lt;br&gt;
Create a Pydantic response_model to strictly define what the client receives. This strips out any extra data and validates the output automatically.&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;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseModel&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;

&lt;span class="nd"&gt;@app&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;"/user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response_model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;UserResponse&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;get_user&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;# Even if you return more data, FastAPI will only send name and age&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;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Ezenwa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"secret123"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mistake 3: Hardcoding Secrets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is so tempting to just write SECRET_KEY = "my_super_secret_key_123" directly in your code. But if you push that to GitHub, bots will scrape it in seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Fix:&lt;/strong&gt;&lt;br&gt;
Use a .env file and the python-dotenv library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Then load it in your app:&lt;/strong&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;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;load_dotenv&lt;/span&gt;

&lt;span class="nf"&gt;load_dotenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SECRET_KEY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FastAPI is fast, but you have to respect its async rules and strict typing. If you avoid blocking the event loop and use Pydantic to validate both inputs and outputs, your API will be significantly more stable.&lt;/p&gt;

&lt;p&gt;Have you made any of these mistakes? Let me know in the comments!&lt;/p&gt;

</description>
      <category>backend</category>
      <category>fastapi</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>📝 How to Use SQLite in Python (Without Installing a Database Server)</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sat, 14 Nov 2026 12:38:53 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/how-to-use-sqlite-in-python-without-installing-a-database-server-17g9</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/how-to-use-sqlite-in-python-without-installing-a-database-server-17g9</guid>
      <description>&lt;p&gt;When developers think of databases, they usually think of PostgreSQL, MySQL, or MongoDB. They think of servers, ports, and complex installations.&lt;/p&gt;

&lt;p&gt;But if you are building a lightweight app, a local script, or an AI tool on constrained hardware (like an Android phone via Termux), you don't need a database server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You need SQLite.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQLite is a C library that provides a lightweight disk-based database. It doesn't require a separate server process, and it allows you to access the database using a nonstandard variant of SQL. Best of all? It is built directly into Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is how to use it in 30 seconds.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The Built-in Module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because sqlite3 is part of the Python Standard Library, you do not need to pip install anything. Just import it.&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;sqlite3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Connecting and Creating a Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you connect to a database file, if the file doesn't exist, SQLite will automatically create it for you.&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="c1"&gt;# Connect to the database (creates 'app.db' if it doesn't exist)&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.db'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create a table&lt;/span&gt;
&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'''
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        email TEXT NOT NULL UNIQUE
    )
'''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save (commit) the changes&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&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;strong&gt;3. Inserting Data Safely&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Never use f-strings to insert data into SQL! It leaves you open to SQL injection attacks. Always use the ? placeholder.&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="c1"&gt;# Insert a new user&lt;/span&gt;
&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'''
    INSERT INTO users (name, email) VALUES (?, ?)
'''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Ezenwa'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ezenwa@example.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&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;strong&gt;4. Fetching Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once your data is saved, you can query it using fetchall() or fetchone().&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;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'SELECT id, name, email FROM users'&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="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&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;row&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="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Always Close the Connection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you are done with your script, make sure to close the connection to free up system resources.&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;conn&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="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why use SQLite?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are building a mobile app, a CLI tool, or an AI script running on a local machine, SQLite is perfect. It is zero-configuration, incredibly fast for local data, and doesn't consume the memory that a full database server like PostgreSQL would.&lt;/p&gt;

&lt;p&gt;Have you used SQLite in your Python projects? Let me know below!&lt;/p&gt;

</description>
      <category>python</category>
      <category>backend</category>
      <category>sqlite</category>
      <category>database</category>
    </item>
    <item>
      <title>The Mobile Coder's Guide to Fixing Python IndentationErrors</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Sat, 14 Nov 2026 09:45:57 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/the-mobile-coders-guide-to-fixing-python-indentationerrors-737</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/the-mobile-coders-guide-to-fixing-python-indentationerrors-737</guid>
      <description>&lt;p&gt;If you write code on your phone like I do (using Termux, Acode, or other mobile editors), you have probably wanted to throw your phone across the room because of this error:&lt;/p&gt;

&lt;p&gt;IndentationError: unexpected indent&lt;/p&gt;

&lt;p&gt;Python is incredibly strict about spaces. But when you copy and paste code from the web into a mobile editor, the phone often adds hidden tabs or extra spaces, completely breaking your script.&lt;/p&gt;

&lt;p&gt;Here is exactly how I fix it when I'm coding on the go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The "Select All" Trick&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the code is a mess, highlight the entire block of code, delete it, and re-type the first line manually. Make sure your cursor is at the absolute far-left edge of the screen before you start typing. Mobile keyboards love to auto-indent the very first line, which ruins the whole block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The Hidden Space Test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Move your cursor to the very beginning of the line that is throwing the error. Press the backspace key a few times. If the line suddenly jumps to the left, it means the editor had hidden spaces pushing the code too far to the right.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Turn Off Auto-Indent (If Possible)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many mobile code editors try to "help" by auto-indenting when you press Enter. If you are writing a while loop, this is great. If you are copying and pasting, it causes chaos. Check your editor's settings and turn off "auto-indent" if you are pasting large chunks of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Golden Rule&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When typing Python manually:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· 4 spaces&lt;/strong&gt; for a function or if statement.&lt;br&gt;
&lt;strong&gt;· 8 spaces&lt;/strong&gt; for the code inside the function.&lt;br&gt;
&lt;strong&gt;• Never mix tabs and spaces.&lt;/strong&gt; Choose spaces, always. (Most modern editors will let you set "Insert spaces instead of tabs").&lt;/p&gt;

&lt;p&gt;If you get an IndentationError, don't panic. Look at the line above the error, find the hidden space, and backspace it.&lt;/p&gt;

&lt;p&gt;Have you ever struggled with mobile coding? Let me know in the comments!&lt;/p&gt;

</description>
      <category>python</category>
      <category>temux</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Handle Asynchronous AI Streams in FastAPI Without Freezing Your Server</title>
      <dc:creator>EZENWA CHRISTIAN CHIMEREMEZE</dc:creator>
      <pubDate>Fri, 13 Nov 2026 14:05:16 +0000</pubDate>
      <link>https://dev.to/ezenwa_christian_5243b406/how-to-handle-asynchronous-ai-streams-in-fastapi-without-freezing-your-server-40j4</link>
      <guid>https://dev.to/ezenwa_christian_5243b406/how-to-handle-asynchronous-ai-streams-in-fastapi-without-freezing-your-server-40j4</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjd21wewa8ncap8emze6c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjd21wewa8ncap8emze6c.jpg" alt=" " width="800" height="447"&gt;&lt;/a&gt;&lt;br&gt;
When I built my AI career app on an Android phone, I hit a wall. My server kept crashing every time I called the Gemini API. Here is how I fixed it.&lt;/p&gt;

&lt;p&gt;When I built my full-stack AI career app using Termux on my Android phone, I was incredibly proud. I had a Flask backend, SQLite for data, and I was calling the Google Gemini API to generate cover letters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But I had a major problem&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every time the AI started generating a long response, the entire server would freeze. I couldn't click any other buttons, the UI would hang, and eventually, the app would time out.&lt;/p&gt;

&lt;p&gt;I realized I had fallen into the classic asynchronous trap. Here is what I learned about handling streaming AI responses in Python, and how you can avoid the same mistake.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Blocking Trap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my first attempt, I was using standard Python time.sleep() to wait for the AI to respond before sending the data to the frontend.&lt;/p&gt;

&lt;p&gt;The problem? time.sleep() is synchronous. It completely blocks the Python event loop. This means while the server is waiting for the AI to finish generating text, it cannot process any other requests. It is literally standing still, waiting.&lt;/p&gt;

&lt;p&gt;If you are building an AI app, you cannot do this. You need your server to stay awake and handle other tasks while the AI is thinking.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;The Fix: asyncio and Streaming Responses&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
To fix this, I had to refactor my backend to use FastAPI and asyncio.&lt;/p&gt;

&lt;p&gt;Instead of waiting for the AI to finish and then sending the whole block of text at once, I used a StreamingResponse. This allows the backend to send the AI's text to the frontend chunk by chunk, as it is being generated.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here is a simplified version of the code I used&lt;/em&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;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;responses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;StreamingResponse&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generativeai&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_ai_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Configure the Gemini API&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GenerativeModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'gemini-pro'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Generate content asynchronously&lt;/span&gt;
    &lt;span class="n"&gt;response&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;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Yield the text chunk by chunk&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&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;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;
        &lt;span class="c1"&gt;# CRITICAL: Use asyncio.sleep, not time.sleep&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="nd"&gt;@app&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;"/generate-cover-letter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_cover_letter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;job_description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Write a cover letter for this job: {job_description}"&lt;/span&gt;

    &lt;span class="c1"&gt;# Return a streaming response&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;StreamingResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;generate_ai_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;media_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"text/event-stream"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Handling API Rate Limits with Exponential Back&lt;/em&gt;&lt;/strong&gt;off&lt;/p&gt;

&lt;p&gt;Another issue I ran into was the Gemini API rate limit. If I sent too many requests too quickly, the API would throw an error and my app would crash.&lt;/p&gt;

&lt;p&gt;To fix this, I implemented exponential backoff using asyncio. If the API fails, the server waits a bit, then tries again. If it fails again, it waits twice as long.&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;asyncio&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ResourceExhausted&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;safe_api_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&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;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_retries&lt;/span&gt;&lt;span class="p"&gt;)&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;ResourceExhausted&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;attempt&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;max_retries&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;raise&lt;/span&gt;
            &lt;span class="c1"&gt;# Wait exponentially longer each time&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&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;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Lesson&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Building AI applications is not just about connecting an API and hoping for the best. It is about understanding the event loop, managing asynchronous state, and writing code that can handle real-world constraints (like mobile servers and API limits).&lt;/p&gt;

&lt;p&gt;If you are building an AI backend, don't block your event loop. Use asyncio, stream your responses, and handle your rate limits. Your users will thank you.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>api</category>
      <category>backend</category>
      <category>python</category>
    </item>
  </channel>
</rss>
