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
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 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 | |
__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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
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
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
__str__()
¶
Get the agent's last message as a string.
This method extracts and concatenates all text content from the final message, ignoring any non-text content like images or structured data. If there's no text content but structured output is present, it serializes the structured output instead.
Returns:
| Type | Description |
|---|---|
str
|
The agent's last message as a string. |
Source code in strands/agent/agent_result.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
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
78 79 80 81 82 83 84 85 86 87 88 | |