Generated Python gRPC code fails on grpc.experimental
Fix it fast
Most common cause, 1 of 2A client calls the EXPERIMENTAL simple-stub class without opting in
Create a channel and use the generated Stub class, which needs no experimental import.
import grpc
import helloworld_pb2, helloworld_pb2_grpc
with grpc.insecure_channel("localhost:50051") as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
reply = stub.SayHello(helloworld_pb2.HelloRequest(name="you"))
print(reply.message)
# or keep the simple-stub call and opt in explicitly
import grpc.experimental
helloworld_pb2_grpc.Greeter.SayHello(
helloworld_pb2.HelloRequest(name="you"), "localhost:50051", insecure=True)Short answer
AttributeError: module 'grpc' has no attribute 'experimental' comes from the class grpcio-tools generates with the bare service name, such as Greeter. Its static methods call grpc.experimental, which loads only after an explicit import. A client should use GreeterStub(channel) or import grpc.experimental; a server must subclass GreeterServicer. Applies to grpcio 1.85.
Which cause do you have?
List every place your code touches a generated class that is not a Stub, a Servicer or the add_..._to_server helper. A hit in client code means cause 1; a hit in a server class definition means cause 2.
grep -rnE "pb2_grpc\.[A-Za-z0-9_]+" --include=*.py . | grep -v "_pb2_grpc.py:" | grep -vE "Stub|Servicer"Then find the line you see in this list and open its fix:
- 1A client calls the EXPERIMENTAL simple-stub class without opting in
The traceback ends inside _pb2_grpc.py on a line reading return grpc.experimental.unary_unary( - 2The server class inherits from the bare service class instead of the Servicer
The server starts, but every call to one RPC returns StatusCode.UNKNOWN with the experimental message; if grpc.experimental is already loaded, the text is '_Context' object has no attribute 'encode' instead
What does the error look like?
AttributeError: module 'grpc' has no attribute 'experimental'StatusCode.UNKNOWN Exception calling application: module 'grpc' has no attribute 'experimental'Exception calling application: '_Context' object has no attribute 'encode'
Cause 1 of 2
A client calls the EXPERIMENTAL simple-stub class without opting in
You seeThe traceback ends inside _pb2_grpc.py on a line reading return grpc.experimental.unary_unary(
Check
On a client, module 'grpc' has no attribute 'experimental' means the code called a static method on the class named exactly like the service, for example helloworld_pb2_grpc.Greeter.SayHello(request, target). The generator labels that class an EXPERIMENTAL API, and a gRPC team member confirmed the missing submodule import is intended: you opt in by importing it yourself.
grep -n "This class is part of an EXPERIMENTAL API" *_pb2_grpc.pyFix
Create a channel and use the generated Stub class, which needs no experimental import. If you want the one-line simple-stub call instead, import grpc.experimental before it, the way gRPC's no_codegen example does.
import grpc
import helloworld_pb2, helloworld_pb2_grpc
with grpc.insecure_channel("localhost:50051") as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
reply = stub.SayHello(helloworld_pb2.HelloRequest(name="you"))
print(reply.message)
# or keep the simple-stub call and opt in explicitly
import grpc.experimental
helloworld_pb2_grpc.Greeter.SayHello(
helloworld_pb2.HelloRequest(name="you"), "localhost:50051", insecure=True)Verify
The client prints the reply from the server and the AttributeError is gone.
python greeter_client.pyConfirmed by github.com
Cause 2 of 2
The server class inherits from the bare service class instead of the Servicer
You seeThe server starts, but every call to one RPC returns StatusCode.UNKNOWN with the experimental message; if grpc.experimental is already loaded, the text is '_Context' object has no attribute 'encode' instead
Check
Seen on the client as StatusCode.UNKNOWN, the experimental error means the server implementation subclasses Greeter rather than GreeterServicer. gRPC's generator calls the bare-named class a namespace for static methods that should never be instantiated. If a method in your subclass is missing or spelled differently from the .proto, gRPC calls the inherited static method with the server context where a target string belongs.
grep -nE "class .*\(.*_pb2_grpc\.[A-Za-z0-9_]+\)" *.py | grep -v ServicerFix
Subclass the generated Servicer, as the gRPC Python basics tutorial does, and copy each RPC name from the .proto exactly, capital letters included. With the Servicer as the base, a method you forgot answers UNIMPLEMENTED with Method not implemented!, a much clearer failure.
from concurrent import futures
import grpc
import helloworld_pb2, helloworld_pb2_grpc
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message=f"Hello, {request.name}!")
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port("[::]:50051")
server.start()
server.wait_for_termination()Verify
The same client request now returns the HelloReply text instead of StatusCode.UNKNOWN.
python greeter_client.pyConfirmed by grpc.io
How do I stop it coming back?
- Treat the bare-named class in _pb2_grpc.py as off limits; flag any pb2_grpc.<Service>. reference without Stub or Servicer in code review.
- Add one test that starts the server in-process and calls every RPC once, so a misspelled method name fails in CI rather than in production.
- Keep grpcio and grpcio-tools on the same version and regenerate the _pb2_grpc.py files on each bump; the generated module raises a RuntimeError when grpcio is older than the code it was built with.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.