<?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: Aashutosh Ojha</title>
    <description>The latest articles on DEV Community by Aashutosh Ojha (@aashutosh_ojha).</description>
    <link>https://dev.to/aashutosh_ojha</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%2F4269320%2Ffb55ba96-43e9-4e09-a265-48cb7a773281.png</url>
      <title>DEV Community: Aashutosh Ojha</title>
      <link>https://dev.to/aashutosh_ojha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aashutosh_ojha"/>
    <language>en</language>
    <item>
      <title>How I Built an Async Company Brochure Generator with FastAPI, Gemini, and Live Token Streaming</title>
      <dc:creator>Aashutosh Ojha</dc:creator>
      <pubDate>Fri, 27 Nov 2026 16:46:06 +0000</pubDate>
      <link>https://dev.to/aashutosh_ojha/how-i-built-an-async-company-brochure-generator-with-fastapi-gemini-and-live-token-streaming-4j7l</link>
      <guid>https://dev.to/aashutosh_ojha/how-i-built-an-async-company-brochure-generator-with-fastapi-gemini-and-live-token-streaming-4j7l</guid>
      <description>&lt;p&gt;Most LLM tutorials stop inside a Jupyter notebook. That is great for experimenting, but real AI engineering starts when you turn a prototype into a fast, resilient backend service that handles messy web data, rate limits, and live streaming.&lt;/p&gt;

&lt;p&gt;Recently, I built an asynchronous &lt;strong&gt;AI Company Brochure Generator API&lt;/strong&gt; using &lt;strong&gt;FastAPI&lt;/strong&gt;, &lt;strong&gt;AsyncOpenAI&lt;/strong&gt;, &lt;strong&gt;Google Gemini&lt;/strong&gt;, and &lt;strong&gt;BeautifulSoup4&lt;/strong&gt;. Given a company name and URL, it scrapes the landing page, uses an LLM to filter only high-value sub-pages (like About and Careers), scrapes those pages, and streams back a structured Markdown brochure in real time.&lt;/p&gt;

&lt;p&gt;Here is how the architecture works and the real-world engineering lessons I ran into along the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Two-Stage LLM Pipeline
&lt;/h3&gt;

&lt;p&gt;Sending every single link on a company website to an LLM wastes tokens, slows down responses, and adds noise. Instead, I split the workflow into two focused stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stage 1 — Intelligent Link Filtering (Structured JSON):&lt;/strong&gt; &lt;code&gt;httpx&lt;/code&gt; and &lt;code&gt;BeautifulSoup4&lt;/code&gt; fetch the homepage HTML, strip out script and style tags to save tokens, and extract all links. A lightweight LLM call (&lt;code&gt;gemini-3.5-flash-lite&lt;/code&gt;) with JSON mode filters out privacy policies and social media links, returning only brochure-relevant URLs:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"links"&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="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"about us"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://example.com"&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="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"careers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://example.com"&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;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stage 2 — Context Assembly &amp;amp; Brochure Synthesis:&lt;/strong&gt; The service fetches the text of those filtered pages, combines them with the landing page content, truncates the context to stay within token budgets, and generates a clean Markdown brochure tailored for clients, investors, and recruits.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2. True End-to-End Streaming with &lt;code&gt;yield&lt;/code&gt; and &lt;code&gt;StreamingResponse&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Waiting 10 seconds for an LLM to finish writing an entire document feels slow to users. To stream tokens as soon as they are predicted, I connected three pieces across the backend:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;stream=True&lt;/code&gt; on &lt;code&gt;AsyncOpenAI&lt;/code&gt; so the provider drips token deltas as they are generated.&lt;/li&gt;
&lt;li&gt;A Python async generator (&lt;code&gt;yield&lt;/code&gt;) that catches each chunk and immediately passes it upward:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_brochure_stream&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="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="n"&gt;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;gemini&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&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="s2"&gt;"gemini-3.5-flash-lite"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;messages&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="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"system"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;brochure_system_prompt&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;"role"&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="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;get_brochure_prompt&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="n"&gt;url&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="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;stream&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="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;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;content&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;choices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;content&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;content&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;FastAPI's &lt;code&gt;StreamingResponse&lt;/code&gt; (&lt;code&gt;media_type="text/plain"&lt;/code&gt;), which keeps the HTTP connection open and flushes every yielded word directly to the client.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  3. Production Lesson: Handling 503 Demand Spikes with Multi-Provider Fallback
&lt;/h3&gt;

&lt;p&gt;While testing on free-tier endpoints during peak global hours, I hit real &lt;code&gt;503 UNAVAILABLE&lt;/code&gt; high-demand errors. Rather than letting the API fail, I took advantage of the OpenAI-compatible endpoint standard:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;AsyncOpenAI&lt;/code&gt; with Google's OpenAI-compatible &lt;code&gt;base_url&lt;/code&gt; lets me use the exact same SDK for both &lt;strong&gt;Google Gemini&lt;/strong&gt; and &lt;strong&gt;Groq&lt;/strong&gt; (&lt;code&gt;llama-3.3-70b-versatile&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Wrapping the primary Gemini call in a &lt;code&gt;try/except&lt;/code&gt; block allows Groq to seamlessly take over whenever upstream servers spike.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Key Takeaways
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prototype in notebooks, ship in &lt;code&gt;.py&lt;/code&gt;:&lt;/strong&gt; Jupyter is unmatched for testing prompts and inspecting scraped HTML, while FastAPI + Pydantic v2 enforces strict input validation (&lt;code&gt;HttpUrl&lt;/code&gt;) and clean REST/streaming contracts in production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always clean HTML before sending to an LLM:&lt;/strong&gt; Removing script, style, and SVG tags dramatically cuts token usage and improves output quality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never forget &lt;code&gt;await&lt;/code&gt; on async pipelines:&lt;/strong&gt; Mixing synchronous and asynchronous calls in FastAPI quickly leads to blocking event loops or unawaited coroutine bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the full source code and setup instructions on GitHub:&lt;br&gt;
&lt;a href="https://github.com/AshutoshOjha77/AI-Brochure-generator" rel="noopener noreferrer"&gt;https://github.com/AshutoshOjha77/AI-Brochure-generator&lt;/a&gt; &lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>programming</category>
      <category>llm</category>
    </item>
  </channel>
</rss>
