Skip to content

Typer

Command-line interfaces generated from type-hinted function signatures.

Developer UtilitiesCLI/UtilsPython

What it is

Typer is a modern Python library for building command-line interface (CLI) applications. It leverages Python type hints to automatically generate help messages, validate inputs, and provide a smooth developer experience.

Typer allows you to define commands and options as Python functions with decorators. It automatically generates help pages, validates input types, and integrates easily with modern Python codebases.

Built on
Click
Licence
MIT

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Building a CLI where you would rather annotate a function than configure a parser
  • You already use FastAPI and want the same style on the command line

Look elsewhere when

  • You need the full flexibility of Click's decorator API for an unusual command structure

Installation

pip install typer[all]

Getting started

The smallest useful thing you can do with it, and what each part means.

Simple CLI command
import typer

def main():
    typer.echo('Hello, Typer!')

if __name__ == '__main__':
    typer.run(main)
Defines a simple command-line program that prints 'Hello, Typer!' when executed.
Command with options
import typer

def greet(name: str = 'World'):
    typer.echo(f'Hello, {name}!')

if __name__ == '__main__':
    typer.run(greet)
Adds an optional parameter `name` to the CLI command, defaulting to 'World'. Typer automatically validates input and generates help text.

Advanced usage

Where the library earns its place over a simpler alternative.

Multiple commands using Typer app
import typer
app = typer.Typer()

@app.command()
def hello():
    typer.echo('Hello!')

@app.command()
def goodbye():
    typer.echo('Goodbye!')

if __name__ == '__main__':
    app()
Defines a Typer application with multiple commands using the `@app.command()` decorator.
Type validation and conversion
import typer

def square(number: int):
    typer.echo(number ** 2)

if __name__ == '__main__':
    typer.run(square)
Automatically validates that `number` is an integer and computes its square. Typer will raise an error if input cannot be converted.
Using Enum for choices
from enum import Enum
import typer

class Color(str, Enum):
    red = 'red'
    green = 'green'
    blue = 'blue'

def favorite(color: Color):
    typer.echo(f'Your favorite color is {color}')

if __name__ == '__main__':
    typer.run(favorite)
Restricts input to predefined choices using Python Enums and provides automatic help messages.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

ValueError
Ensure input types match function annotations. Typer automatically handles conversion and raises errors if inputs are invalid.
typer.Exit
Use `raise typer.Exit(code=...)` to exit gracefully with a specific exit code.

Best practices

  • Use Python type hints to enable automatic validation and documentation.
  • Organize multiple commands with a Typer app instance instead of standalone functions.
  • Leverage default values and Enums to guide user input.
  • Use `typer.echo()` instead of `print()` for consistent CLI output.
  • Combine Typer with rich or colorama for styled terminal outputs.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Typer was created by Sebastián Ramírez, the author of FastAPI, to make building CLI apps as easy as building web APIs with FastAPI. It emphasizes simplicity, automatic documentation, and type safety while being fully compatible with Python’s type hints.