<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manalimran on Medium]]></title>
        <description><![CDATA[Stories by Manalimran on Medium]]></description>
        <link>https://medium.com/@minalimran2002?source=rss-6e6507dbd466------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*syfz2EUZ4GrbRTE0wAPAHA.jpeg</url>
            <title>Stories by Manalimran on Medium</title>
            <link>https://medium.com/@minalimran2002?source=rss-6e6507dbd466------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 15:16:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@minalimran2002/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The “No-GIL” Revolution: Preparing for Free-Threaded Python]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/the-no-gil-revolution-preparing-for-free-threaded-python-39df73c374fc?source=rss-6e6507dbd466------2"><img src="https://cdn-images-1.medium.com/max/1024/1*19UbDIlMvDMHzYQdvSYbOw.png" width="1024"></a></p><p class="medium-feed-snippet">How the removal of Python&#x2019;s Global Interpreter Lock will reshape performance, architecture, and engineering mindset</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/the-no-gil-revolution-preparing-for-free-threaded-python-39df73c374fc?source=rss-6e6507dbd466------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/the-no-gil-revolution-preparing-for-free-threaded-python-39df73c374fc?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/39df73c374fc</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Mon, 20 Jul 2026 15:33:37 GMT</pubDate>
            <atom:updated>2026-07-20T15:33:37.524Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to Handle Real-time Data Synchronization Across Multiple Browsers]]></title>
            <link>https://levelup.gitconnected.com/how-to-handle-real-time-data-synchronization-across-multiple-browsers-0a1f951c04e7?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a1f951c04e7</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Wed, 11 Feb 2026 17:33:01 GMT</pubDate>
            <atom:updated>2026-02-11T17:33:01.782Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Designing scalable, conflict-free, and production-ready real-time systems for modern web applications</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*67oDM-Dz5XyxBk0PO2t2sw.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>1. Why Real-time Synchronization Is Harder Than It Looks</h3><p>Real-time synchronization sounds simple: one user updates data, and all other browsers see the change instantly. But in production systems, challenges quickly emerge — race conditions, network latency, stale state, offline users, and data conflicts.</p><p>In collaborative dashboards, live editors, or SaaS admin panels, even a 1-second delay can break user trust. The biggest mistake I see engineers make is assuming polling is “good enough.” It isn’t once you scale beyond a handful of concurrent users.</p><p>A naive polling approach might look like this:</p><pre>setInterval(async () =&gt; {<br>  const res = await fetch(&quot;/api/data&quot;);<br>  const data = await res.json();<br>  updateUI(data);<br>}, 3000);</pre><p>This wastes bandwidth, introduces lag, and scales poorly. Real-time systems require <strong>event-driven architecture</strong> instead.</p><h3>2. Choosing the Right Real-time Communication Strategy</h3><p>There are three main real-time transport approaches:</p><ul><li>WebSockets</li><li>Server-Sent Events (SSE)</li><li>Realtime Databases (Firestore, Supabase, Firebase RTDB)</li></ul><p>WebSockets are usually the most flexible because they allow full duplex communication.</p><pre>const socket = new WebSocket(&quot;ws://localhost:3000&quot;);<br>socket.onmessage = (event) =&gt; {<br>  const update = JSON.parse(event.data);<br>  render(update);<br>};</pre><p>Realtime databases simplify synchronization because they manage listeners automatically, but WebSockets give you total architectural control.</p><h3>3. Server Architecture: Broadcasting Updates Efficiently</h3><p>A good real-time system uses <strong>publish–subscribe patterns</strong> instead of direct client messaging. When one browser updates data, the server broadcasts events to all connected clients.</p><p>Example using Node.js and WebSocket:</p><pre>const WebSocket = require(&quot;ws&quot;);<br>const wss = new WebSocket.Server({ port: 3000 });<br>wss.on(&quot;connection&quot;, (ws) =&gt; {<br>  ws.on(&quot;message&quot;, (msg) =&gt; {<br>    wss.clients.forEach((client) =&gt; {<br>      if (client.readyState === WebSocket.OPEN) {<br>        client.send(msg.toString());<br>      }<br>    });<br>  });<br>});</pre><p>This ensures synchronization happens instantly across every connected browser.</p><h3>4. Handling State Consistency on the Client Side</h3><p>Real-time updates are only useful if UI state stays consistent. You must manage:</p><ul><li>incoming events</li><li>local updates</li><li>UI rendering order</li></ul><p>A simple state reducer approach helps maintain consistency.</p><pre>function reducer(state, action) {<br>  switch (action.type) {<br>    case &quot;UPDATE_ITEM&quot;:<br>      return { ...state, item: action.payload };<br>    default:<br>      return state;<br>  }<br>}</pre><p>This prevents UI from breaking when multiple updates arrive simultaneously.</p><h3>5. Dealing with Conflicts Between Multiple Browsers</h3><p>Two users editing the same record at once can cause data conflicts. You need conflict resolution strategies such as:</p><ul><li>last write wins</li><li>operational transforms</li><li>CRDTs (Conflict-free Replicated Data Types)</li></ul><p>A simple optimistic concurrency example:</p><pre>if (clientVersion === serverVersion) {<br>  saveUpdate();<br>} else {<br>  showConflictDialog();<br>}</pre><p>In large collaborative apps (Google Docs–style), CRDTs or operational transforms are essential.</p><h3>6. Real-time Sync Using Firebase Firestore Listeners</h3><p>Realtime databases like Firestore simplify synchronization dramatically. Instead of manual socket management, you subscribe to live updates.</p><pre>import { onSnapshot, doc } from &quot;firebase/firestore&quot;;<br>onSnapshot(doc(db, &quot;tasks&quot;, &quot;123&quot;), (snapshot) =&gt; {<br>  updateUI(snapshot.data());<br>});</pre><p>Whenever any browser updates the document, all other clients receive instant updates automatically.</p><h3>7. Offline Support and Event Queues</h3><p>Modern applications must handle intermittent connectivity. Browsers may disconnect temporarily, then reconnect later.</p><p>A robust strategy involves maintaining a local queue:</p><pre>let queue = [];<br>function sendUpdate(data) {<br>  if (navigator.onLine) {<br>    socket.send(JSON.stringify(data));<br>  } else {<br>    queue.push(data);<br>  }<br>}</pre><p>When the connection resumes, queued updates are synced back to the server.</p><h3>8. Scaling Real-time Systems for High Traffic</h3><p>When traffic grows, a single WebSocket server becomes a bottleneck. Production-grade solutions include:</p><ul><li>Redis Pub/Sub</li><li>Kafka Event Streams</li><li>Load-balanced socket clusters</li></ul><p>Example using Redis Pub/Sub:</p><pre>redis.subscribe(&quot;updates&quot;);<br>redis.on(&quot;message&quot;, (_, message) =&gt; {<br>  broadcastToClients(message);<br>});</pre><p>This allows multiple servers to synchronize events across a distributed infrastructure.</p><h3>9. Security and Authentication in Real-time Sync</h3><p>Real-time systems are vulnerable if authentication is ignored. Every connection must be verified before allowing updates.</p><pre>socket.on(&quot;message&quot;, (msg) =&gt; {<br>  if (!isAuthenticated(userToken)) return;<br>  processUpdate(msg);<br>});</pre><p>Additionally:</p><ul><li>Validate payload schemas</li><li>Prevent unauthorized broadcast</li><li>Apply rate limiting</li></ul><p>Security is often overlooked in early real-time implementations — and becomes a painful retrofit later.</p><h3>10. Observability: Monitoring Sync Health in Production</h3><p>Without monitoring, real-time bugs become invisible. You need:</p><ul><li>connection tracking</li><li>latency metrics</li><li>event throughput monitoring</li></ul><pre>console.log(&quot;Active connections:&quot;, wss.clients.size);</pre><p>In production, integrate observability tools like:</p><ul><li>Prometheus</li><li>Datadog</li><li>OpenTelemetry</li></ul><p>This ensures you detect synchronization delays before users do.</p><h3>Conclusion: Real-time Sync Is an Architecture, Not a Feature</h3><p>Real-time synchronization across multiple browsers is not just about adding WebSockets. It requires:</p><ul><li>Event-driven server architecture</li><li>State consistency strategies</li><li>Conflict resolution models</li><li>Scalability planning</li><li>Secure messaging</li><li>Offline resilience</li></ul><p>In my experience building collaborative systems, the biggest success factor isn’t the technology itself it’s designing synchronization as a <strong>first-class architectural concern</strong> from day one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a1f951c04e7" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/how-to-handle-real-time-data-synchronization-across-multiple-browsers-0a1f951c04e7">How to Handle Real-time Data Synchronization Across Multiple Browsers</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Protocol-Oriented Programming in Python (PoPP)]]></title>
            <link>https://python.plainenglish.io/protocol-oriented-programming-in-python-popp-89a16782b165?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/89a16782b165</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Sat, 07 Feb 2026 12:52:15 GMT</pubDate>
            <atom:updated>2026-02-07T12:52:15.498Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>How Python protocols changed the way I design flexible, testable, and future-proof systems</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YSjIqx_ws3YN4HR9EGWw4Q.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>Introduction: When Inheritance Stops Scaling</h3><p>Early in my career, I leaned heavily on class inheritance. It felt natural — base classes, shared logic, and overridden methods. Over time, those hierarchies became brittle. A small change in a base class caused unexpected failures across the system. Protocol-Oriented Programming (PoPP) in Python helped me unlearn inheritance-heavy thinking and replace it with <strong>behavior-driven design</strong>. Instead of asking <em>“What does this object inherit from?”</em>, PoPP asks <em>“What behavior does this object promise?”</em></p><h3>What Protocol-Oriented Programming Really Means</h3><p>Protocol-Oriented Programming focuses on <strong>interfaces defined by behavior</strong>, not concrete types. In Python, this concept is powered by typing.Protocol.</p><pre>from typing import Protocol<br>class Serializer(Protocol):<br>    def serialize(self, data: dict) -&gt; str:<br>        ...</pre><p>Any object that implements serialize() now conforms to Serializer—without inheritance. This decouples design from class hierarchies.</p><h3>Why Python Was Always Ready for PoPP</h3><p>Python has always favored duck typing:</p><pre>def save(serializer):<br>    return serializer.serialize({&quot;id&quot;: 1})</pre><p>Protocols simply make this <strong>explicit and type-safe</strong>. They preserve Python’s flexibility while enabling static analysis and tooling.</p><h3>Protocols vs Abstract Base Classes (ABCs)</h3><p>ABCs enforce inheritance. Protocols enforce behavior.</p><pre># ABC<br>from abc import ABC, abstractmethod<br>class Payment(ABC):<br>    @abstractmethod<br>    def pay(self, amount: float): ...<br># Protocol<br>class Payment(Protocol):<br>    def pay(self, amount: float): ...</pre><p>With protocols, classes don’t need to know they’re part of a hierarchy. This reduces coupling dramatically.</p><h3>Structural Subtyping: The Core Advantage</h3><p>Protocols introduce <strong>structural subtyping</strong> if it looks like a duck and quacks like a duck, it <em>is</em> a duck.</p><pre>class StripePayment:<br>    def pay(self, amount: float):<br>        print(f&quot;Paid {amount}&quot;)</pre><p>StripePayment automatically satisfies Payment—no inheritance required. This is a major shift in design philosophy.</p><h3>Designing Systems Around Capabilities, Not Classes</h3><p>PoPP encourages thinking in terms of <strong>capabilities</strong>.</p><pre>class Cache(Protocol):<br>    def get(self, key: str) -&gt; str | None: ...<br>    def set(self, key: str, value: str) -&gt; None: ...</pre><p>Your system no longer cares <em>how</em> caching is implemented — only that the behavior exists.</p><h3>Testability Improves Instantly with Protocols</h3><p>Testing becomes easier because mocks don’t need to inherit from anything.</p><pre>class FakeCache:<br>    def get(self, key): return None<br>    def set(self, key, value): pass</pre><p>No base class. No mocking frameworks. Just behavior.</p><h3>Protocols in Dependency Injection</h3><p>Protocols pair naturally with dependency injection.</p><pre>def process_payment(payment: Payment):<br>    payment.pay(100)</pre><p>Any compliant implementation can be injected — local, remote, fake, or future.</p><h3>Protocols and Static Type Checking</h3><p>Tools like mypy finally make dynamic Python safer without making it rigid.</p><pre>def checkout(payment: Payment):<br>    payment.pay(50)</pre><p>If an object lacks pay(), the error is caught <strong>before runtime</strong>.</p><h3>Avoiding Inheritance Hell with Composition + Protocols</h3><p>Protocols encourage composition over inheritance.</p><pre>class Logger(Protocol):<br>    def log(self, message: str): ...</pre><p>Small, focused protocols replace deep inheritance trees.</p><h3>Protocols Across Service Boundaries</h3><p>PoPP scales across microservices and APIs.</p><pre>class UserRepository(Protocol):<br>    def get_user(self, user_id: int) -&gt; dict: ...</pre><p>Database, API, or cache implementations can be swapped without touching business logic.</p><h3>Runtime Safety vs Compile-Time Guarantees</h3><p>Protocols are mostly static — at runtime, Python remains flexible.</p><pre>from typing import runtime_checkable<br>@runtime_checkable<br>class Serializer(Protocol):<br>    def serialize(self, data: dict) -&gt; str: ...</pre><p>This allows safe runtime validation when needed.</p><h3>Performance Impact: Almost Zero</h3><p>Protocols add <strong>no runtime overhead</strong> unless runtime checks are used. They influence design and tooling not execution speed.</p><h3>PoPP and the Future of Python</h3><p>As Python moves toward <strong>free-threading (No-GIL)</strong> and more complex systems, protocol-based design becomes essential. Clear behavioral contracts reduce race conditions and ambiguity.</p><h3>Common Mistakes When Adopting PoPP</h3><ul><li>Over-engineering small scripts</li><li>Creating large “god protocols”</li><li>Replacing all classes with protocols</li></ul><pre>Protocols define behavior—not business logic</pre><p>Use them deliberately.</p><h3>How PoPP Changed My Engineering Mindset</h3><p>PoPP taught me to:</p><ul><li>Design for change</li><li>Minimize coupling</li><li>Optimize for testability</li><li>Think in behaviors, not hierarchies</li></ul><pre># Behavior survives refactoring<br>if supports(Payment):<br>    process_payment(obj)</pre><h3>Conclusion: Protocols Make Python Systems Grow Gracefully</h3><p>Protocol-Oriented Programming doesn’t fight Python’s dynamic nature — it refines it. Protocols make large Python systems safer, cleaner, and easier to evolve without sacrificing flexibility. In modern Python engineering, PoPP is not an advanced trick it’s a <strong>necessary design tool</strong>.</p><p>Inheritance models structure.<br> Protocols model <strong>capability and intent</strong>.</p><p>That distinction changes everything.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89a16782b165" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/protocol-oriented-programming-in-python-popp-89a16782b165">Protocol-Oriented Programming in Python (PoPP)</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PoPP vs OOP: Choosing the Right Design Model in Python]]></title>
            <link>https://python.plainenglish.io/popp-vs-oop-choosing-the-right-design-model-in-python-d34ce7c3965e?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/d34ce7c3965e</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Sat, 07 Feb 2026 12:51:47 GMT</pubDate>
            <atom:updated>2026-02-07T12:51:47.165Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>A senior engineer’s perspective on when inheritance fails and behavior-driven design wins</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xxWAeYsTfTvcRjzIWXf8IA.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>1. Introduction: Why This Debate Matters More Than Ever</h3><p>For years, Object-Oriented Programming (OOP) was the default mental model for Python engineers. Classes, inheritance, and polymorphism were considered best practices. But as Python applications grew — microservices, AI pipelines, distributed systems I personally started feeling friction. Systems became rigid, hard to test, and risky to refactor. That’s when <strong>Protocol-Oriented Programming (PoPP)</strong> quietly entered the picture, not as a replacement for OOP, but as a correction to its overuse.</p><pre># Traditional OOP thinking<br>class Service:<br>    def execute(self):<br>        raise NotImplementedError</pre><p>PoPP asks a different question: <em>Do we really need inheritance here?</em></p><h3>2. Object-Oriented Programming: Strengths and Structural Limits</h3><p>OOP excels at modeling <strong>real-world entities</strong> and enforcing <strong>shared logic</strong> through inheritance. For small systems, it works beautifully.</p><pre>class Animal:<br>    def speak(self):<br>        raise NotImplementedError<br>class Dog(Animal):<br>    def speak(self):<br>        return &quot;Bark&quot;</pre><p>However, as systems scale, inheritance trees become fragile. A single change in a base class can ripple across dozens of subclasses. In my experience, this becomes a liability more than a benefit.</p><h3>3. The Hidden Cost of Inheritance in Large Codebases</h3><p>Inheritance tightly couples components. The deeper the hierarchy, the harder it is to reason about behavior.</p><pre>class BaseProcessor:<br>    def process(self, data):<br>        self.validate(data)<br>        return self.transform(data)<br>class CustomProcessor(BaseProcessor):<br>    def validate(self, data):<br>        pass</pre><p>This looks clean until you need <em>slightly different behavior</em>. Soon, you’re overriding half the base class or duplicating logic — both are red flags.</p><h3>4. Protocol-Oriented Programming: Designing by Behavior</h3><p>PoPP flips the model. Instead of inheritance, it defines <strong>behavioral contracts</strong> using typing.Protocol.</p><pre>from typing import Protocol<br>class Processor(Protocol):<br>    def process(self, data: dict) -&gt; dict:<br>        ...</pre><p>Any object implementing process() now satisfies the protocol—without inheritance. This single shift dramatically improves flexibility and long-term maintainability.</p><h3>5. Structural Subtyping: The Core Advantage of PoPP</h3><p>Unlike OOP’s nominal typing, PoPP uses <strong>structural subtyping</strong>.</p><pre>class JsonProcessor:<br>    def process(self, data: dict) -&gt; dict:<br>        return {&quot;status&quot;: &quot;ok&quot;}</pre><p>JsonProcessor conforms to Processor automatically. This decouples implementation from design intent—something inheritance simply cannot offer.</p><h3>6. Testability: Where PoPP Clearly Wins</h3><p>Testing OOP-heavy systems often requires mocks that inherit from abstract classes.</p><pre>class FakeProcessor:<br>    def process(self, data):<br>        return data</pre><p>With PoPP, no mocking framework, no inheritance — just behavior. This drastically reduces test complexity and improves reliability in CI pipelines.</p><h3>7. Dependency Injection: OOP vs PoPP in Practice</h3><p>OOP dependency injection often relies on base classes.</p><pre>def run(processor: BaseProcessor):<br>    processor.process({})</pre><p>PoPP removes that coupling entirely.</p><pre>def run(processor: Processor):<br>    processor.process({})</pre><p>This allows seamless swapping of implementations — local, remote, cached, AI-powered — without touching core logic.</p><h3>8. When OOP Is Still the Right Choice</h3><p>PoPP is powerful but not universal. OOP shines when:</p><ul><li>There is shared state</li><li>There is deep domain modeling</li><li>Behavior is tightly bound to data</li></ul><pre>class User:<br>    def __init__(self, email):<br>        self.email = email<br>def deactivate(self):<br>        self.active = False</pre><p>In such cases, OOP provides clarity and cohesion.</p><h3>9. A Hybrid Approach: The Real-World Sweet Spot</h3><p>In real production systems, I rarely choose PoPP <em>or</em> OOP exclusively. The most stable systems blend both.</p><pre>class EmailSender(Protocol):<br>    def send(self, to: str, body: str): ...<br>class SMTPClient:<br>    def send(self, to, body):<br>        pass</pre><p>OOP models entities.<br> PoPP models interactions between them.</p><h3>10. Performance and Runtime Considerations</h3><p>Protocols are mostly <strong>static constructs</strong>. They introduce almost zero runtime overhead.</p><pre>from typing import runtime_checkable<br>@runtime_checkable<br>class Cache(Protocol):<br>    def get(self, key: str): ...</pre><p>Runtime checks are optional and explicit — keeping performance predictable.</p><h3>11. How This Choice Impacts AI and Distributed Systems</h3><p>Modern systems — AI agents, RAG pipelines, event-driven architectures — change rapidly. PoPP excels here because behavior evolves faster than class hierarchies.</p><pre>class VectorStore(Protocol):<br>    def search(self, query: str) -&gt; list: ...</pre><p>This abstraction survives refactors, vendor changes, and scaling decisions.</p><h3>12. My Personal Rule of Thumb</h3><p>After years in production systems:</p><pre>Use OOP to model “things”<br>Use PoPP to model “capabilities”</pre><p>When code starts resisting change, inheritance is often the culprit.</p><h3>Conclusion: Choosing the Right Model Is a Design Skill</h3><p>PoPP is not anti-OOP. It is anti-rigidity. The best Python engineers don’t ask <em>“Which paradigm should I use?”</em> They ask:</p><blockquote><em>“Which design will survive change with the least friction?”</em></blockquote><p>In modern Python, that answer increasingly includes <strong>Protocol-Oriented Programming</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d34ce7c3965e" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/popp-vs-oop-choosing-the-right-design-model-in-python-d34ce7c3965e">PoPP vs OOP: Choosing the Right Design Model in Python</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing RAG Systems with Firestore and Vector Search]]></title>
            <link>https://levelup.gitconnected.com/designing-rag-systems-with-firestore-and-vector-search-0b6da5a94a0a?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/0b6da5a94a0a</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Mon, 26 Jan 2026 04:57:47 GMT</pubDate>
            <atom:updated>2026-01-26T04:57:47.448Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>How I built scalable, hallucination-resistant AI systems by combining Retrieval-Augmented Generation with Firestore</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XzGX0Gm61kR5p3Ds02Hulg.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>Introduction: Why RAG Became Mandatory in Real AI Systems</h3><p>When I first deployed AI features into production, the biggest issue wasn’t model accuracy it was <strong>trust</strong>. Users expected answers grounded in reality, not plausible guesses. That’s where Retrieval-Augmented Generation (RAG) fundamentally changed how I design AI systems. Instead of asking models to “know everything,” RAG allows them to <strong>look things up</strong>. Firestore, with its real-time capabilities and structured data model, turned out to be a surprisingly strong backbone for building RAG pipelines at scale.</p><h3>Understanding RAG from a System Design Perspective</h3><p>At a high level, RAG combines two systems:</p><ol><li>A <strong>retrieval layer</strong> that fetches relevant knowledge</li><li>A <strong>generation layer</strong> that reasons over that knowledge</li></ol><p>What matters in production is not the model — it’s how fast, accurate, and consistent your retrieval layer is.</p><pre>User Query<br>   ↓<br>Vector Search (Firestore / Vector DB)<br>   ↓<br>Relevant Context<br>   ↓<br>LLM Response (Grounded)</pre><p>Firestore excels here because it already handles <strong>scalability, indexing, and real-time updates</strong> without additional infrastructure.</p><h3>Why Firestore Works Well for RAG Systems</h3><p>Firestore wasn’t built for AI, but it aligns extremely well with RAG requirements:</p><ul><li>Low-latency reads</li><li>Strong consistency</li><li>Structured metadata support</li><li>Real-time updates</li></ul><p>I store <strong>documents + embeddings + metadata</strong> together, which simplifies reasoning and filtering.</p><pre>{<br>  &quot;content&quot;: &quot;Refunds are processed within 5 business days&quot;,<br>  &quot;embedding&quot;: [0.021, -0.334, 0.889],<br>  &quot;source&quot;: &quot;policy_docs&quot;,<br>  &quot;updatedAt&quot;: &quot;2025-01-10&quot;<br>}</pre><p>This structure enables both semantic search and rule-based filtering.</p><h3>Embedding Strategy: The Foundation of Retrieval Quality</h3><p>Poor embeddings guarantee poor RAG results. I learned quickly that embeddings must be:</p><ul><li>Generated consistently</li><li>Stored immutably</li><li>Versioned carefully</li></ul><pre>from openai import embeddings<br>vector = embeddings.create(<br>    model=&quot;text-embedding-3-large&quot;,<br>    input=document_text<br>)[&quot;data&quot;][0][&quot;embedding&quot;]</pre><p>Every time the embedding model changes, I re-embed everything. Mixing embedding versions silently degrades retrieval accuracy.</p><h3>Designing Vector Search with Firestore</h3><p>Firestore doesn’t natively compute cosine similarity — but it doesn’t need to. In practice, I use:</p><ul><li>Pre-filtering in Firestore</li><li>Vector similarity in a dedicated layer (or extension)</li></ul><pre>docs = firestore.collection(&quot;knowledge&quot;) \<br>    .where(&quot;source&quot;, &quot;==&quot;, &quot;policy_docs&quot;) \<br>    .limit(20) \<br>    .stream()</pre><p>Then I compute similarity in memory or via a vector service.</p><pre>def cosine_similarity(a, b):<br>    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))</pre><p>This hybrid approach scales surprisingly well for most enterprise workloads.</p><h3>Metadata Filtering: The Hidden RAG Superpower</h3><p>Metadata filtering prevents irrelevant context from ever reaching the model.</p><pre>query = firestore.collection(&quot;docs&quot;) \<br>    .where(&quot;region&quot;, &quot;==&quot;, &quot;EU&quot;) \<br>    .where(&quot;visibility&quot;, &quot;==&quot;, &quot;public&quot;)</pre><p>This single step reduced hallucinations more than prompt tweaks ever did.</p><h3>Context Window Management: Less Is More</h3><p>One mistake I made early was sending <strong>too much context</strong>. More context increases confusion.</p><pre>top_docs = sorted(<br>    docs,<br>    key=lambda d: similarity(d.embedding, query_embedding),<br>    reverse=True<br>)[:3]</pre><p>Three high-quality documents outperform ten mediocre ones every time.</p><h3>Prompt Design: Enforcing Grounded Responses</h3><p>The model must be told explicitly to <strong>use retrieved context only</strong>.</p><pre>Answer the question using ONLY the context below.<br>If the answer is not present, say &quot;I don’t know.&quot;<br>Context:<br>{{documents}}</pre><p>This instruction alone eliminated entire classes of hallucinations.</p><h3>Handling Missing Context Safely</h3><p>A production RAG system must fail gracefully.</p><pre>if not retrieved_docs:<br>    return {<br>        &quot;answer&quot;: &quot;No relevant information found&quot;,<br>        &quot;confidence&quot;: 0.0<br>    }</pre><p>If the system doesn’t know, it must say so — confident ignorance is safer than confident hallucination.</p><h3>Firestore Real-Time Updates and Knowledge Freshness</h3><p>Firestore’s real-time nature is a massive advantage. When content changes, users see updated AI behavior immediately — no retraining required.</p><pre>def on_doc_update(event):<br>    regenerate_embedding(event.document)</pre><p>This makes RAG systems <strong>living knowledge systems</strong>, not static models.</p><h3>Scaling RAG with Firestore</h3><p>At scale, the bottlenecks are:</p><ul><li>Embedding generation</li><li>Similarity computation</li><li>Context assembly</li></ul><p>Firestore handles reads effortlessly. I scale embeddings and similarity independently.</p><pre>Firestore → Retrieval<br>Cloud Functions → Embeddings<br>LLM API → Generation</pre><p>Each layer scales independently, which is critical for reliability.</p><h3>Observability: Detecting Retrieval Failures Early</h3><p>You cannot fix what you don’t measure.</p><pre>log({<br>  &quot;query&quot;: user_query,<br>  &quot;retrieved_docs&quot;: len(docs),<br>  &quot;confidence&quot;: response.confidence<br>})</pre><p>Most hallucinations originate from retrieval failure — not generation failure.</p><h3>Security and Access Control in RAG Systems</h3><p>Firestore’s security rules integrate cleanly with RAG.</p><pre>allow read: if request.auth != null</pre><p>This ensures the AI never accesses unauthorized knowledge — an often overlooked risk.</p><h3>RAG vs Traditional AI Pipelines: The Engineering Shift</h3><p>Traditional pipelines trained intelligence <strong>into</strong> models. RAG shifts intelligence <strong>around</strong> models.</p><pre>Model = Reasoning Engine<br>Firestore = Source of Truth</pre><p>This separation is why RAG scales safely.</p><h3>Conclusion: Firestore Turns RAG into an Engineering Problem, Not an AI Gamble</h3><p>Designing RAG systems with Firestore taught me a critical lesson: hallucination prevention is an architectural problem, not a prompt trick. Firestore’s consistency, structure, and scalability make it an excellent backbone for real-world RAG systems. When retrieval is reliable, models behave. When retrieval fails, no model can save you.</p><p>The future of AI systems isn’t smarter models — it’s <strong>better-designed retrieval layers</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0b6da5a94a0a" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/designing-rag-systems-with-firestore-and-vector-search-0b6da5a94a0a">Designing RAG Systems with Firestore and Vector Search</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Preventing Hallucinations in AI APIs]]></title>
            <link>https://levelup.gitconnected.com/preventing-hallucinations-in-ai-apis-90a0a466a85d?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/90a0a466a85d</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Thu, 22 Jan 2026 19:48:40 GMT</pubDate>
            <atom:updated>2026-01-22T19:48:40.614Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Subtitle: Engineering techniques I use to make AI systems reliable, predictable, and safe for real-world production</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*84kfLJQGFBzvi2diWJiU2g.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>Introduction: When AI Sounds Confident but Is Completely Wrong</h3><p>One of the most dangerous moments in building AI-powered systems is when the model responds confidently and incorrectly. Early in my work with AI APIs, hallucinations were the single biggest reason systems failed silently. The responses looked polished, convincing, and authoritative, yet contained fabricated facts, invented fields, or unsupported conclusions. What made this problem serious was not that the AI was wrong — but that <strong>it didn’t know it was wrong</strong>. Preventing hallucinations became less about improving intelligence and more about applying strong engineering discipline around uncertainty, validation, and constraints.</p><h3>Why Hallucinations Happen in AI APIs</h3><p>Hallucinations are not bugs in the traditional sense; they are a natural result of probabilistic models trained to predict likely sequences of text. When an AI doesn’t have enough information — or when a prompt is ambiguous — it fills the gap with statistically plausible content. I learned quickly that <strong>AI will always produce an answer unless explicitly told not to</strong>. Without guardrails, the model optimizes for fluency, not correctness.</p><pre>User Input: &quot;Is this transaction safe?&quot;<br>AI Output: &quot;Yes, this transaction is completely safe.&quot;<br>Problem: No data was provided to support that claim.</pre><p>The failure wasn’t the model — it was the lack of constraints.</p><h3>Define Clear Boundaries for What the Model Is Allowed to Say</h3><p>The most effective hallucination prevention technique I’ve used is explicitly defining <strong>what the model must NOT do</strong>. Instead of asking open-ended questions, I enforce strict decision boundaries.</p><pre>If the information is insufficient:<br>- Do NOT guess<br>- Do NOT infer<br>- Return &quot;unknown&quot;</pre><p>This single instruction dramatically reduced fabricated responses. AI models are obedient when instructions are precise.</p><h3>Force Structured Outputs Instead of Free Text</h3><p>Hallucinations thrive in unstructured, conversational responses. The moment I switched to structured JSON outputs, hallucinations dropped significantly. Structure limits creativity and forces the model to operate within predefined fields.</p><pre>{<br>  &quot;decision&quot;: &quot;review&quot;,<br>  &quot;confidence&quot;: 0.42,<br>  &quot;reason&quot;: &quot;insufficient_data&quot;<br>}</pre><p>A structured response makes it obvious when the model doesn’t know something — and prevents it from inventing explanations.</p><h3>Ground AI Responses in Verified Context</h3><p>One of the biggest improvements came from grounding model responses strictly in supplied context. If the AI is only allowed to reference known data, hallucinations become much harder.</p><pre>Answer using ONLY the information provided below.<br>If the answer is not present, return null.<br>Context:<br>- User age: 29<br>- Account status: active</pre><p>This approach is foundational to Retrieval-Augmented Generation (RAG) systems and is essential for factual accuracy.</p><h3>Use Confidence Scores as a First-Class Output</h3><p>I learned not to trust binary answers alone. Every meaningful AI decision should include a confidence score. Low confidence signals uncertainty and enables safer downstream handling.</p><pre>{<br>  &quot;classification&quot;: &quot;fraud_risk&quot;,<br>  &quot;confidence&quot;: 0.31<br>}</pre><p>In production systems, low-confidence outputs are routed to human review or fallback logic — never trusted blindly.</p><h3>Design Prompts That Reward Saying “I Don’t Know”</h3><p>By default, models avoid saying “I don’t know.” I explicitly teach them that uncertainty is acceptable — and expected.</p><pre>It is acceptable and preferred to say &quot;unknown&quot;<br>rather than provide an uncertain or fabricated answer.</pre><p>Once this behavior is reinforced, hallucination frequency drops dramatically.</p><h3>Validate AI Outputs Programmatically Every Time</h3><p>No matter how strong your prompts are, <strong>never trust AI output without validation</strong>. Every AI API I deploy includes schema validation, type checks, and business-rule enforcement.</p><pre>def validate_response(data):<br>    if data[&quot;confidence&quot;] &lt; 0.5:<br>        return &quot;manual_review&quot;<br>    if data[&quot;decision&quot;] not in [&quot;approve&quot;, &quot;reject&quot;, &quot;review&quot;]:<br>        raise ValueError(&quot;Invalid decision&quot;)<br>    return data</pre><p>Validation transforms AI from a risk into a controlled component.</p><h3>Fail Safely Instead of Failing Silently</h3><p>One of the worst mistakes I made early was letting hallucinated outputs propagate silently. Now, every AI failure fails <strong>loudly and safely</strong>.</p><pre>if not validate_ai_output(response):<br>    log_error(response)<br>    return fallback_response()</pre><p>Silent failures destroy trust. Explicit failures protect systems.</p><h3>Version and Audit AI Behavior Over Time</h3><p>Hallucinations can reappear when models or prompts change. I treat prompts like versioned APIs and log outputs for audit and regression detection.</p><pre>{<br>  &quot;prompt_version&quot;: &quot;v3.1&quot;,<br>  &quot;model&quot;: &quot;gpt-4&quot;,<br>  &quot;output&quot;: {...}<br>}</pre><p>Observability is critical for long-term reliability.</p><h3>Conclusion: Hallucination Prevention Is an Engineering Problem</h3><p>Preventing hallucinations in AI APIs is not about finding a “smarter model.” It’s about <strong>engineering for uncertainty</strong>. Clear constraints, structured outputs, grounding, validation, confidence scoring, and safe failure modes transform AI from a creative assistant into a reliable system component.</p><p>AI should not be trusted because it sounds confident.<br> AI should be trusted because <strong>it is constrained, verified, and monitored</strong>.</p><p>That’s the difference between experimentation and production-grade AI.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90a0a466a85d" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/preventing-hallucinations-in-ai-apis-90a0a466a85d">Preventing Hallucinations in AI APIs</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing AI APIs with JSON-Safe Outputs]]></title>
            <link>https://levelup.gitconnected.com/designing-ai-apis-with-json-safe-outputs-0c49c6e39a73?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c49c6e39a73</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Thu, 22 Jan 2026 19:48:34 GMT</pubDate>
            <atom:updated>2026-01-22T19:48:34.490Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Subtitle: How I learned to make AI responses reliable, parsable, and production-ready in real-world systems</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pidmh0iDPtaCrBU4q7gAZQ.png" /><figcaption>Image Genertaed by AI</figcaption></figure><h3>Introduction: When AI Meets Real Software Systems</h3><p>The first time I exposed an AI model through an API, everything looked perfect — until it didn’t. In testing, responses were readable and impressive. In production, they were chaos. Random explanations wrapped around JSON, unexpected formatting changes, missing fields, and hallucinated keys broke downstream services repeatedly. That’s when I realized a hard truth: <strong>AI output that looks good to humans is often unusable for machines</strong>. Designing AI APIs isn’t about intelligence — it’s about <strong>predictability</strong>. JSON-safe outputs are the foundation that makes AI usable in real software systems.</p><h3>Why JSON Safety Is a Non-Negotiable Requirement</h3><p>Modern applications rely on strict contracts. Frontends, microservices, data pipelines, and analytics engines expect structured, machine-readable data. When an AI response violates that contract — even slightly — it causes cascading failures. A missing comma can break a pipeline. An extra explanation can crash a parser. Over time, I learned that <strong>AI APIs must behave like deterministic services</strong>, not conversational agents.</p><pre>{<br>  &quot;status&quot;: &quot;approved&quot;,<br>  &quot;confidence&quot;: 0.91<br>}</pre><p>This structure is infinitely more valuable than a paragraph explaining <em>why</em> something was approved.</p><h3>Treat AI Outputs Like API Responses, Not Chat Messages</h3><p>The biggest mental shift I made was this:<br> <strong>Prompts are API specifications.</strong></p><p>Instead of asking questions, I now define strict response contracts. I explicitly tell the model what fields are required, what types are allowed, and what behavior is forbidden.</p><pre>Return ONLY valid JSON.<br>No explanations.<br>No markdown.<br>No comments.<br>Fields must exactly match this schema:<br>{<br>  &quot;decision&quot;: string,<br>  &quot;risk_score&quot;: number,<br>  &quot;reasons&quot;: array of strings<br>}</pre><p>Once I started treating prompts like OpenAPI specs, reliability improved dramatically.</p><h3>Hard Constraints Reduce Hallucinations</h3><p>Language models are probabilistic by nature. Without constraints, they invent structure. I learned that <strong>freedom is the enemy of JSON safety</strong>. Every time I restricted choices, outputs became more stable.</p><pre>Allowed values for decision:<br>- &quot;approve&quot;<br>- &quot;reject&quot;<br>- &quot;review&quot;<br>If unsure, return &quot;review&quot;.</pre><p>This removes ambiguity and prevents creative — but dangerous — responses.</p><h3>Use Delimiters to Prevent Instruction Leakage</h3><p>One subtle but critical issue I encountered was instruction leakage — where user input altered model behavior. The fix was isolating instructions from data using clear delimiters.</p><pre>SYSTEM INSTRUCTIONS:<br>You are a strict JSON API.<br>USER INPUT:<br>&lt;&lt;&lt;<br>Transaction amount: 12000<br>Country: Unknown<br>&gt;&gt;&gt;</pre><p>This separation significantly reduced malformed responses and injection-style failures.</p><h3>Few-Shot Examples Anchor the Output Format</h3><p>Even the best-written instructions can be misinterpreted. Examples remove guesswork. I include minimal, precise examples that match the expected format exactly.</p><pre>Example Output:<br>{<br>  &quot;decision&quot;: &quot;review&quot;,<br>  &quot;risk_score&quot;: 0.67,<br>  &quot;reasons&quot;: [&quot;unverified_country&quot;]<br>}</pre><p>Models imitate structure extremely well when shown a clean template.</p><h3>Force the Model to Self-Validate</h3><p>One of the most effective techniques I’ve used is asking the model to validate its own output before returning it. This dramatically reduces broken JSON.</p><pre>Before responding:<br>1. Validate JSON syntax<br>2. Ensure all required fields exist<br>3. Fix any formatting issues<br>4. Return only the final JSON</pre><p>This adds a safety net without additional infrastructure.</p><h3>Design for Uncertainty Explicitly</h3><p>AI models will always face ambiguous inputs. If you don’t define how uncertainty should be handled, the model will guess — and guessing is dangerous. I now explicitly define fallback behavior.</p><pre>If information is missing:<br>- Use null<br>- Do not infer or fabricate data</pre><p>This single rule eliminated an entire class of hallucination bugs in my systems.</p><h3>Always Validate Outputs in Code (Never Trust the Model Alone)</h3><p>No matter how good your prompts are, <strong>never trust AI blindly</strong>. Every production system I’ve worked on includes strict validation layers.</p><pre>from jsonschema import validate, ValidationError<br>schema = {<br>    &quot;type&quot;: &quot;object&quot;,<br>    &quot;properties&quot;: {<br>        &quot;decision&quot;: {&quot;type&quot;: &quot;string&quot;},<br>        &quot;risk_score&quot;: {&quot;type&quot;: &quot;number&quot;},<br>        &quot;reasons&quot;: {&quot;type&quot;: &quot;array&quot;}<br>    },<br>    &quot;required&quot;: [&quot;decision&quot;, &quot;risk_score&quot;]<br>}<br>def validate_ai_output(response):<br>    try:<br>        validate(instance=response, schema=schema)<br>        return True<br>    except ValidationError:<br>        return False</pre><p>This is the difference between experimentation and engineering.</p><h3>Version Your AI Contracts Like Any Other API</h3><p>One mistake I made early was changing prompt structures silently. This broke older clients instantly. Now, I version every AI output format.</p><pre>{<br>  &quot;api_version&quot;: &quot;v1&quot;,<br>  &quot;decision&quot;: &quot;approve&quot;<br>}</pre><p>AI APIs must evolve safely — just like REST or GraphQL APIs.</p><h3>Conclusion: JSON-Safe AI Is an Engineering Discipline</h3><p>Designing AI APIs with JSON-safe outputs taught me that <strong>AI reliability is not about smarter models it’s about better contracts</strong>. When you constrain, validate, version, and design for failure, AI becomes a dependable system component instead of a liability.</p><p>The future of AI in production belongs to engineers who treat prompts as specifications, outputs as contracts, and models as untrusted but powerful dependencies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c49c6e39a73" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/designing-ai-apis-with-json-safe-outputs-0c49c6e39a73">Designing AI APIs with JSON-Safe Outputs</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prompt Engineering Best Practices for Structured AI Outputs]]></title>
            <link>https://levelup.gitconnected.com/prompt-engineering-best-practices-for-structured-ai-outputs-ee44b7a9c293?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee44b7a9c293</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Wed, 21 Jan 2026 03:15:01 GMT</pubDate>
            <atom:updated>2026-01-21T03:15:01.099Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Subtitle: How I learned to make large language models produce predictable, reliable, and production-ready results</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XRRlfd5R_OIs1WoVPtpRBg.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>Introduction: When “Just Ask the Model” Stops Working</h3><p>Early in my work with large language models, I treated prompts like casual conversations. I asked questions and hoped the model would figure out what I meant. That approach worked for demos — but it failed badly in production. Responses were inconsistent, formats changed between calls, and downstream systems broke because the output wasn’t predictable. I quickly learned that <strong>structured AI outputs don’t happen by accident</strong>. They are the result of deliberate prompt engineering, where clarity, constraints, and context matter more than clever wording.</p><h3>Start with a Clear Output Contract</h3><p>The most important habit I developed was defining an explicit output contract. Models don’t naturally respect structure unless you tell them exactly what “correct” looks like. Whenever I need JSON, tables, or schema-safe data, I specify the structure upfront and treat it like an API contract.</p><pre>Return the response strictly in JSON with the following fields:<br>- title: string<br>- confidence_score: number (0–1)<br>- reasoning: string<br>Do not include any additional text.</pre><p>Once I started doing this consistently, parsing errors dropped dramatically.</p><h3>Use Delimiters to Separate Instructions from Data</h3><p>One subtle but powerful practice is isolating instructions from input data. Without clear boundaries, models sometimes blend user content into instructions, which leads to unpredictable behavior. I now always use delimiters like triple backticks or XML-style tags.</p><pre>You are an AI that extracts structured data.<br>INPUT:</pre><p>User feedback: “The app is fast but crashes on login.”</p><pre>OUTPUT:<br>Return JSON with fields: sentiment, issues</pre><p>This small habit improved accuracy more than almost any other change.</p><h3>Constrain the Model’s Degrees of Freedom</h3><p>Models are creative by default. Structured output requires <strong>reducing creativity</strong>, not increasing it. I explicitly restrict tone, verbosity, and choices when structure matters.</p><pre>Choose ONLY one of the following values:<br>[&quot;positive&quot;, &quot;neutral&quot;, &quot;negative&quot;]<br>Do not explain your choice.</pre><p>By limiting the model’s freedom, I made its behavior far more reliable and repeatable.</p><h3>Demonstrate the Format with Few-Shot Examples</h3><p>When consistency matters, examples beat explanations. I include one or two short examples showing <em>exactly</em> how the output should look. This anchors the model’s behavior far better than abstract instructions.</p><pre>Example:<br>Input: &quot;The service was slow&quot;<br>Output:<br>{<br>  &quot;sentiment&quot;: &quot;negative&quot;,<br>  &quot;confidence&quot;: 0.82<br>}</pre><p>Few-shot prompting became my go-to technique for stabilizing structured outputs across different inputs.</p><h3>Validate and Self-Correct Inside the Prompt</h3><p>One technique that surprised me was asking the model to validate its own output before returning it. This simple step catches formatting issues early.</p><pre>Before responding:<br>1. Check that the output is valid JSON<br>2. Ensure all required fields exist<br>3. If invalid, fix it silently</pre><p>This approach dramatically reduced malformed responses in production pipelines.</p><h3>Design Prompts for Failure, Not Perfection</h3><p>In real systems, inputs are messy. I learned to explicitly define what the model should do when it’s unsure or data is missing.</p><pre>If a field cannot be determined:<br>- Set its value to null<br>- Do not guess</pre><p>This prevents hallucinations and makes AI outputs safer to integrate into structured workflows.</p><h3>Pair Prompt Engineering with Programmatic Validation</h3><p>Even the best prompts aren’t perfect. I never rely on prompts alone. Every structured output is validated in code before use.</p><pre>import json<br>def safe_parse(response):<br>    try:<br>        return json.loads(response)<br>    except json.JSONDecodeError:<br>        return None</pre><p>Prompt engineering and programmatic validation together form a reliable production pattern.</p><h3>Conclusion: Structured Outputs Are an Engineering Discipline</h3><p>Prompt engineering for structured AI outputs isn’t about clever wording — it’s about <strong>engineering discipline</strong>. Clear contracts, strict constraints, examples, and validation transform language models from chatbots into dependable system components. Once I stopped treating prompts like conversations and started treating them like APIs, AI became predictable, scalable, and safe to use in real products.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee44b7a9c293" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/prompt-engineering-best-practices-for-structured-ai-outputs-ee44b7a9c293">Prompt Engineering Best Practices for Structured AI Outputs</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG vs Fine-Tuning: Preventing Hallucinations at Scale]]></title>
            <link>https://aws.plainenglish.io/rag-vs-fine-tuning-preventing-hallucinations-at-scale-47bc78c59c5b?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/47bc78c59c5b</guid>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Tue, 20 Jan 2026 17:47:33 GMT</pubDate>
            <atom:updated>2026-01-20T17:47:33.633Z</atom:updated>
            <content:encoded><![CDATA[<h4>How I decide between Retrieval-Augmented Generation and Fine-Tuning when building reliable, production-grade AI systems</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_aI7W0lPMb4jPbqs3NXO1g.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>Introduction: Scaling AI Accuracy Is Harder Than Scaling AI Intelligence</h3><p>As AI systems move from demos to production, hallucinations become a scaling problem not an occasional annoyance. Early on, I assumed better models or more training data would fix hallucinations. In practice, I learned that <strong>how you feed information to a model matters more than how smart the model is</strong>. Two dominant strategies emerged in real systems: Retrieval-Augmented Generation (RAG) and Fine-Tuning. Both reduce hallucinations, but they solve very different problems. Choosing the wrong one can quietly break your system at scale.</p><h3>Understanding the Root Cause of Hallucinations at Scale</h3><p>At scale, hallucinations are amplified by three things: incomplete context, outdated knowledge, and overgeneralization. A model trained once cannot keep up with evolving data, policies, or domain-specific edge cases. When it lacks certainty, it fills gaps with statistically reasonable — but often incorrect — content.</p><pre>Model behavior:<br>If probability of correctness &lt; certainty threshold<br>→ generate plausible completion anyway</pre><p>This behavior is not a flaw — it’s expected. The real question is how we design systems around it.</p><h3>What RAG Actually Solves (and Why It Works So Well)</h3><p>Retrieval-Augmented Generation works because it <strong>forces the model to ground its answers in real, retrievable data</strong>. Instead of relying on memory, the model reasons over provided context. In production, this was the single most effective hallucination reduction strategy I implemented.</p><pre>query_embedding = embed(user_query)<br>documents = vector_db.search(query_embedding, top_k=3)<br>prompt = f&quot;&quot;&quot;<br>Answer using ONLY the following documents:<br>{documents}<br>&quot;&quot;&quot;</pre><p>When the model is constrained to known data, hallucinations collapse dramatically. If the data isn’t present, the model has nothing to invent from.</p><h3>RAG Scales Because Knowledge Is Decoupled from the Model</h3><p>One of the biggest operational wins with RAG is that <strong>knowledge updates don’t require model retraining</strong>. I’ve shipped systems where content changes daily — policies, pricing, compliance rules — and RAG handled it seamlessly.</p><pre>Update knowledge base → re-embed documents → done</pre><p>No downtime. No retraining cycles. No risk of model drift from outdated facts.</p><h3>Where RAG Struggles in Real Systems</h3><p>RAG is not a silver bullet. It struggles when:</p><ul><li>Retrieval quality is poor</li><li>Context windows are too small</li><li>Reasoning depends on learned patterns rather than facts</li></ul><pre>if retrieved_docs is empty:<br>    return &quot;unknown&quot;</pre><p>Without high-quality embeddings and retrieval logic, RAG can fail just as badly — only more quietly.</p><h3>What Fine-Tuning Actually Improves</h3><p>Fine-tuning shines when the problem is <strong>behavioral consistency</strong>, not factual recall. I use fine-tuning to teach models how to respond, not what to know.</p><pre>{<br>  &quot;input&quot;: &quot;Classify transaction risk&quot;,<br>  &quot;output&quot;: {<br>    &quot;risk&quot;: &quot;high&quot;,<br>    &quot;confidence&quot;: 0.87<br>  }<br>}</pre><p>Fine-tuning helps models follow domain-specific language, formatting, and decision logic — but it does not make them factually up-to-date.</p><h3>The Hidden Risk of Fine-Tuning for Knowledge</h3><p>One mistake I made early was fine-tuning models with factual data that changed over time. That knowledge became frozen into the model — and slowly wrong.</p><pre>Fine-tuned fact ≠ live fact</pre><p>Every policy update required retraining. At scale, this is operationally expensive and risky.</p><h3>Fine-Tuning Can Reduce Hallucinations — but Only in Narrow Domains</h3><p>Fine-tuning reduces hallucinations when:</p><ul><li>Outputs must follow strict structure</li><li>Decision boundaries are stable</li><li>Language patterns matter more than facts</li></ul><pre># Enforced response schema learned during fine-tuning<br>{<br>  &quot;decision&quot;: &quot;approve | reject | review&quot;,<br>  &quot;confidence&quot;: float<br>}</pre><p>Outside these boundaries, hallucinations return quickly.</p><h3>RAG vs Fine-Tuning: The Real Decision Framework I Use</h3><p>In real systems, I don’t ask “Which is better?”<br> I ask <strong>“Where does uncertainty come from?”</strong></p><pre>If uncertainty comes from missing facts → RAG<br>If uncertainty comes from inconsistent behavior → Fine-tuning</pre><p>This framing has saved me months of rework.</p><h3>The Most Reliable Pattern: RAG + Fine-Tuning Together</h3><p>At scale, the most stable systems I’ve built combine both approaches. RAG supplies facts. Fine-tuning enforces behavior.</p><pre>facts = retrieve_context(user_query)<br>response = fine_tuned_model.generate(<br>    context=facts,<br>    schema=STRICT_JSON_SCHEMA<br>)</pre><p>This combination dramatically reduces hallucinations while preserving flexibility.</p><h3>Monitoring Hallucinations at Scale</h3><p>No solution is complete without observability. I log confidence scores, missing-context cases, and fallback usage.</p><pre>if response.confidence &lt; 0.5:<br>    log_event(&quot;low_confidence_response&quot;)</pre><p>Hallucinations don’t disappear they surface where your system is weakest.</p><h3>Conclusion: Hallucinations Don’t Go Away — They Get Engineered Around</h3><p>RAG and Fine-Tuning are not competing ideas. They are complementary tools for controlling uncertainty. RAG keeps models honest. Fine-tuning keeps them disciplined. At scale, hallucination prevention is less about AI intelligence and more about <strong>system architecture, data flow, and constraints</strong>.</p><p>The most reliable AI systems I’ve built weren’t smarter.<br> They were <strong>better engineered</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47bc78c59c5b" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/rag-vs-fine-tuning-preventing-hallucinations-at-scale-47bc78c59c5b">RAG vs Fine-Tuning: Preventing Hallucinations at Scale</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing High Performance APIs with Java and Spring Boot]]></title>
            <link>https://medium.com/javarevisited/designing-high-performance-apis-with-java-and-spring-boot-d60003b8b727?source=rss-6e6507dbd466------2</link>
            <guid isPermaLink="false">https://medium.com/p/d60003b8b727</guid>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Manalimran]]></dc:creator>
            <pubDate>Mon, 19 Jan 2026 15:29:53 GMT</pubDate>
            <atom:updated>2026-01-19T15:29:53.418Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>How real-world performance comes from architecture, discipline, and understanding the JVM not shortcuts</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iyk3T6qeGiQYX_nRbGxH4A.png" /><figcaption>Image Generated by AI</figcaption></figure><h3>Introduction: Performance Is Rarely a Framework Problem</h3><p>Early in my career, I believed API performance was about choosing the “right” framework. Over time, Java and Spring Boot taught me a harder truth: <strong>most performance problems are architectural, not technical</strong>. Spring Boot is fast enough for almost any workload what determines success is how you design threading, data access, serialization, caching, and failure handling. High-performance APIs aren’t built by tuning one parameter; they emerge from a series of deliberate engineering decisions.</p><h3>Start with a Performance Oriented API Contract</h3><p>Before writing code, I define the API contract with performance in mind. Over-fetching data, bloated responses, and ambiguous endpoints are silent performance killers.</p><pre>@GetMapping(&quot;/users/{id}&quot;)<br>public UserSummary getUser(@PathVariable Long id) {<br>    return userService.getUserSummary(id);<br>}</pre><p>Returning <strong>only what the client needs</strong> reduces serialization cost, network latency, and memory pressure. Performance begins at the contract — not the database.</p><h3>Controller Layer: Keep It Thin and Predictable</h3><p>Spring Boot controllers should not contain business logic. Thin controllers are easier to optimize, cache, and scale.</p><pre>@RestController<br>@RequestMapping(&quot;/api/orders&quot;)<br>public class OrderController {<br>@GetMapping(&quot;/{id}&quot;)<br>    public OrderDTO getOrder(@PathVariable Long id) {<br>        return orderService.fetchOrder(id);<br>    }<br>}</pre><p>This separation allows the service layer to evolve independently, enabling better caching and concurrency strategies later.</p><h3>Service Layer Design: Idempotency and Statelessness Matter</h3><p>High-performance APIs scale horizontally. That only works when services are stateless and idempotent.</p><pre>public OrderDTO fetchOrder(Long id) {<br>    return orderRepository.findById(id)<br>            .orElseThrow(() -&gt; new NotFoundException());<br>}</pre><p>Stateless services reduce synchronization needs and allow aggressive scaling without shared-memory bottlenecks.</p><h3>Database Access: The Real Bottleneck in Most APIs</h3><p>In production systems, the database not Java is usually the slowest component. Efficient APIs minimize round trips and avoid unnecessary joins.</p><pre>@Query(&quot;SELECT new com.app.OrderDTO(o.id, o.status) FROM Order o WHERE o.id = :id&quot;)<br>OrderDTO findOrderSummary(@Param(&quot;id&quot;) Long id);</pre><p>Projection queries dramatically reduce memory usage and serialization time.</p><h3>Connection Pooling: Your First Line of Defense</h3><p>Without proper connection pooling, APIs collapse under load. I always tune HikariCP explicitly.</p><pre>spring:<br>  datasource:<br>    hikari:<br>      maximum-pool-size: 20<br>      minimum-idle: 5<br>      connection-timeout: 30000</pre><p>A well-sized pool prevents thread starvation and protects downstream systems.</p><h3>Caching: Performance Without Complexity</h3><p>Caching is the highest ROI performance optimization when done correctly.</p><pre>@Cacheable(value = &quot;users&quot;, key = &quot;#id&quot;)<br>public UserSummary getUserSummary(Long id) {<br>    return userRepository.findSummaryById(id);<br>}</pre><p>Spring’s caching abstraction allows performance improvements without polluting business logic.</p><h3>Asynchronous Processing: Freeing Request Threads</h3><p>Blocking threads kill throughput. I use async execution for non-critical tasks.</p><pre>@Async<br>public void publishAuditEvent(Order order) {<br>    auditService.send(order);<br>}</pre><p>This keeps request-response cycles fast and predictable.</p><h3>Threading Model: Respect the JVM</h3><p>High-performance APIs respect thread limits. More threads ≠ more performance.</p><pre>@Bean<br>public Executor taskExecutor() {<br>    return Executors.newFixedThreadPool(10);<br>}</pre><p>Controlled concurrency prevents CPU thrashing and GC pressure.</p><h3>Serialization: JSON Is Not Free</h3><p>Serialization cost grows with object size. I avoid exposing entities directly.</p><pre>public record UserDTO(Long id, String name) {}</pre><p>Java records reduce boilerplate and improve serialization clarity.</p><h3>HTTP Layer Optimizations</h3><p>Simple HTTP optimizations add up at scale.</p><pre>@GetMapping(value = &quot;/health&quot;, produces = MediaType.TEXT_PLAIN_VALUE)<br>public String health() {<br>    return &quot;OK&quot;;<br>}</pre><p>Avoid unnecessary JSON when plain text will do.</p><h3>Error Handling Without Performance Penalties</h3><p>Exception-heavy flows are expensive. I design APIs where errors are exceptional — not expected.</p><pre>@ResponseStatus(HttpStatus.NOT_FOUND)<br>public class NotFoundException extends RuntimeException {}</pre><p>Clear error paths reduce overhead and improve observability.</p><h3>Observability: You Can’t Optimize What You Can’t See</h3><p>Metrics reveal real bottlenecks.</p><pre>@Timed(value = &quot;api.order.fetch&quot;)<br>public OrderDTO fetchOrder(Long id) {<br>    return orderRepository.findById(id).orElseThrow();<br>}</pre><p>Spring Boot Actuator and Micrometer are essential for performance tuning.</p><h3>Graceful Degradation Under Load</h3><p>High-performance APIs don’t crash — they degrade.</p><pre>if (systemLoadHigh()) {<br>    return cachedFallbackResponse();<br>}</pre><p>This approach protects core functionality during traffic spikes.</p><h3>JVM Tuning: The Final 10%</h3><p>Most performance wins come from architecture, but JVM tuning matters at scale.</p><pre>-XX:+UseG1GC<br>-XX:MaxGCPauseMillis=200</pre><p>Understanding GC behavior prevents latency spikes under load.</p><h3>Security Without Killing Performance</h3><p>Security checks must be efficient.</p><pre>@PreAuthorize(&quot;hasRole(&#39;USER&#39;)&quot;)<br>public OrderDTO getOrder(Long id) { }</pre><p>Spring Security, when configured correctly, adds minimal overhead.</p><h3>Conclusion: High Performance Is a System Property</h3><p>Designing high-performance APIs with Java and Spring Boot taught me that speed is not about clever code — it’s about <strong>discipline, boundaries, and respect for the runtime</strong>. Java provides predictability. Spring Boot provides structure. When combined with thoughtful design, they produce APIs that scale gracefully, survive traffic spikes, and age well over years.</p><p>Fast APIs are not hacked together.<br> They are <strong>engineered deliberately</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d60003b8b727" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/designing-high-performance-apis-with-java-and-spring-boot-d60003b8b727">Designing High Performance APIs with Java and Spring Boot</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>