@@ -141,16 +141,39 @@ def validate(
141141 If None, the string representation of the expression will be used.
142142
143143 Examples:
144- Basic usage with capability check:
144+ Demonstrating with an async method:
145+ >>> import asyncio
146+ >>> from a2a.utils.errors import ServerError
147+ >>>
145148 >>> class MyAgent:
146149 ... def __init__(self, streaming_enabled: bool):
147150 ... self.streaming_enabled = streaming_enabled
148151 ...
149- ... @validate(lambda self: self.streaming_enabled)
152+ ... @validate(
153+ ... lambda self: self.streaming_enabled,
154+ ... 'Streaming is not enabled for this agent',
155+ ... )
150156 ... async def stream_response(self, message: str):
151157 ... return f'Streaming: {message}'
158+ >>>
159+ >>> async def run_async_test():
160+ ... # Successful call
161+ ... agent_ok = MyAgent(streaming_enabled=True)
162+ ... result = await agent_ok.stream_response('hello')
163+ ... print(result)
164+ ...
165+ ... # Call that fails validation
166+ ... agent_fail = MyAgent(streaming_enabled=False)
167+ ... try:
168+ ... await agent_fail.stream_response('world')
169+ ... except ServerError as e:
170+ ... print(e.error.message)
171+ >>>
172+ >>> asyncio.run(run_async_test())
173+ Streaming: hello
174+ Streaming is not enabled for this agent
152175
153- With custom error message :
176+ Demonstrating with a sync method :
154177 >>> class SecureAgent:
155178 ... def __init__(self):
156179 ... self.auth_enabled = False
@@ -161,9 +184,8 @@ def validate(
161184 ... )
162185 ... def secure_operation(self, data: str):
163186 ... return f'Processing secure data: {data}'
164-
165- Error case example:
166- >>> from a2a.utils.errors import ServerError
187+ >>>
188+ >>> # Error case example
167189 >>> agent = SecureAgent()
168190 >>> try:
169191 ... agent.secure_operation('secret')
@@ -221,7 +243,10 @@ def validate_async_generator(
221243 If None, the string representation of the expression will be used.
222244
223245 Examples:
224- Streaming capability validation:
246+ Streaming capability validation with success case:
247+ >>> import asyncio
248+ >>> from a2a.utils.errors import ServerError
249+ >>>
225250 >>> class StreamingAgent:
226251 ... def __init__(self, streaming_enabled: bool):
227252 ... self.streaming_enabled = streaming_enabled
@@ -233,11 +258,18 @@ def validate_async_generator(
233258 ... async def stream_messages(self, count: int):
234259 ... for i in range(count):
235260 ... yield f'Message {i}'
261+ >>>
262+ >>> async def run_streaming_test():
263+ ... # Successful streaming
264+ ... agent = StreamingAgent(streaming_enabled=True)
265+ ... async for msg in agent.stream_messages(2):
266+ ... print(msg)
267+ >>>
268+ >>> asyncio.run(run_streaming_test())
269+ Message 0
270+ Message 1
236271
237272 Error case - validation fails:
238- >>> from a2a.utils.errors import ServerError
239- >>> import asyncio
240- >>>
241273 >>> class FeatureAgent:
242274 ... def __init__(self):
243275 ... self.features = {'real_time': False}
@@ -248,15 +280,16 @@ def validate_async_generator(
248280 ... )
249281 ... async def real_time_updates(self):
250282 ... yield 'This should not be yielded'
251- >>> async def run_test():
283+ >>>
284+ >>> async def run_error_test():
252285 ... agent = FeatureAgent()
253286 ... try:
254287 ... async for _ in agent.real_time_updates():
255288 ... pass
256289 ... except ServerError as e:
257290 ... print(e.error.message)
258291 >>>
259- >>> asyncio.run(run_test ())
292+ >>> asyncio.run(run_error_test ())
260293 Real-time feature must be enabled to stream updates
261294
262295 Note:
0 commit comments