How to fix rosbag2_py No RMW implementation found supporting serialization format

Problem

You have code such as

import rosbag2_py
# [...]

storage_options = rosbag2_py.StorageOptions(uri=bag_directory, storage_id="sqlite3")
converter_options = rosbag2_py.ConverterOptions(output_serialization_format="cdr")
writer = rosbag2_py.SequentialWriter()
writer.open(storage_options, converter_options)

but when you run it, you see an error log such as

[INFO] [1736530048.500477128] [rosbag2_cpp]: No plugin found providing serialization format ''. Falling back to checking RMW implementations.
[ERROR] [1736530048.500722869] [rosbag2_cpp]: Could not initialize RMWImplementedConverter: No RMW implementation found supporting serialization format 
[INFO] [1736530048.500779197] [rosbag2_cpp]: No plugin found providing serialization format 'cdr'. Falling back to checking RMW implementations.
Traceback (most recent call last):
  File "/dev/shm/test.py", line 52, in <module>
    write_ros2_bag()
  File "/dev/shm/test.py", line 35, in write_ros2_bag
    writer.open(storage_options, converter_options)
RuntimeError: Could not find converter for format 

Solution

Even though you are only writing (and not reading a ROSBag), you need to specify an input_serialization_format serialization format for the converter as well:

converter_options = rosbag2_py.ConverterOptions(
  input_serialization_format="cdr",
  output_serialization_format="cdr"
)
# [...]

Full fixed code

import rosbag2_py
# [...]

storage_options = rosbag2_py.StorageOptions(uri=bag_directory, storage_id="sqlite3")
converter_options = rosbag2_py.ConverterOptions(
  input_serialization_format="cdr",
  output_serialization_format="cdr"
)
writer = rosbag2_py.SequentialWriter()
writer.open(storage_options, converter_options)