Skip to content

Commit 2a9c509

Browse files
committed
docs: add docs to util helper
1 parent d2e869f commit 2a9c509

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/a2a/utils/helpers.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ def validate(
139139
and returns a boolean.
140140
error_message: An optional custom error message for the `UnsupportedOperationError`.
141141
If None, the string representation of the expression will be used.
142+
143+
Examples:
144+
Basic usage with capability check:
145+
>>> class MyAgent:
146+
... def __init__(self):
147+
... self.agent_card = AgentCard(
148+
... capabilities=Capabilities(streaming=True)
149+
... )
150+
...
151+
... @validate(lambda self: self.agent_card.capabilities.streaming)
152+
... async def stream_response(self, message: str):
153+
... return f'Streaming: {message}'
154+
155+
With custom error message:
156+
>>> class SecureAgent:
157+
... def __init__(self):
158+
... self.auth_enabled = False
159+
...
160+
... @validate(
161+
... lambda self: self.auth_enabled,
162+
... 'Authentication must be enabled for this operation',
163+
... )
164+
... def secure_operation(self, data: str):
165+
... return f'Processing secure data: {data}'
166+
167+
Error case example:
168+
>>> agent = SecureAgent()
169+
>>> agent.secure_operation('secret') # Raises ServerError
170+
ServerError: UnsupportedOperationError(message="Authentication must be enabled for this operation")
171+
172+
Note:
173+
This decorator works with both sync and async methods automatically.
142174
"""
143175

144176
def decorator(function: Callable) -> Callable:
@@ -174,7 +206,7 @@ def sync_wrapper(self: Any, *args, **kwargs) -> Any:
174206
def validate_async_generator(
175207
expression: Callable[[Any], bool], error_message: str | None = None
176208
):
177-
"""Decorator that validates if a given expression evaluates to True.
209+
"""Decorator that validates if a given expression evaluates to True for async generators.
178210
179211
Typically used on class methods to check capabilities or configuration
180212
before executing the method's logic. If the expression is False,
@@ -185,6 +217,44 @@ def validate_async_generator(
185217
and returns a boolean.
186218
error_message: An optional custom error message for the `UnsupportedOperationError`.
187219
If None, the string representation of the expression will be used.
220+
221+
Examples:
222+
Streaming capability validation:
223+
>>> class StreamingAgent:
224+
... def __init__(self):
225+
... self.agent_card = AgentCard(
226+
... capabilities=Capabilities(streaming=True)
227+
... )
228+
...
229+
... @validate_async_generator(
230+
... lambda self: self.agent_card.capabilities.streaming,
231+
... 'Streaming is not supported by this agent',
232+
... )
233+
... async def stream_messages(self, count: int):
234+
... for i in range(count):
235+
... yield f'Message {i}'
236+
237+
Feature flag validation:
238+
>>> class FeatureAgent:
239+
... def __init__(self):
240+
... self.features = {'real_time': False}
241+
...
242+
... @validate_async_generator(
243+
... lambda self: self.features.get('real_time', False)
244+
... )
245+
... 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>")
254+
255+
Note:
256+
This decorator is specifically for async generator methods (async def with yield).
257+
The validation happens before the generator starts yielding values.
188258
"""
189259

190260
def decorator(function):

0 commit comments

Comments
 (0)