<?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>Forem: autozbudoucnosti</title>
    <description>The latest articles on Forem by autozbudoucnosti (@autozbudoucnosti).</description>
    <link>https://forem.com/autozbudoucnosti</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3764781%2Fce200cf5-4604-48c5-b85b-37d13da21645.jpg</url>
      <title>Forem: autozbudoucnosti</title>
      <link>https://forem.com/autozbudoucnosti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/autozbudoucnosti"/>
    <language>en</language>
    <item>
      <title>Stop Letting AI Hallucinate Your Sustainability Data: Why I Built a Deterministic API</title>
      <dc:creator>autozbudoucnosti</dc:creator>
      <pubDate>Tue, 10 Feb 2026 17:44:24 +0000</pubDate>
      <link>https://forem.com/autozbudoucnosti/stop-letting-ai-hallucinate-your-sustainability-data-why-i-built-a-deterministic-api-38mi</link>
      <guid>https://forem.com/autozbudoucnosti/stop-letting-ai-hallucinate-your-sustainability-data-why-i-built-a-deterministic-api-38mi</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.amazonaws.com%2Fuploads%2Farticles%2Fjjg9l5elumam623u4xtc.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.amazonaws.com%2Fuploads%2Farticles%2Fjjg9l5elumam623u4xtc.png" alt=" " width="800" height="929"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem: AI Guesses, Math Doesn't&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We live in the age of LLMs. You can ask ChatGPT, "What is the carbon footprint of a cotton t-shirt?" and it will give you a convincing answer. But ask it three times, and you might get three different numbers.&lt;/p&gt;

&lt;p&gt;For e-commerce brands and supply chain tools, "hallucinated" data isn't good enough. You need deterministic, consistent, and auditable numbers.&lt;/p&gt;

&lt;p&gt;That’s why I built the &lt;strong&gt;Product Impact API&lt;/strong&gt;. It’s a dedicated engine that uses hard-coded environmental factors to calculate a consistent "Impact Score" for products based on their materials and shipping weight.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Tech Stack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I chose this stack for speed and type safety:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Language&lt;/strong&gt;: Python 3.12 (because modern Python is fast).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Framework&lt;/strong&gt;: FastAPI (for async performance and auto-generated docs).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Validation&lt;/strong&gt;: Pydantic (to ensure no bad data gets in).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployment&lt;/strong&gt;: Railway (for effortless CI/CD).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The core logic revolves around a weighted scoring system. A product is defined by its material (e.g., Cotton, Polyester) and its weight.&lt;/p&gt;

&lt;p&gt;Here is the Pydantic model that enforces the data structure:&lt;/p&gt;

&lt;p&gt;Python&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 plaintext"&gt;&lt;code&gt;class Product(BaseModel):
    name: str
    material: str
    weight_kg: float
    shipping_distance_km: float

    # Example payload for documentation
    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "name": "Eco T-Shirt",
                    "material": "cotton",
                    "weight_kg": 0.2,
                    "shipping_distance_km": 1500
                }
            ]
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The calculation engine uses a dictionary lookup for materials. This is O(1) complexity—super fast compared to querying an LLM or a heavy database.&lt;/p&gt;

&lt;p&gt;Python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MATERIAL_SCORES = {
    "cotton": 4.5,    # High water usage
    "polyester": 5.5, # Microplastics/Oil based
    "bamboo": 2.0,    # Sustainable
    "recycled": 1.5   # Best option
}

def calculate_impact(product: Product):
    # Base score from material
    base_score = MATERIAL_SCORES.get(product.material.lower(), 5.0)

    # Logistics penalty (0.1 points per 100km)
    transport_penalty = (product.shipping_distance_km / 100) * 0.1

    total_score = base_score * product.weight_kg + transport_penalty
    return round(total_score, 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Challenge: CORS &amp;amp; Security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One hurdle I hit during development was connecting my frontend (a simple HTML/JS dashboard) to the hosted API. I kept getting 401 Unauthorized errors.&lt;/p&gt;

&lt;p&gt;I learned that even for public APIs, you need to be careful with CORS (Cross-Origin Resource Sharing). I had to explicitly allow my frontend's origin in the FastAPI middleware:&lt;/p&gt;

&lt;p&gt;Python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # In production, lock this down!
    allow_methods=["*"],
    allow_headers=["*"],
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also implemented API Key authentication using fastapi.security.api_key to prevent abuse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Result&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The API is now live. It accepts a JSON payload and returns a detailed breakdown of the environmental cost, CO2 estimates, and a "Green Score."&lt;/p&gt;

&lt;p&gt;You can check out the auto-generated Swagger documentation here: &lt;a href="https://web-production-a1f91.up.railway.app/docs#/" rel="noopener noreferrer"&gt;https://web-production-a1f91.up.railway.app/docs#/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's Next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I plan to expand the MATERIAL_SCORES database and add a "Suggestion Engine" that recommends greener alternatives (e.g., "Switching to Bamboo would save 1.2kg of CO2").&lt;/p&gt;

&lt;p&gt;If you're looking for a developer who understands both backend logic and sustainability metrics, feel free to connect!&lt;/p&gt;

</description>
      <category>python</category>
      <category>api</category>
      <category>webdev</category>
      <category>fastapi</category>
    </item>
  </channel>
</rss>
