strands.agent.a2a_agent
¶
A2A Agent client for Strands Agents.
This module provides the A2AAgent class, which acts as a client wrapper for remote A2A agents, allowing them to be used standalone or as part of multi-agent patterns.
A2AAgent can be used to get the Agent Card and interact with the agent.
A2AResponse = tuple[Task, TaskStatusUpdateEvent | TaskArtifactUpdateEvent | None] | Message | Any
module-attribute
¶
AgentInput = str | list[ContentBlock] | list[InterruptResponseContent] | Messages | None
module-attribute
¶
_DEFAULT_TIMEOUT = 300
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
A2AAgent
¶
Bases: AgentBase
Client wrapper for remote A2A agents.
Source code in strands/agent/a2a_agent.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
__call__(prompt=None, **kwargs)
¶
Synchronously invoke the remote A2A agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input to the agent (string, message list, or content blocks). |
None
|
**kwargs
|
Any
|
Additional arguments (ignored). |
{}
|
Returns:
| Type | Description |
|---|---|
AgentResult
|
AgentResult containing the agent's response. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If prompt is None. |
RuntimeError
|
If no response received from agent. |
Source code in strands/agent/a2a_agent.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
__init__(endpoint, *, name=None, description=None, timeout=_DEFAULT_TIMEOUT, a2a_client_factory=None)
¶
Initialize A2A agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
The base URL of the remote A2A agent. |
required |
name
|
str | None
|
Agent name. If not provided, will be populated from agent card. |
None
|
description
|
str | None
|
Agent description. If not provided, will be populated from agent card. |
None
|
timeout
|
int
|
Timeout for HTTP operations in seconds (defaults to 300). |
_DEFAULT_TIMEOUT
|
a2a_client_factory
|
ClientFactory | None
|
Optional pre-configured A2A ClientFactory. If provided, it will be used to create the A2A client after discovering the agent card. Note: When providing a custom factory, you are responsible for managing the lifecycle of any httpx client it uses. |
None
|
Source code in strands/agent/a2a_agent.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
get_agent_card()
async
¶
Fetch and return the remote agent's card.
This method eagerly fetches the agent card from the remote endpoint, populating name and description if not already set. The card is cached after the first fetch.
Returns:
| Type | Description |
|---|---|
AgentCard
|
The remote agent's AgentCard containing name, description, capabilities, skills, etc. |
Source code in strands/agent/a2a_agent.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
invoke_async(prompt=None, **kwargs)
async
¶
Asynchronously invoke the remote A2A agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input to the agent (string, message list, or content blocks). |
None
|
**kwargs
|
Any
|
Additional arguments (ignored). |
{}
|
Returns:
| Type | Description |
|---|---|
AgentResult
|
AgentResult containing the agent's response. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If prompt is None. |
RuntimeError
|
If no response received from agent. |
Source code in strands/agent/a2a_agent.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
stream_async(prompt=None, **kwargs)
async
¶
Stream remote agent execution asynchronously.
This method provides an asynchronous interface for streaming A2A protocol events. Unlike Agent.stream_async() which yields text deltas and tool events, this method yields raw A2A protocol events wrapped in A2AStreamEvent dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input to the agent (string, message list, or content blocks). |
None
|
**kwargs
|
Any
|
Additional arguments (ignored). |
{}
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[Any]
|
An async iterator that yields events. Each event is a dictionary:
- A2AStreamEvent: {"type": "a2a_stream", "event": |
Raises:
| Type | Description |
|---|---|
ValueError
|
If prompt is None. |
Example
async for event in a2a_agent.stream_async("Hello"):
if event.get("type") == "a2a_stream":
print(f"A2A event: {event['event']}")
elif "result" in event:
print(f"Final result: {event['result'].message}")
Source code in strands/agent/a2a_agent.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
A2AStreamEvent
¶
Bases: TypedEvent
Event emitted for every update received from the remote A2A server.
This event wraps all A2A response types during streaming, including: - Partial task updates (TaskArtifactUpdateEvent) - Status updates (TaskStatusUpdateEvent) - Complete messages (Message) - Final task completions
The event is emitted for EVERY update from the server, regardless of whether it represents a complete or partial response. When streaming completes, an AgentResultEvent containing the final AgentResult is also emitted after all A2AStreamEvents.
Source code in strands/types/a2a.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
__init__(a2a_event)
¶
Initialize with A2A event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a2a_event
|
A2AResponse
|
The original A2A event (Task tuple or Message) |
required |
Source code in strands/types/a2a.py
27 28 29 30 31 32 33 34 35 36 37 38 | |
AgentBase
¶
Bases: Protocol
Protocol defining the interface for all agent types in Strands.
This protocol defines the minimal contract that all agent implementations must satisfy.
Source code in strands/agent/base.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
__call__(prompt=None, **kwargs)
¶
Synchronously invoke the agent with the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input to the agent. |
None
|
**kwargs
|
Any
|
Additional arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
AgentResult
|
AgentResult containing the agent's response. |
Source code in strands/agent/base.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
invoke_async(prompt=None, **kwargs)
async
¶
Asynchronously invoke the agent with the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input to the agent. |
None
|
**kwargs
|
Any
|
Additional arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
AgentResult
|
AgentResult containing the agent's response. |
Source code in strands/agent/base.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
stream_async(prompt=None, **kwargs)
¶
Stream agent execution asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input to the agent. |
None
|
**kwargs
|
Any
|
Additional arguments. |
{}
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[Any]
|
Events representing the streaming execution. |
Source code in strands/agent/base.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
AgentResult
dataclass
¶
Represents the last result of invoking an agent with a prompt.
Attributes:
| Name | Type | Description |
|---|---|---|
stop_reason |
StopReason
|
The reason why the agent's processing stopped. |
message |
Message
|
The last message generated by the agent. |
metrics |
EventLoopMetrics
|
Performance metrics collected during processing. |
state |
Any
|
Additional state information from the event loop. |
interrupts |
Sequence[Interrupt] | None
|
List of interrupts if raised by user. |
structured_output |
BaseModel | None
|
Parsed structured output when structured_output_model was specified. |
Source code in strands/agent/agent_result.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
__str__()
¶
Return a string representation of the agent result.
Priority order: 1. Interrupts (if present) → stringified list of interrupt dicts 2. Structured output (if present) → JSON string 3. Text content from message → concatenated text blocks
Returns:
| Type | Description |
|---|---|
str
|
String representation based on the priority order above. |
Source code in strands/agent/agent_result.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
from_dict(data)
classmethod
¶
Rehydrate an AgentResult from persisted JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary containing the serialized AgentResult data |
required |
Returns: AgentResult instance Raises: TypeError: If the data format is invalid@
Source code in strands/agent/agent_result.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
to_dict()
¶
Convert this AgentResult to JSON-serializable dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing serialized AgentResult data |
Source code in strands/agent/agent_result.py
89 90 91 92 93 94 95 96 97 98 99 | |
AgentResultEvent
¶
Bases: TypedEvent
Source code in strands/types/_events.py
412 413 414 | |
convert_input_to_message(prompt)
¶
Convert AgentInput to A2A Message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
AgentInput
|
Input in various formats (string, message list, or content blocks). |
required |
Returns:
| Type | Description |
|---|---|
Message
|
A2AMessage ready to send to the remote agent. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If prompt format is unsupported. |
Source code in strands/multiagent/a2a/_converters.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
convert_response_to_agent_result(response)
¶
Convert A2A response to AgentResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
A2AResponse
|
A2A response (either A2AMessage or tuple of task and update event). |
required |
Returns:
| Type | Description |
|---|---|
AgentResult
|
AgentResult with extracted content and metadata. |
Source code in strands/multiagent/a2a/_converters.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
run_async(async_func)
¶
Run an async function in a separate thread to avoid event loop conflicts.
This utility handles the common pattern of running async code from sync contexts by using ThreadPoolExecutor to isolate the async execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
async_func
|
Callable[[], Awaitable[T]]
|
A callable that returns an awaitable |
required |
Returns:
| Type | Description |
|---|---|
T
|
The result of the async function |
Source code in strands/_async.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |