Skip to content

Commit 53ad485

Browse files
authored
fix: Prevent import grpc issues for Client after making dependencies optional (#330)
Follow-up to #322 Fixes #326
1 parent 5061834 commit 53ad485

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/a2a/client/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Client-side components for interacting with an A2A agent."""
22

3+
import logging
4+
35
from a2a.client.auth import (
46
AuthInterceptor,
57
CredentialService,
@@ -12,11 +14,31 @@
1214
A2AClientJSONError,
1315
A2AClientTimeoutError,
1416
)
15-
from a2a.client.grpc_client import A2AGrpcClient
1617
from a2a.client.helpers import create_text_message_object
1718
from a2a.client.middleware import ClientCallContext, ClientCallInterceptor
1819

1920

21+
logger = logging.getLogger(__name__)
22+
23+
try:
24+
from a2a.client.grpc_client import A2AGrpcClient # type: ignore
25+
except ImportError as e:
26+
_original_error = e
27+
logger.debug(
28+
'A2AGrpcClient not loaded. This is expected if gRPC dependencies are not installed. Error: %s',
29+
_original_error,
30+
)
31+
32+
class A2AGrpcClient: # type: ignore
33+
"""Placeholder for A2AGrpcClient when dependencies are not installed."""
34+
35+
def __init__(self, *args, **kwargs):
36+
raise ImportError(
37+
'To use A2AGrpcClient, its dependencies must be installed. '
38+
'You can install them with \'pip install "a2a-sdk[grpc]"\''
39+
) from _original_error
40+
41+
2042
__all__ = [
2143
'A2ACardResolver',
2244
'A2AClient',

src/a2a/client/grpc_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
from collections.abc import AsyncGenerator
44

5-
import grpc
5+
6+
try:
7+
import grpc
8+
except ImportError as e:
9+
raise ImportError(
10+
'A2AGrpcClient requires grpcio and grpcio-tools to be installed. '
11+
'Install with: '
12+
"'pip install a2a-sdk[grpc]'"
13+
) from e
14+
615

716
from a2a.grpc import a2a_pb2, a2a_pb2_grpc
817
from a2a.types import (

src/a2a/server/request_handlers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
_original_error = e
2424
logger.debug(
2525
'GrpcHandler not loaded. This is expected if gRPC dependencies are not installed. Error: %s',
26+
_original_error,
2627
)
2728

2829
class GrpcHandler: # type: ignore

0 commit comments

Comments
 (0)