FastAPI + PydanticAI: Streaming Partially Validated Structured JSON to React Frontends with SSE

FastAPI + PydanticAI: Streaming Partially Validated Structured JSON to React Frontends with SSE

(Updated: ) ๐Ÿ“– 4 min read

Waiting 8 to 15 seconds for an LLM to generate an exhaustive JSON object creates a sluggish, unresponsive user experience. In modern AI applications, users demand immediate progressive visual feedbackโ€”watching summary paragraphs, bulleted risks, and metrics render dynamically on the screen as tokens are generated.

However, streaming structured data introduces a core technical challenge:

  • Streaming raw text prose is trivial (yield chunk.text).
  • But streaming structured data means token fragments like {"summary": "Market exhibits strong are syntactically invalid JSON. Calling JSON.parse() on the frontend will throw immediate syntax errors and crash the UI.

The solution is Progressive Partial-Model Streaming: combining PydanticAIโ€™s built-in partial JSON parser with a FastAPI Server-Sent Events (SSE) endpoint and a resilient React streaming hook.

This guide provides the complete end-to-end implementation for both backend and frontend.


1. The Real-Time Streaming Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       HTTP GET /api/analyze-stream       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  React Browser  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€> โ”‚  FastAPI Server โ”‚
โ”‚                 โ”‚ <โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      text/event-stream (SSE Chunks)      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                                                            โ”‚
         โ”‚ Updates state on every chunk                               โ”‚ run_stream()
         โ–ผ                                                            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Progressive UI  โ”‚                                          โ”‚ PydanticAI      โ”‚
โ”‚ Renders cards,  โ”‚                                          โ”‚ Progressive     โ”‚
โ”‚ lists & scores  โ”‚                                          โ”‚ Partial Parser  โ”‚
โ”‚ in real time    โ”‚                                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. The FastAPI Backend: Progressive Structured Streaming

PydanticAI provides a native run_stream() context manager. Calling stream_structured() yields partially validated snapshots of your target Pydantic model at each token increment:

import asyncio
import json
from typing import List, Optional
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from pydantic_ai import Agent

app = FastAPI(title="PydanticAI Real-Time SSE Gateway")

# Enable CORS for local React development
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 1. Define the Structured Response Contract
class FinancialAnalysisPayload(BaseModel):
    executive_summary: str = Field(description="High-level overview of current asset condition")
    bull_catalysts: List[str] = Field(default_factory=list, description="Key upward drivers")
    bear_risks: List[str] = Field(default_factory=list, description="Downside risk factors")
    target_price_12m: Optional[float] = Field(None, description="Projected 12-month valuation")
    conviction_score: Optional[int] = Field(None, description="Confidence rating from 1 to 100")

# 2. Initialize PydanticAI Agent
analyst_agent = Agent(
    "google-gla:gemini-2.5-flash",
    result_type=FinancialAnalysisPayload,
    system_prompt=(
        "You are a Wall Street quantitative equities analyst. "
        "Provide thorough, data-driven financial research structured precisely into the requested format."
    )
)

# 3. Streaming SSE Endpoint
@app.get("/api/v1/analyze-equity-stream")
async def stream_equity_analysis(ticker: str):
    """Streams progressive partial JSON snapshots to frontend via SSE."""
    
    async def sse_event_generator():
        prompt = f"Conduct a comprehensive technical and fundamental valuation analysis of {ticker}."
        
        try:
            async with analyst_agent.run_stream(prompt) as stream:
                # stream_structured() emits progressive Pydantic model instances
                async for partial_snapshot in stream.stream_structured():
                    # Serialize partial snapshot to JSON string
                    payload_json = partial_snapshot.model_dump_json()
                    
                    # Yield standards-compliant SSE event
                    yield f"event: partial\ndata: {payload_json}\n\n"
                    await asyncio.sleep(0.015)  # Yield control to event loop

            # Emit terminal completion event
            yield "event: complete\ndata: {}\n\n"

        except Exception as exc:
            error_data = json.dumps({"error": str(exc)})
            yield f"event: error\ndata: {error_data}\n\n"

    headers = {
        "Content-Type": "text/event-stream",
        "Cache-Control": "no-cache, no-transform",
        "Connection": "keep-alive",
        "X-Accel-Buffering": "no"  # Crucial: Disables Nginx reverse proxy buffering
    }

    return StreamingResponse(sse_event_generator(), headers=headers)

3. The React Frontend: TypeScript Streaming Hook

To consume an SSE stream with high reliability, use the fetch() API with a ReadableStreamDefaultReader rather than the legacy EventSource API (which lacks support for custom headers and granular error handling):

import { useState, useCallback } from 'react';

export interface FinancialAnalysis {
  executive_summary?: string;
  bull_catalysts?: string[];
  bear_risks?: string[];
  target_price_12m?: number;
  conviction_score?: number;
}

export function useStructuredAnalysis() {
  const [data, setData] = useState<FinancialAnalysis | null>(null);
  const [isStreaming, setIsStreaming] = useState<boolean>(false);
  const [error, setError] = useState<string | null>(null);

  const startStream = useCallback(async (ticker: string) => {
    setIsStreaming(true);
    setError(null);
    setData(null);

    try {
      const response = await fetch(`http://localhost:8000/api/v1/analyze-equity-stream?ticker=${encodeURIComponent(ticker)}`);
      if (!response.ok || !response.body) {
        throw new Error(`HTTP network error: ${response.statusText}`);
      }

      const reader = response.body.getReader();
      const decoder = new TextDecoder();
      let buffer = '';

      while (true) {
        const { value, done } = await reader.read();
        if (done) break;

        buffer += decoder.decode(value, { stream: true });
        const lines = buffer.split('\n\n');
        buffer = lines.pop() || ''; // Keep partial line in buffer

        for (const block of lines) {
          if (!block.trim()) continue;

          const match = block.match(/^event:\s*(\w+)\s*\ndata:\s*(.*)$/s);
          if (!match) continue;

          const [, eventType, rawData] = match;

          if (eventType === 'partial') {
            try {
              const parsed: FinancialAnalysis = JSON.parse(rawData);
              setData(parsed);
            } catch (err) {
              console.warn('Skipping malformed SSE frame', err);
            }
          } else if (eventType === 'complete') {
            setIsStreaming(false);
          } else if (eventType === 'error') {
            setError(rawData);
            setIsStreaming(false);
          }
        }
      }
    } catch (err: any) {
      setError(err.message || 'Stream processing failed');
    } finally {
      setIsStreaming(false);
    }
  }, []);

  return { data, isStreaming, error, startStream };
}

4. Building the Real-Time React Dashboard Component

With the hook in place, building an interactive progressive dashboard is simple:

import React, { useState } from 'react';
import { useStructuredAnalysis } from './useStructuredAnalysis';

export const EquityDashboard: React.FC = () => {
  const [ticker, setTicker] = useState('NVDA');
  const { data, isStreaming, error, startStream } = useStructuredAnalysis();

  return (
    <div style=>
      <h2>Real-Time Equity Research Terminal</h2>
      
      <div style=>
        <input
          value={ticker}
          onChange={(e) => setTicker(e.target.value.toUpperCase())}
          style=
          placeholder="Ticker (e.g. AAPL)"
        />
        <button
          onClick={() => startStream(ticker)}
          disabled={isStreaming}
          style=
        >
          {isStreaming ? 'Streaming Research...' : 'Run Analysis'}
        </button>
      </div>

      {error && <div style=>Error: {error}</div>}

      {data && (
        <div style=>
          {/* Executive Summary */}
          <h3>Executive Summary</h3>
          <p>{data.executive_summary || <span style=>Generating summary...</span>}</p>

          {/* Catalysts Grid */}
          <div style=>
            <div>
              <h4 style=>Bullish Catalysts</h4>
              <ul>
                {data.bull_catalysts?.map((catalyst, idx) => (
                  <li key={idx}>{catalyst}</li>
                ))}
              </ul>
            </div>

            <div>
              <h4 style=>Downside Risks</h4>
              <ul>
                {data.bear_risks?.map((risk, idx) => (
                  <li key={idx}>{risk}</li>
                ))}
              </ul>
            </div>
          </div>

          {/* Key Metrics */}
          <div style=>
            <div>
              <strong>Target Price (12M): </strong>
              {data.target_price_12m ? `$${data.target_price_12m}` : 'Calculating...'}
            </div>
            <div>
              <strong>Analyst Conviction: </strong>
              {data.conviction_score ? `${data.conviction_score} / 100` : 'Evaluating...'}
            </div>
          </div>
        </div>
      )}
    </div>
  );
};

5. Nginx Production Reverse Proxy Configuration

When deploying behind an Nginx reverse proxy, Nginx defaults to buffering backend responses until buffers fill, completely breaking SSE real-time streaming and forcing users to wait until the request finishes.

Add the following directives to your Nginx location block:

location /api/v1/analyze-equity-stream {
    proxy_pass http://fastapi_backend:8000;
    
    # 1. Disable Nginx Proxy Buffering
    proxy_buffering off;
    proxy_cache off;
    
    # 2. Enforce HTTP/1.1 for persistent chunked connections
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    
    # 3. Increase timeouts for long-running generative sessions
    proxy_read_timeout 300s;
    proxy_send_timeout 300s;
    
    # 4. Forward host headers
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

By enforcing proxy_buffering off; and pairing PydanticAIโ€™s stream_structured() with React state reducers, users receive immediate, responsive UI updates with zero parsing crashes.

WEEKLY NEWSLETTER

Get Weekly AI Architect Cost & Strategy Updates

Join 14,000+ developers receiving weekly, data-driven cost-reduction blueprints and production-ready agent guidelines.

Professor XAI
Professor XAI ML Engineer passionate about advancing AI technologies and building intelligent systems.
comments powered by Disqus