strands.agent.base
¶
Agent Interface.
Defines the minimal interface that all agent types must implement.
AgentInput = str | list[ContentBlock] | list[InterruptResponseContent] | Messages | None
module-attribute
¶
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 | |