Skip to main content

Configuring Developer Exceptions

To customize how exceptions are displayed during development in a Typer application, you can configure the Typer instance with specific "pretty exception" parameters. These settings control whether tracebacks are simplified, if local variables are displayed, and whether the formatting is enhanced using the Rich library.

Customizing Exception Display

You can configure exception behavior directly in the typer.Typer constructor. By default, Typer enables pretty exceptions and shortens tracebacks to hide internal library frames.

import typer

# Configure the app to show local variables in tracebacks
app = typer.Typer(
pretty_exceptions_show_locals=True,
pretty_exceptions_short=False,
pretty_exceptions_enable=True
)

@app.command()
def calculate(x: int, y: int):
# This will raise a ZeroDivisionError if y is 0
result = x / y
print(f"Result: {result}")

if __name__ == "__main__":
app()

Configuration Options

The typer.models.DeveloperExceptionConfig class stores these settings, which are passed from the Typer constructor:

  • pretty_exceptions_enable: (Default: True) Toggles the custom exception hook. If False, Typer uses the standard Python traceback.
  • pretty_exceptions_show_locals: (Default: False) When True, the traceback includes the values of local variables for each frame. This is highly useful for debugging but should be used cautiously if your code handles sensitive data (like passwords or tokens).
  • pretty_exceptions_short: (Default: True) When True, Typer filters out internal stack frames from typer and click to focus on your application code. Set to False to see the full stack trace.

Disabling Pretty Exceptions Globally

If you prefer standard Python tracebacks across all Typer applications without modifying the code, you can use environment variables. This is useful for CI/CD environments or specific terminal setups.

Set either of these environment variables to any value (e.g., 1 or True):

export TYPER_STANDARD_TRACEBACK=1
# OR
export _TYPER_STANDARD_TRACEBACK=1

The except_hook in typer/main.py checks these variables before applying any custom formatting:

# From typer/main.py
standard_traceback = os.getenv(
"TYPER_STANDARD_TRACEBACK", os.getenv("_TYPER_STANDARD_TRACEBACK")
)
if (
standard_traceback
or not exception_config
or not exception_config.pretty_exceptions_enable
):
_original_except_hook(exc_type, exc_value, tb)
return

Showing Full Tracebacks

To see every frame in the stack, including those inside Typer and Click, disable the "short" exception mode:

import typer

# Disable short tracebacks to see internal library frames
app = typer.Typer(pretty_exceptions_short=False)

@app.command()
def main():
raise RuntimeWarning("Something went wrong deep in the stack")

if __name__ == "__main__":
app()

Troubleshooting

Missing Rich Formatting

Pretty exceptions rely on the rich library for colors and local variable display. If rich is not installed, Typer falls back to a simplified text-based traceback that still attempts to respect the pretty_exceptions_short setting by hiding internal frames, but it will lack the advanced formatting.

Exceptions Outside of app()

The custom exception hook is installed when the Typer app is called (e.g., app()). If an exception occurs during module import or before the app() call, the standard Python exception handler will be used instead.

import typer

app = typer.Typer()

# This exception happens BEFORE app() is called.
# It will use the standard Python traceback, NOT Typer's pretty exceptions.
# raise ValueError("Early error")

@app.command()
def main():
raise ValueError("Command error")

if __name__ == "__main__":
app() # The hook is active here