google/python-fire · error · FireExit

2

2

Error message

Fire trace and error output displayed on stderr before exiting

What it means

When the component trace produced during command execution contains an error, Fire displays the error and raises FireExit with code 2, printing the Fire trace and help/error output to stderr. This is the standard non-zero exit path for any exception or usage failure that occurs while Fire executes your callable.

Source

Thrown at fire/core.py:139

  argparser = parser.CreateParser()
  parsed_flag_args, unused_args = argparser.parse_known_args(flag_args)

  context = {}
  if parsed_flag_args.interactive or component is None:
    # Determine the calling context.
    caller = inspect.stack()[1]
    caller_frame = caller[0]
    caller_globals = caller_frame.f_globals
    caller_locals = caller_frame.f_locals
    context.update(caller_globals)
    context.update(caller_locals)

  component_trace = _Fire(component, args, parsed_flag_args, context, name)

  if component_trace.HasError():
    _DisplayError(component_trace)
    raise FireExit(2, component_trace)
  if component_trace.show_trace and component_trace.show_help:
    output = [f'Fire trace:\n{component_trace}\n']
    result = component_trace.GetResult()
    help_text = helptext.HelpText(
        result, trace=component_trace, verbose=component_trace.verbose)
    output.append(help_text)
    Display(output, out=sys.stderr)
    raise FireExit(0, component_trace)
  if component_trace.show_trace:
    output = [f'Fire trace:\n{component_trace}']
    Display(output, out=sys.stderr)
    raise FireExit(0, component_trace)
  if component_trace.show_help:
    result = component_trace.GetResult()
    help_text = helptext.HelpText(
        result, trace=component_trace, verbose=component_trace.verbose)
    output = [help_text]
    Display(output, out=sys.stderr)

View on GitHub (pinned to 716bbc23d7)

Solutions

  1. Read the Fire trace on stderr to find which component/arg failed
  2. Fix the offending argument values, names, or types in the invocation
  3. Wrap risky component logic to convert exceptions into clearer messages
  4. In callers, catch fire.core.FireExit and inspect .code and the trace

Example fix

// before
fire.Fire(MyService)  # exits with code 2 on any failure
// after
try:
    fire.Fire(MyService)
except fire.core.FireExit as e:
    print(f'fire failed with code {e.code}', file=sys.stderr)
    sys.exit(e.code)
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try:
    fire.Fire(component)
except fire.core.FireExit as e:
    sys.stderr.write(str(e.trace))
    sys.exit(e.code)  # 2

Prevention

When it happens

Trigger: Any exception raised inside the target component during fire.Fire() execution (TypeError from wrong args, FireError from consumption failures, user exceptions); also when the traced command fails to resolve and trace/help output is shown.

Common situations: CI pipelines where the fired command fails and scripts check exit code 2; typos in member/flag names causing resolution failure; wrong number/type of arguments to the fired function.

Related errors


AI-assisted analysis of google/python-fire@716bbc23d7 (2026-08-28). Data as JSON: /api/errors/7ab5e8597b57f9f7. Report an issue: GitHub.