1

I am trying to generate python code from a proto file, with a proto like this.

syntax="proto3";

service EmailInferencing {
  rpc Infer (EmailInferRequest) returns (EmailInferResponse) {}
}

// ######## Email Infer ########

message Embeddings {
    repeated int64 feature = 1;
}

message EmailInferRequest {
  repeated string model_names = 1;
  string customer_id = 2;
  repeated Embeddings embeddings = 3;
}

message EmailInferResponse {
  repeated string labels = 1;
}

the pb2.py file that i recieve looks like this

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: infer.proto
"""Generated protocol buffer code."""
from google.protobuf.internal import builder as _builder
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0binfer.proto\x12\x05\x65mail\"\x1d\n\nEmbeddings\x12\x0f\n\x07\x66\x65\x61ture\x18\x01 \x03(\x03\"d\n\x11\x45mailInferRequest\x12\x13\n\x0bmodel_names\x18\x01 \x03(\t\x12\x13\n\x0b\x63ustomer_id\x18\x02 \x01(\t\x12%\n\nembeddings\x18\x03 \x03(\x0b\x32\x11.email.Embeddings\"$\n\x12\x45mailInferResponse\x12\x0e\n\x06labels\x18\x01 \x03(\t2R\n\x10\x45mailInferencing\x12>\n\x05Infer\x12\x18.email.EmailInferRequest\x1a\x19.email.EmailInferResponse\"\x00\x62\x06proto3')

_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals())
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'infer_pb2', globals())
if _descriptor._USE_C_DESCRIPTORS == False:

  DESCRIPTOR._options = None
  _EMBEDDINGS._serialized_start=22
  _EMBEDDINGS._serialized_end=51
  _EMAILINFERREQUEST._serialized_start=53
  _EMAILINFERREQUEST._serialized_end=153
  _EMAILINFERRESPONSE._serialized_start=155
  _EMAILINFERRESPONSE._serialized_end=191
  _EMAILINFERENCING._serialized_start=193
  _EMAILINFERENCING._serialized_end=275
# @@protoc_insertion_point(module_scope)

this has EmailInferRequest and EmailInferResponse mising which I can import in my server.py file.

for the proto generation I am using the following code:

python3 -m grpc_tools.protoc -I.  infer.proto  --python_out=. --grpc_python_out=.

This problem is similar to this but I have added the flags to the command, but still there is something wrong.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
Aman Rai
  • 19
  • 6
  • See this [answer](https://stackoverflow.com/a/73480809/609290). Your `proto`, `python3 -m grpc_tools.protoc` and the generated `_pb2.py` file are all correct. If you write code that leverages the service and the messages, it **will** work correctly. It's the nature of the gRPC|protobuf implementations in Python that is confusing. – DazWilkin Oct 03 '22 at 16:34

1 Answers1

1

Okay, yes..the generated files are correct, it's just that the structure of the generated files have been recently changed. I tried to play around with it and the imports from the generated files work the same way they use to work before. That means, you can keep using them like before without minding the look of the generated files. Thanks!

Aman Rai
  • 19
  • 6