Skip to content

Commit b960f68

Browse files
committed
feat: Add validation for transport types in ClientFactory
1 parent 2ddf585 commit b960f68

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/a2a/client/client_factory.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _register_defaults(
9191
if GrpcTransport is None:
9292
raise ImportError(
9393
'To use GrpcClient, its dependencies must be installed. '
94-
'You can install them with \'pip install "a2a-sdk[grpc]"\''
94+
'You can install them with \'pip install "a2a-sdk[grpc]"\'',
9595
)
9696
self.register(
9797
TransportProtocol.grpc,
@@ -124,6 +124,14 @@ def create(
124124
If there is no valid matching of the client configuration with the
125125
server configuration, a `ValueError` is raised.
126126
"""
127+
valid_transports = {member.value for member in TransportProtocol}
128+
for transport in self._config.supported_transports:
129+
if transport not in valid_transports:
130+
raise ValueError(
131+
f"Unsupported transport type in ClientConfig: '{transport}'. "
132+
f'Valid types are: {", ".join(sorted(valid_transports))}',
133+
)
134+
127135
server_preferred = card.preferred_transport or TransportProtocol.jsonrpc
128136
server_set = {server_preferred: card.url}
129137
if card.additional_interfaces:

tests/client/test_client_factory.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,19 @@ def test_client_factory_no_compatible_transport(base_agent_card: AgentCard):
103103
factory = ClientFactory(config)
104104
with pytest.raises(ValueError, match='no compatible transports found'):
105105
factory.create(base_agent_card)
106+
107+
108+
def test_client_factory_invalid_transport_in_config(base_agent_card: AgentCard):
109+
"""Verify that the factory raises an error for an unknown transport type."""
110+
config = ClientConfig(
111+
httpx_client=httpx.AsyncClient(),
112+
supported_transports=[
113+
TransportProtocol.jsonrpc,
114+
'invalid-transport-protocol',
115+
],
116+
)
117+
factory = ClientFactory(config)
118+
with pytest.raises(
119+
ValueError, match='Unsupported transport type in ClientConfig'
120+
):
121+
factory.create(base_agent_card)

0 commit comments

Comments
 (0)