{"record":{"id":"9c8dd8a53128d503","repo":"google/python-fire","slug":"the-argument-serialize-must-be-empty-or-callable","errorCode":null,"errorMessage":"The argument `serialize` must be empty or callable:","messagePattern":"The argument `serialize` must be empty or callable:","errorType":"validation","errorClass":"FireError","httpStatus":null,"severity":"error","filePath":"fire/core.py","lineNumber":249,"sourceCode":"  if show_help:\n    component_trace.show_help = True\n    command = f'{component_trace.GetCommand()} -- --help'\n    print(f'INFO: Showing help with the command {shlex.quote(command)}.\\n',\n          file=sys.stderr)\n  return show_help\n\n\ndef _PrintResult(component_trace, verbose=False, serialize=None):\n  \"\"\"Prints the result of the Fire call to stdout in a human readable way.\"\"\"\n  # TODO(dbieber): Design human readable deserializable serialization method\n  # and move serialization to its own module.\n  result = component_trace.GetResult()\n\n  # Allow users to modify the return value of the component and provide\n  # custom formatting.\n  if serialize:\n    if not callable(serialize):\n      raise FireError(\n          'The argument `serialize` must be empty or callable:', serialize)\n    result = serialize(result)\n\n  if value_types.HasCustomStr(result):\n    # If the object has a custom __str__ method, rather than one inherited from\n    # object, then we use that to serialize the object.\n    print(str(result))\n    return\n\n  if isinstance(result, (list, set, frozenset, types.GeneratorType)):\n    for i in result:\n      print(_OneLineResult(i))\n  elif inspect.isgeneratorfunction(result):\n    raise NotImplementedError\n  elif isinstance(result, dict) and value_types.IsSimpleGroup(result):\n    print(_DictAsString(result, verbose))\n  elif isinstance(result, tuple):\n    print(_OneLineResult(result))","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/google/python-fire/blob/716bbc23d7eca949fdb682172283c8d18f742cb6/fire/core.py#L231-L267","documentation":"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.","triggerScenarios":"fire.Fire(component, serialize='json'), serialize=True, or passing a variable that is expected to be a function but isn't.","commonSituations":"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.","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"],"exampleFix":"// before\nfire.Fire(component, serialize='json')\n// after\nfire.Fire(component, serialize=lambda result: json.dumps(result))","handlingStrategy":"type-guard","validationCode":"if serialize is not None and not callable(serialize):\n    raise TypeError('serialize must be callable')\nfire.Fire(component, serialize=serialize)","typeGuard":"def is_serializer(x) -> bool:\n    return x is None or callable(x)","tryCatchPattern":"try:\n    fire.Fire(component, serialize=serialize)\nexcept fire.core.FireError as e:\n    if 'serialize' in str(e):\n        fire.Fire(component)\n    else:\n        raise","preventionTips":["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"],"tags":["python","argument-validation","serialization"],"backgroundTag":"callback-not-callable","analyzedSha":"716bbc23d7eca949fdb682172283c8d18f742cb6","analyzedAt":"2026-08-28T21:57:47.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}