google/python-fire · error · FireError
The argument `serialize` must be empty or callable:
Error message
The argument `serialize` must be empty or callable:
What it means
The optional `serialize` parameter of fire.Fire() must be either empty/None or a callable that transforms the result before display. Passing a non-callable truthy value (e.g. a string like 'json' or True) raises this FireError because Fire cannot use it to format output.
Source
Thrown at fire/core.py:249
if show_help:
component_trace.show_help = True
command = f'{component_trace.GetCommand()} -- --help'
print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
file=sys.stderr)
return show_help
def _PrintResult(component_trace, verbose=False, serialize=None):
"""Prints the result of the Fire call to stdout in a human readable way."""
# TODO(dbieber): Design human readable deserializable serialization method
# and move serialization to its own module.
result = component_trace.GetResult()
# Allow users to modify the return value of the component and provide
# custom formatting.
if serialize:
if not callable(serialize):
raise FireError(
'The argument `serialize` must be empty or callable:', serialize)
result = serialize(result)
if value_types.HasCustomStr(result):
# If the object has a custom __str__ method, rather than one inherited from
# object, then we use that to serialize the object.
print(str(result))
return
if isinstance(result, (list, set, frozenset, types.GeneratorType)):
for i in result:
print(_OneLineResult(i))
elif inspect.isgeneratorfunction(result):
raise NotImplementedError
elif isinstance(result, dict) and value_types.IsSimpleGroup(result):
print(_DictAsString(result, verbose))
elif isinstance(result, tuple):
print(_OneLineResult(result))View on GitHub (pinned to 716bbc23d7)
Solutions
- Pass a function: serialize=lambda result: json.dumps(result)
- Remove the serialize argument if default formatting is fine
- Use --serialize flag only when the receiving callable is validated
Example fix
// before fire.Fire(component, serialize='json') // after fire.Fire(component, serialize=lambda result: json.dumps(result))
Defensive patterns
Strategy: type-guard
Validate before calling
if serialize is not None and not callable(serialize):
raise TypeError('serialize must be callable')
fire.Fire(component, serialize=serialize) Type guard
def is_serializer(x) -> bool:
return x is None or callable(x) Try / catch
try:
fire.Fire(component, serialize=serialize)
except fire.core.FireError as e:
if 'serialize' in str(e):
fire.Fire(component)
else:
raise Prevention
- Always pass a function (e.g. lambda r: json.dumps(r)), never a string format name
- Remember serialize transforms the value, it is not a format enum
- Unit-test custom serializers before wiring them into the CLI
When it happens
Trigger: fire.Fire(component, serialize='json'), serialize=True, or passing a variable that is expected to be a function but isn't.
Common situations: Misunderstanding serialize as a format name instead of a formatting function; wiring a config string into serialize; older code written for a custom formatting flag.
Related errors
- The command argument must be a string or a sequence of argum
- Given file path does not exist.
- Unable to load module from specified path.
- Fire can only be called on .py files.
- Fire was passed a filename which could not be found.
AI-assisted analysis of google/python-fire@716bbc23d7 (2026-08-28).
Data as JSON: /api/errors/9c8dd8a53128d503.
Report an issue: GitHub.