TypeError: Message must be initialized with a dict

10,848 views
Skip to first unread message

Tony Piazza

unread,
Jun 10, 2023, 11:56:36 PM6/10/23
to Protocol Buffers
My current task requires me to use the BigQuery Storage Write API. I have created a .proto file and was able to use protoc to generate a Python message class. I am seeing this exception when creating an instance of that class:

TypeError: Message must be initialized with a dict: combocurve.Measurement
File "/google/api_core/grpc_helpers.py", line 162, in error_remapped_callable

Here is my .proto file:

syntax = "proto2";

package combocurve;

import "google/protobuf/timestamp.proto";
import "google/type/date.proto";

message Measurement {
required string device_id = 1;
required google.type.Date last_service_date = 2;
optional double temperature = 3;
optional double pressure = 4;
optional google.protobuf.Timestamp created_at = 5;
}

Here is the code that is raising the exception:

measurement = Measurement(
device_id='ABC123',
last_service_date=date_pb2.Date(
year=last_service_date.year,
month=last_service_date.month,
day=last_service_date.day),
temperature=10.0,
pressure=20.0,
created_at=int(created_at.timestamp() * 1e6)
)

Please let me know if you have any ideas as to what is causing this exception.

Thanks in advance for your help!

-Tony

Tony Piazza

unread,
Jun 11, 2023, 12:38:42 AM6/11/23
to Protocol Buffers
The problem had to do with the Date and Timestamp fields. The problem was solved by converting them to int64.

Fred Douglis

unread,
Nov 21, 2023, 11:47:41 AM11/21/23
to Protocol Buffers
I found this thread when searching for the same message, but my solution was entirely different.  It turned out that I was trying to update a client to use a slightly modified proto file, and I was making a call that was assigning to a field that no longer existed.  But the error message here ( TypeError: Message must be initialized with a dict)   is completely misleading -- the real message should have been "unknown argument" or "unknown field" or something .... 
Reply all
Reply to author
Forward
0 new messages