Failed to populate field in ros2 topic pub and service call

Failed to populate field in ros2 topic pub and service call: YAML forms that work, measured

$ ros2 topic pub -1 /s std_msgs/msg/String '{data:hi}'
Failed to populate field: 'String' object has no attribute 'data:hi'

The same family of errors comes from ros2 service call: Failed to populate field: 'SetBool_Request' object has no attribute 'data:true', Value '1.0' is expected to be a dictionary but is a float, The passed value needs to be a dictionary in YAML format, and on Humble a raw yaml.parser.ParserError traceback.

Short answer: the last argument is YAML, and YAML needs a space after every colon. {data:hi} is a map with one key called data:hi, so ros2cli looks for a field with that name and fails. Put the whole value in single quotes, write a space after each colon, nest sub-messages as their own maps, and quote any string that contains a colon:

ros2 topic pub -1 /cmd_vel geometry_msgs/msg/Twist '{linear: {x: 0.5}, angular: {z: 1.0}}'
ros2 topic pub -1 /s std_msgs/msg/String "{data: 'time: 12:30'}"
ros2 service call /sb std_srvs/srv/SetBool '{data: true}'

If you don't know the field names, ros2 interface proto --no-quotes <type> prints a template that you can paste or pass straight in. Without --no-quotes the template is wrapped in literal quotes and fails as an argument.

Find your error text — measured results table
At a glance: the main measured table from this article. Full details below.

Find your error text

Unless a row says otherwise, the text and exit code were the same on Humble, Jazzy and Lyrical.

