Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Generated Python gRPC code fails on grpc.experimental

Your error

AttributeError: module 'grpc' has no attribute 'experimental'2 more lines it can show

Fix it fast

Most common cause, 1 of 2

A client calls the EXPERIMENTAL simple-stub class without opting in

Create a channel and use the generated Stub class, which needs no experimental import.

Fix
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)
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to grpcio 1.85.3, grpcio-tools 1.85.3, Python 3.10 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1A client calls the EXPERIMENTAL simple-stub class without opting inThe traceback ends inside _pb2_grpc.py on a line reading return grpc.experimental.unary_unary(
  2. 2The server class inherits from the bare service class instead of the ServicerThe 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.

Check
grep -n "This class is part of an EXPERIMENTAL API" *_pb2_grpc.py

Fix

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.

Fix
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.

Verify
python greeter_client.py

Confirmed by github.com

Did this fix it?

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.

Check
grep -nE "class .*\(.*_pb2_grpc\.[A-Za-z0-9_]+\)" *.py | grep -v Servicer

Fix

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.

Fix
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.

Verify
python greeter_client.py

Confirmed by grpc.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.