gapic-generator-python icon indicating copy to clipboard operation
gapic-generator-python copied to clipboard

Using `SynthesizeSpeechRequest` in a proto message creates incorrect import statements in .pb2 files

Open onur-yildirim-infinitusai opened this issue 2 years ago • 4 comments

In my project, I am creating a proto message that contains SynthesizeSpeechRequest and this is the message

import "google/cloud/texttospeech/v1/cloud_tts.proto";
message SharedAudioRequest {
   google.cloud.texttospeech.v1.SynthesizeSpeechRequest request = 1;
   TTSSetting tts_setting= 2;
}

However, after using grpc.tools.protoc protocol buffer compiler to generate pb2 files I am getting ModuleNotFoundError: No module named 'google.cloud.texttospeech.v1' error because the generated file has the line below.

from google.cloud.texttospeech.v1 import cloud_tts_pb2 as google_dot_cloud_dot_texttospeech_dot_v1_dot_cloud__tts__pb2

I don't know what I am doing wrong but using protocol buffer compiler with a .proto file that has the cloud_tts.proto imported and has SynthesizeSpeechRequest in a proto message populates .pb2 with incorrect import statements. I would appreciate any help to understand what I am doing wrong

Environment details

  • OS type and version: Mac OS Monterey v 12.6.8
  • Python version: 3.11.4
  • pip version: pip --version
  • google-cloud-texttospeech version: pip show google-cloud-texttospeech

Steps to reproduce

  1. Add import "google/cloud/texttospeech/v1/cloud_tts.proto"; to your proto file
  2. Run pipenv run python -m grpc.tools.protoc \ --proto_path=../proto \ --python_out=$OUT_DIR \ --mypy_out=$OUT_DIR \ --grpc_python_out=$OUT_DIR \ $(find ../proto -iname "*.proto") to use protocol buffer compilerprotoc`
  3. Check the generated .pb2 files

Thanks!

Regrettably, the behaviour is correct for the steps that you followed. The issue is that the steps that you shared and the client library in this repository are not compatible in the same environment. You'll need to generate the output using the plugin for protoc via the option ---python_gapic_out, as the code in this repository is generated using --python_gapic_out instead of --grpc_python_out, and it is not possible to mix these 2 options for the same package (We have a module at google.cloud.texttospeech which may be conflicting with the import google.cloud.texttospeech.v1 https://github.com/googleapis/python-texttospeech/tree/main/google/cloud/texttospeech). See the gapic-generator-python code generator.

parthea avatar Sep 22 '23 11:09 parthea

I'm going to transfer this issue to google-cloud-python as we're prepared to migrate the code from this repository there in the next 1-2 weeks.

parthea avatar Sep 22 '23 11:09 parthea

Thanks for the quick response, it's unfortunate that I have to use another plugin for this only. Hope it will be modified soon!

I'm going to move this issue to gapic-generator-python to see if we can remove the default import in the future. For example, remove the generated client google.cloud.texttospeech which is conflicting with google.cloud.texttospeech.v1 as described in https://github.com/googleapis/gapic-generator-python/issues/1849

parthea avatar Nov 08 '23 02:11 parthea