Skip to content

Commit 51b8d29

Browse files
committed
update docs
1 parent 2a9c509 commit 51b8d29

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/a2a/utils/helpers.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,10 @@ def validate(
143143
Examples:
144144
Basic usage with capability check:
145145
>>> class MyAgent:
146-
... def __init__(self):
147-
... self.agent_card = AgentCard(
148-
... capabilities=Capabilities(streaming=True)
149-
... )
146+
... def __init__(self, streaming_enabled: bool):
147+
... self.streaming_enabled = streaming_enabled
150148
...
151-
... @validate(lambda self: self.agent_card.capabilities.streaming)
149+
... @validate(lambda self: self.streaming_enabled)
152150
... async def stream_response(self, message: str):
153151
... return f'Streaming: {message}'
154152
@@ -165,9 +163,13 @@ def validate(
165163
... return f'Processing secure data: {data}'
166164
167165
Error case example:
166+
>>> from a2a.utils.errors import ServerError
168167
>>> agent = SecureAgent()
169-
>>> agent.secure_operation('secret') # Raises ServerError
170-
ServerError: UnsupportedOperationError(message="Authentication must be enabled for this operation")
168+
>>> try:
169+
... agent.secure_operation('secret')
170+
... except ServerError as e:
171+
... print(e.error.message)
172+
Authentication must be enabled for this operation
171173
172174
Note:
173175
This decorator works with both sync and async methods automatically.
@@ -221,36 +223,41 @@ def validate_async_generator(
221223
Examples:
222224
Streaming capability validation:
223225
>>> class StreamingAgent:
224-
... def __init__(self):
225-
... self.agent_card = AgentCard(
226-
... capabilities=Capabilities(streaming=True)
227-
... )
226+
... def __init__(self, streaming_enabled: bool):
227+
... self.streaming_enabled = streaming_enabled
228228
...
229229
... @validate_async_generator(
230-
... lambda self: self.agent_card.capabilities.streaming,
230+
... lambda self: self.streaming_enabled,
231231
... 'Streaming is not supported by this agent',
232232
... )
233233
... async def stream_messages(self, count: int):
234234
... for i in range(count):
235235
... yield f'Message {i}'
236236
237-
Feature flag validation:
237+
Error case - validation fails:
238+
>>> from a2a.utils.errors import ServerError
239+
>>> import asyncio
240+
>>>
238241
>>> class FeatureAgent:
239242
... def __init__(self):
240243
... self.features = {'real_time': False}
241244
...
242245
... @validate_async_generator(
243-
... lambda self: self.features.get('real_time', False)
246+
... lambda self: self.features.get('real_time', False),
247+
... 'Real-time feature must be enabled to stream updates',
244248
... )
245249
... async def real_time_updates(self):
246-
... async for update in self._get_updates():
247-
... yield update
248-
249-
Error case - validation fails:
250-
>>> agent = FeatureAgent()
251-
>>> async for msg in agent.real_time_updates():
252-
... print(msg) # Raises ServerError before iteration starts
253-
ServerError: UnsupportedOperationError(message="<lambda>")
250+
... yield 'This should not be yielded'
251+
>>> async def run_test():
252+
... agent = FeatureAgent()
253+
... try:
254+
... async for _ in agent.real_time_updates():
255+
... pass
256+
... except ServerError as e:
257+
... print(e.error.message)
258+
>>>
259+
>>> asyncio.run(run_test())
260+
Real-time feature must be enabled to stream updates
254261
255262
Note:
256263
This decorator is specifically for async generator methods (async def with yield).

0 commit comments

Comments
 (0)