has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)

2,349 views
Skip to first unread message

mken...@gmail.com

unread,
Aug 15, 2018, 8:25:37 PM8/15/18
to grpc.io
Hello All, using python 3 the self.stub.filter_add() call below  receives the following Traceback.  It was my understanding the json.dumps() would correct this, but does not.  Any ideas what might be causing this problem?



    def filter_add(self, filter_dict):
         return self.stub.filter_add(UtrSSXFilterServer.SSXFilter(Filter=json.dumps(filter_dict)))

Traceback (most recent call last):
  File "/root/utr-tc/lib/python3.4/site-packages/google/protobuf/internal/python_message.py", line 526, in init
    setattr(self, field_name, field_value)
  File "/root/utr-tc/lib/python3.4/site-packages/google/protobuf/internal/python_message.py", line 662, in field_setter
    new_value = type_checker.CheckValue(new_value)
  File "/root/utr-tc/lib/python3.4/site-packages/google/protobuf/internal/type_checkers.py", line 109, in CheckValue
    raise TypeError(message)
TypeError: '{"switchName": "null", "ssxEvent": "GET_PFC_SEND_RECV_P"}' has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)

Mehrdad Afshari

unread,
Aug 22, 2018, 1:55:56 PM8/22/18
to grpc.io
Can you share your proto message definition?

mken...@gmail.com

unread,
Aug 27, 2018, 12:52:01 PM8/27/18
to grpc.io
      1 syntax = "proto3";
      2
      3 option java_multiple_files = true;
      4 option java_package = "io.grpc.UtrServer";
      5 option java_outer_classname = "UtrServerProto";
      6 option objc_class_prefix = "UtrServer";
      7
      8 package UtrSSXFilterServer;
      9
     10 service UtrSSXFilterServer {
     11     rpc register_server_details (ServerDetails) returns (UtrFilterServerResp) {}
     12     rpc filter_add(SSXFilter) returns (UtrFilterServerResp) {}
     13     rpc filter_del(SSXFilter) returns (UtrFilterServerResp) {}
     14     rpc get_current_config(Empty) returns (ConfigData) {}
     15 }
     16
     17 message ServerDetails {
     18     string ipAddress = 1;
     19     string port = 2;
     20 }
     21
     22 message UtrFilterServerResp {
     23     bool response = 1;
     24 }
     25
     26 message SSXFilter {
     27     bytes Filter = 1;
     28 }
     29
     30 message ConfigData {
     31     ServerDetails Details = 1;
     32     bytes Config = 2;
     33 }



On Wednesday, August 15, 2018 at 5:25:37 PM UTC-7, mken...@gmail.com wrote:

jie...@google.com

unread,
Aug 29, 2018, 6:24:11 PM8/29/18
to grpc.io
The Filter is a bytes field, json.dumps() returns str so your code works with python 2 but does not work with python 3: In python2 str is byte strings, in python3 str is unicode.

To make it work with python 3, you can encode it to bytes after json.dumps: Filter=json.dumps(filter_dict).encode()
Reply all
Reply to author
Forward
0 new messages