What was typed Error Exit
'{data:hi}' (no space after colon) Failed to populate field: 'String' object has no attribute 'data:hi' 1
'data:hi' (no space, no braces) The passed value needs to be a dictionary in YAML format 1
'{dat: hi}' (wrong field name) Failed to populate field: 'String' object has no attribute 'dat' 1
'{data: time: 12:30}' (unquoted colon in a string) Humble: traceback ending yaml.parser.ParserError: while parsing a flow mapping. Jazzy, Lyrical: The passed value needs to be in YAML string or a dictionary 1
Twist '{x: 1.0}' (nested field written flat) Failed to populate field: 'Twist' object has no attribute 'x' 1
Twist '{linear: 1.0}' (scalar where a message goes) Value '1.0' is expected to be a dictionary but is a float 1
PoseStamped '{position: {x: 1.0}}' (skipped pose) 'PoseStamped' object has no attribute 'position' 1
Pose '{position:{1,1,1},orientation:{1,1,1,1}}' Humble: getattr(): attribute name must be string. Jazzy, Lyrical: attribute name must be string, not 'int' 1
JointState "{position: '[1,2]'}" (quoted list) cannot use a str to initialize an array with typecode 'd' 1
JointState '{position: 1.0}' (scalar for an array) 'float' object is not iterable 1
UInt8MultiArray '{data: [256]}' / '{data: [-1]}' unsigned byte integer is greater than maximum / ... less than minimum 1
ByteMultiArray '{data: [3, 4]}' or [0x03, 0x04] Humble: The 'data' field must be a set or sequence and each value of type 'bytes' (exit 1). Jazzy, Lyrical: Assertion `PyBytes_Check(item)' failed. and the process aborts 1 / 134
Int32 '{data: 3000000000}' Humble: The 'data' field must be an integer in [-2147483648, 2147483647]. Jazzy, Lyrical: no error (see below) 1 / 0
Float64MultiArray, tab-completed form pasted as one line Humble: yaml.scanner.ScannerError: mapping values are not allowed here. Jazzy, Lyrical: The passed value needs to be in YAML string or a dictionary 1
"{data: "a: b"}" (double quotes inside double quotes) ros2: error: unrecognized arguments: b} 2
{data: hi} with no shell quotes ros2: error: unrecognized arguments: hi} 2
SetBool 'true' (bare value) Value 'True' is expected to be a dictionary but is a bool 1

Value ... is expected to be a dictionary means a plain value went where the type has a nested message. That is the cause in the SetRobotMode question, where mode is itself a message with its own mode field.

What I ran

On 2026-10-31 I ran one script in the official ros:humble-ros-base, ros:jazzy-ros-base and ros:lyrical-ros-base images, one after another, in containers with no network. A small rclpy node subscribed to every test topic and printed what it received. A second node served std_srvs/srv/SetBool and printed each request. Each command ran under timeout 20, so a hang shows as exit 124. Recording what actually arrived shows values that published but changed on the way. Topic tests used -1 -w 1 unless noted.

Missing space after the colon

This is the most common one. In YAML, data:hi is a single plain scalar, not a key and a value. Inside braces it becomes a key with no value, so ros2cli tries to set an attribute named data:hi. Without braces the whole argument is the string data:hi, and you get The passed value needs to be a dictionary in YAML format. Service calls fail the same way: '{data:true}' gave 'SetBool_Request' object has no attribute 'data:true'. The fix is a space after every colon. Braces are optional at the top level: 'data: hi' worked too.

Strings that YAML reads as something else

Unquoted values go through YAML type rules before ros2cli sees them. For a string field ros2cli then turns the result back into text, so these did not fail. They published something else instead:

Typed for a string field Received
{data: 42} '42'
{data: yes} 'True'
{data: 12:30} '750'
{data: 'time: 12:30'} 'time: 12:30'

12:30 became 750 on all three distros. YAML 1.1 reads it as a base-60 number, 12 × 60 + 30. A string with a space after a colon (time: 12:30) doesn't parse at all. Quote such strings inside the YAML: "{data: 'time: 12:30'}".

Int where float is expected, and the reverse

Neither direction raised an error. Twist '{linear: {x: 1}}' arrived as x=1.0, and JointState position: [1, 2] as [1.0, 2.0]. The reverse needs care: Int32 {data: 1.5} arrived as data=1 on every distro, with no warning. {data: '5'} arrived as 5.

Range checks changed between distros. On Humble, {data: 3000000000} for an Int32 was rejected with must be an integer in [-2147483648, 2147483647]. On Jazzy and Lyrical, the command printed publishing #1: std_msgs.msg.Int32(data=3000000000) and exited 0, but the subscriber received data=-1294967296. The value wrapped around. uint8[] elements were still range-checked on all three: 256 and -1 were rejected.

Nested messages need nested maps

Each sub-message is its own map, named by its field. There is no dotted path and no flattening. Twist {x: 1.0} and {linear.x: 1.0} both failed with no attribute. A list in place of a Vector3 failed with is expected to be a dictionary but is a list. For PoseStamped you need both levels:

ros2 topic pub -1 /ps geometry_msgs/msg/PoseStamped \
  '{header: {frame_id: map}, pose: {position: {x: 1.0, y: 2.0}, orientation: {w: 1.0}}}'

The Pose question uses {position:{1,1,1},...}. That fails twice: the colons have no spaces, and {1,1,1} is a YAML map whose keys are integers, so ros2cli can't use them as field names. Use named fields with the same nesting as the PoseStamped command above. Fields you leave out keep their defaults; the quaternion came back as w=1.0.

Block YAML works if the newlines reach ros2cli; a bash $'...\n...' string did. The Float64MultiArray tab-completion form from this question fails because the newlines are lost when it's pasted as one line (layout: dim: [] data_offset: 0data: [0.2]). With real newlines, or as '{data: [0.2, 0.4]}', it worked.

Arrays

Use a YAML list without quotes around it: '{name: [j1, j2], position: [1.0, 2.0]}'. A quoted list is a string: "{position: '[1,2]'}" failed with cannot use a str to initialize an array with typecode 'd'. A single number for an array field failed with 'float' object is not iterable.

byte[] and uint8[]

For uint8[] (std_msgs/UInt8MultiArray), plain integers and hex both worked: [3, 4, 255] and [0x03, 0xff]. A YAML binary value worked as well: {data: !!binary AwQ=} arrived as [3, 4].

For byte[] (std_msgs/ByteMultiArray), each element must be a Python bytes object. On Humble, integers and hex failed with the message from the ByteMultiArray question and ros2cli issue #760: The 'data' field must be a set or sequence and each value of type 'bytes'. On Jazzy and Lyrical the same input got past the Python check. The C conversion then failed with Assertion `PyBytes_Check(item)' failed. and the process aborted with exit code 134. On Lyrical it even printed publishing #1: ... data=[3, 4] first, but nothing reached the subscriber. The form that worked on all three is one !!binary item per byte, in base64:

ros2 topic pub -1 /b std_msgs/msg/ByteMultiArray '{data: [!!binary Aw==, !!binary BA==]}'
# received: data=[b'\x03', b'\x04']

A single !!binary AwQ= for the whole byte[] field failed: exit 1 on Humble, abort on Jazzy and Lyrical.

Header stamp: now and auto

There is no --stamp option on any of the three. Instead, two placeholders work inside the YAML on all three distros. The help text for values mentions them: "You can use 'now' placeholder to get the current time and 'auto' placeholder to get a std_msgs.msg.Header with current time and empty frame_id."

  • '{header: {stamp: now, frame_id: map}}' arrived with the current wall time in sec/nanosec and frame_id='map'.
  • '{header: auto}' arrived with the current time and an empty frame_id. It also worked for JointState.
  • With no header at all, the stamp was sec=0, nanosec=0.
  • An explicit stamp needs a map: {stamp: {sec: 5, nanosec: 0}}. {stamp: 5.5} failed.

If you stamp messages in your own rclpy code and then compare them, mixing clock types fails differently: Can't subtract times with different clock types.

Shell quoting

Use single quotes on the outside and double quotes inside the YAML when you need them. Both "{data: 'hello world'}" and '{data: "hello world"}' worked. Two mistakes come from the shell, not from ROS:

  • Double quotes inside double quotes. "{data: "a: b"}" splits into extra arguments and argparse exits 2 with unrecognized arguments: b}. No quotes at all ({data: hi}) has the same effect.
  • $ inside double quotes. "{data: price $5}" published 'price', because the shell expanded $5 to nothing. Single quotes kept 'price $5'.

Jazzy added --stdin to both commands, and Lyrical added --yaml-file to ros2 topic pub, which avoid shell quoting entirely. In my run, ros2 service call ... --stdin < file.yaml worked on Jazzy and Lyrical. ros2 topic pub --stdin failed on both with TypeError: a bytes-like object is required, not 'str'. ros2 topic pub ... --yaml-file m.yaml worked on Lyrical and published 'time: 12:30' from a file line data: 'time: 12:30'.

ros2 interface proto as a template

ros2 interface proto printed the same templates on all three distros, for messages and for the request part of a service (std_srvs/srv/SetBool gave data: false). By default the output is wrapped in literal double quotes, so passing it straight in fails:

$ ros2 topic pub -1 /tw geometry_msgs/msg/Twist "$(ros2 interface proto geometry_msgs/msg/Twist)"
The passed value needs to be a dictionary in YAML format

With --no-quotes it worked on all three, for Twist, PoseStamped and the SetBool request.

-1, -t, -r and whether the first message is lost

The help text on all three says -w "Defaults to 1 when using "-1"/"--once"/"--times", otherwise defaults to 0." I measured that with the subscriber already running:

Command Humble Jazzy Lyrical
-1 (default wait), 20 separate runs 20/20 20/20 20/20
-1 -w 1, 20 runs 20/20 20/20 20/20
-1 -w 0, 20 runs 17/20 10/20 11/20
-r 10 -t 20 (default wait), 3 rounds 20, 20, 20 20, 20, 20 20, 20, 20
-r 10 -t 20 -w 0, 3 rounds 0, 2, 5 20, 20, 1 20, 20, 16
-r 10 for 3 s (no -t, so no wait), 3 rounds 22, 22, 22 of 22 22, 22, 13 of 22 17 of 19, 22 of 22, 0 of 22

So -1 already waits on these distros and didn't lose a message in my runs. Messages were lost when the wait was turned off with -w 0, or with -r alone, where the default is 0. Those numbers varied a lot from round to round, so treat -w 0 as unreliable rather than as a fixed loss rate. If you stream with -r and need the early messages, add -w 1. Once, on Lyrical, a -1 -w 1 message printed publishing #1 and wasn't seen by the subscriber, so -w lowers the risk but isn't a delivery receipt.

The default wait has a side effect. With -1 or -t and no subscriber, the command prints Waiting for at least 1 matching subscription(s)... and never exits. It also hides field errors: ros2 topic pub -1 /nosub std_msgs/msg/String '{data:hi}' waited the full 20 seconds without reporting Failed to populate field. The same command with -w 0, or with -r 10, failed in about a second. YAML syntax errors in my runs appeared without any Waiting line. If the wait never ends while a subscriber is running, the two processes probably can't discover each other. See nodes that can't see each other. On Jazzy and Lyrical you can also bound the wait with --max-wait-time-secs, which I didn't test.

For ros2 service call with SetBool, the server received data=True for true, True, yes, 1 and 'true'. With no values argument it got the default, data=False.

Rules

Rule Fails Works
Space after every colon '{data:hi}' '{data: hi}'
Single quotes outside the YAML "{data: "a: b"}" '{data: "a: b"}'
Quote strings with colons, times, yes/no or digits {data: 12:30} gives '750' "{data: '12:30'}"
One map per sub-message '{x: 1.0}', '{linear.x: 1.0}' '{linear: {x: 1.0}}'
Arrays as unquoted lists "{position: '[1,2]'}" '{position: [1.0, 2.0]}'
Float fields accept ints, int fields truncate floats {data: 1.5} gives 1 {data: 1}
byte[] needs bytes items {data: [3, 4]} {data: [!!binary Aw==, !!binary BA==]}
uint8[] takes ints {data: [256]} {data: [3, 4, 255]}
Stamps via placeholders {stamp: 5.5} {header: auto}, {header: {stamp: now}}
Templates without quotes "$(ros2 interface proto T)" "$(ros2 interface proto --no-quotes T)"

Version notes

Most messages were identical across Humble (ros2cli 0.18.20), Jazzy (0.32.13) and Lyrical (0.40.9). Four things changed after Humble: YAML syntax errors became a one-line message instead of a traceback, an out-of-range Int32 wraps instead of failing, integers for byte[] abort the process instead of failing cleanly, and the Pose {1,1,1} text changed. --stdin exists from Jazzy. --yaml-file for topic pub and --timeout for service call appear only on Lyrical.

Not tested

Custom message types, other byte forms such as std_msgs/Byte and sensor_msgs/Image, --max-wait-time-secs, ros2 service call --timeout, ros2 action send_goal, QoS options, Cyclone DDS or Zenoh, communication across machines, zsh or PowerShell quoting, Iron, Kilted and Rolling. I didn't investigate why the -w 0 counts varied so much between rounds.

FAQ

What does "Failed to populate field: 'String' object has no attribute 'data:hi'" mean?

The YAML had no space after the colon, so data:hi became one field name. Write '{data: hi}'. The same error with a different name, for example 'dat', means the field name is wrong. Check it with ros2 interface show.

How do I fix "Value is expected to be a dictionary but is a float"?

The field is a nested message and you gave it a plain value. Write it as its own map, for example '{linear: {x: 1.0}}' for Twist instead of '{linear: 1.0}'.

How do I publish a ByteMultiArray from the command line?

Give each byte as a base64 binary item: '{data: [!!binary Aw==, !!binary BA==]}' published bytes 3 and 4 on Humble, Jazzy and Lyrical. Plain integers failed on Humble and aborted the process on Jazzy and Lyrical. UInt8MultiArray accepts plain integers.

Is there a way to fill header.stamp with the current time in ros2 topic pub?

Yes. Use '{header: auto}' for the current time with an empty frame_id, or '{header: {stamp: now, frame_id: map}}'. Both worked on all three distros. There is no --stamp flag.

Does ros2 topic pub --once lose the first message?

Not in my runs. -1 waits for one subscriber by default, and 20 of 20 messages arrived on each distro. With -w 0, 10 to 17 of 20 arrived. With no subscriber, -1 waits forever and doesn't report field errors.

Measured on 2026-10-31 with ros:humble-ros-base (sha256:d9bcf2e6f72d..., ros2cli 0.18.20, rclpy 3.3.22, rclcpp 16.0.21, rmw 6.1.4, rmw_fastrtps_cpp 6.2.10, PyYAML 5.4.1), ros:jazzy-ros-base (sha256:8eed0a4669bb..., ros2cli 0.32.13, rclpy 7.1.12, rclcpp 28.1.22, rmw 7.3.4, rmw_fastrtps_cpp 8.4.4, PyYAML 6.0.1) and ros:lyrical-ros-base (sha256:3224ee150cb0..., ros2cli 0.40.9, rclpy 10.0.11, rclcpp 32.0.3, rmw 7.10.2, rmw_fastrtps_cpp 9.4.10, PyYAML 6.0.3), default RMW (Fast DDS), bash. Full digests are in digests.txt.

Script and raw output: evidence folder. Questions this answers: Publish geometry_msgs Pose from command-line, How is a std_msgs/msg/ByteMultiArray message published via CLI?, Publish Float64MultiArray from command line - ROS2, Ros2 service parameters expects dictionary, ros2cli #760: Cannot/don't know how to publish message with byte[] field.