ScriptsApr 12, 2026·2 min read

Starlette — The Little ASGI Framework That Shines

Starlette is a lightweight ASGI framework for building async web services in Python. It is the foundation that FastAPI is built on top of. Provides routing, middleware, WebSocket, GraphQL, background tasks, and streaming responses.

TL;DR
Lightweight async Python web framework providing routing, middleware, WebSocket, and streaming. The foundation under FastAPI.
§01

What it is

Starlette is a lightweight ASGI framework for building high-performance async web services in Python. It provides routing, middleware, WebSocket support, GraphQL, background tasks, and streaming responses. Most notably, Starlette is the foundation that FastAPI is built on top of, handling all the HTTP-level concerns while FastAPI adds data validation and OpenAPI generation.

Starlette targets Python developers who want async web capabilities without the overhead of a full framework. It is also relevant to anyone using FastAPI who wants to understand or extend the underlying request handling layer.

§02

How it saves time or tokens

Starlette's minimal surface area means less boilerplate to write and maintain. A working API endpoint requires only a route function and a response object. The async-first design handles concurrent requests efficiently without thread pool management. For AI agent backends that need to handle streaming LLM responses, Starlette's native StreamingResponse maps directly to server-sent events without additional dependencies.

§03

How to use

  1. Install Starlette and an ASGI server:
pip install starlette uvicorn
  1. Create a basic application:
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import JSONResponse
import uvicorn

async def list_items(request):
    return JSONResponse([{'name': 'item-1', 'status': 'active'}])

app = Starlette(routes=[Route('/items', list_items)])

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)
  1. Run the server with python app.py and access http://localhost:8000/items.
§04

Example

Streaming response for an AI agent backend:

from starlette.responses import StreamingResponse
import asyncio

async def stream_response(request):
    async def generate():
        for i in range(10):
            yield f'data: chunk {i}\n\n'
            await asyncio.sleep(0.1)
        yield 'data: [DONE]\n\n'

    return StreamingResponse(
        generate(),
        media_type='text/event-stream'
    )

This pattern is commonly used for streaming LLM completions to frontend clients via server-sent events.

§05

Related on TokRepo

§06

Common pitfalls

  • Starlette requires an ASGI server like Uvicorn or Hypercorn. It does not include a built-in server, so forgetting to install one causes import errors at startup.
  • WebSocket handlers must explicitly accept the connection before sending messages. Skipping await websocket.accept() causes silent connection drops.
  • Middleware ordering matters. Authentication middleware must come before route-specific middleware to prevent unauthenticated access to protected endpoints.
  • Always check the official documentation for the latest version-specific changes and migration guides before upgrading in production environments.
  • For team deployments, establish clear guidelines on configuration and usage patterns to ensure consistency across developers.

Frequently Asked Questions

What is the relationship between Starlette and FastAPI?+

FastAPI is built on top of Starlette. Starlette handles HTTP routing, middleware, WebSocket, and responses. FastAPI adds Pydantic data validation, automatic OpenAPI documentation, and dependency injection on top of Starlette's foundation.

Is Starlette fast enough for production?+

Yes. Starlette is one of the fastest Python web frameworks in benchmarks. Its async-first design handles thousands of concurrent connections efficiently. Combined with Uvicorn, it serves as a production-grade ASGI stack.

Does Starlette support WebSocket?+

Yes. Starlette has built-in WebSocket support with a clean async interface. You define WebSocket routes alongside HTTP routes and use the WebSocket object to accept connections, send messages, and receive data.

When should I use Starlette instead of FastAPI?+

Use Starlette when you want minimal overhead and do not need automatic data validation or OpenAPI docs. Starlette is ideal for lightweight services, proxies, streaming endpoints, or WebSocket servers where FastAPI's extras add unnecessary complexity.

Can I use Starlette middleware with FastAPI?+

Yes. Since FastAPI is built on Starlette, all Starlette middleware works directly with FastAPI applications. You can add CORS, authentication, and custom middleware using the same Starlette patterns.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets