KatInterview

Layered REST API for Text Condensing with FastAPI

Data Structure DesignModel answer
MediumModel answerData Structure Designfastapipythonpydantic

Last updated

Design a small FastAPI service that accepts text, returns a placeholder-generated summary, validates input, handles errors, and keeps a bounded in-memory history, using clear layers.

Task

Build a small HTTP service in Python with FastAPI that condenses text. The condensing logic can be a simple stand-in (no real model is required), but the service around it should look like something you would be comfortable shipping.

Contract

  • Endpoint: POST /summarize
  • Request body (JSON): { "text": string }
  • Response body (JSON): { "summary": string }
  • Every request and its resulting response must be kept in memory. No database.

Engineering expectations

  • Separate the code into a router layer, a service layer and a models layer.
  • Use type hints everywhere.
  • Validate requests and shape responses with Pydantic models.
  • Deal sensibly with invalid input and with unexpected failures.
  • Keep the code modular, readable and simple.

What to deliver

  1. The proposed directory and file layout.
  2. The code for each file.
  3. A short explanation of your design choices (validation, error boundaries, state handling, testability).

Hints

Hint 1

Keep the HTTP concerns (status codes, HTTPException) in the router only; the service should raise domain exceptions and know nothing about HTTP.

Hint 2

In-memory state is shared across requests, and sync FastAPI endpoints run in a threadpool. Think about what protects the store from races and what stops it from growing forever.

Hint 3

Let Pydantic reject bad input (missing field, wrong type, blank or oversized text, extra fields) and add a global exception handler so unexpected errors are logged but never leak stack traces.

Hint 4

Inject the service and store with Depends so a test can override them with fakes.