Module 19 Exercises: The AI-First API

Module 19 Exercises: The AI-First API

Practical exercises to challenge your understanding of async AI clients, structured output, and streaming data.

Module 19 Exercises: The AI-First API

AI is the most exciting and the most unpredictable part of modern software. These exercises will help you build the "Bridges" that make AI reliable.


Exercise 1: The Structured Summarizer

You are building an API that summarizes law documents.

  1. Define a Pydantic model LegalSummary that includes:
    • case_name (str)
    • ruling_date (ISO Date)
    • summary (str, max 500 chars)
    • is_appealable (bool)
  2. Explain why having this model is better than just returning a raw String from the AI.

Exercise 2: Implementing a Streamer

Write a FastAPI endpoint @app.get("/countdown") that:

  1. Streams the numbers from 10 down to 1.
  2. Sends exactly one number every second.
  3. Uses StreamingResponse.

Exercise 3: The Async Advantage

You have a server with 1 CPU. You receive 50 requests at the same time for an LLM generation that takes 5 seconds.

  1. If you use Async Clients, how long does the last user have to wait to start seeing results?
  2. If you use Sync Clients, how long does the last user have to wait?

Self-Correction / Discussion

Exercise 1 Answer:

  1. class LegalSummary(BaseModel):
        case_name: str
        ruling_date: datetime.date
        summary: str = Field(..., max_length=500)
        is_appealable: bool
    
  2. Structured output allows your Frontend (or another service) to display the date in a specific format, filter by "Appealable," or ensure the summary doesn't break the UI layout. It turns "Text" into "Data."

Exercise 2 Answer:

async def countdown_generator():
    for i in range(10, 0, -1):
        yield f"Seconds left: {i}\n"
        await asyncio.sleep(1)

@app.get("/countdown")
async def countdown():
    return StreamingResponse(countdown_generator())

Exercise 3 Answer:

  1. Async: All 50 users start seeing results in ~5 seconds. The server handles all 50 handshakes "concurrently."
  2. Sync: The last user has to wait 250 seconds (5s * 50). The server must finish the first user before it even looks at the second one. This is why async is mandatory for AI.

Summary of Module 19

You have entered the frontier of development.

  • Async-AI: You know how to wait for slow models without blocking.
  • Structured Outputs: You can tame the "Chaos" of LLMs.
  • Real-Time Streams: You know how to provide instant feedback.

In Module 20: Capstone Design Project, we will put everything together to design a production-grade, AI-powered system from scratch.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn