strands.experimental.bidi.tools.stop_conversation
¶
Tool to gracefully stop a bidirectional connection.
stop_conversation()
¶
Stop the bidirectional conversation gracefully.
Use ONLY when user says "stop conversation" exactly. Do NOT use for: "stop", "goodbye", "bye", "exit", "quit", "end" or other farewells or phrases.
Returns:
| Type | Description |
|---|---|
str
|
Success message confirming the conversation will end |
Source code in strands/experimental/bidi/tools/stop_conversation.py
6 7 8 9 10 11 12 13 14 15 16 | |
tool(func=None, description=None, inputSchema=None, name=None, context=False)
¶
tool(__func: Callable[P, R]) -> DecoratedFunctionTool[P, R]
tool(
description: Optional[str] = None,
inputSchema: Optional[JSONSchema] = None,
name: Optional[str] = None,
context: bool | str = False,
) -> Callable[
[Callable[P, R]], DecoratedFunctionTool[P, R]
]
Decorator that transforms a Python function into a Strands tool.
This decorator seamlessly enables a function to be called both as a regular Python function and as a Strands tool. It extracts metadata from the function's signature, docstring, and type hints to generate an OpenAPI-compatible tool specification.
When decorated, a function:
- Still works as a normal function when called directly with arguments
- Processes tool use API calls when provided with a tool use dictionary
- Validates inputs against the function's type hints and parameter spec
- Formats return values according to the expected Strands tool result format
- Provides automatic error handling and reporting
The decorator can be used in two ways:
- As a simple decorator: @tool
- With parameters: @tool(name="custom_name", description="Custom description")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Optional[Callable[P, R]]
|
The function to decorate. When used as a simple decorator, this is the function being decorated. When used with parameters, this will be None. |
None
|
description
|
Optional[str]
|
Optional custom description to override the function's docstring. |
None
|
inputSchema
|
Optional[JSONSchema]
|
Optional custom JSON schema to override the automatically generated schema. |
None
|
name
|
Optional[str]
|
Optional custom name to override the function's name. |
None
|
context
|
bool | str
|
When provided, places an object in the designated parameter. If True, the param name defaults to 'tool_context', or if an override is needed, set context equal to a string to designate the param name. |
False
|
Returns:
| Type | Description |
|---|---|
Union[DecoratedFunctionTool[P, R], Callable[[Callable[P, R]], DecoratedFunctionTool[P, R]]]
|
An AgentTool that also mimics the original function when invoked |
Example
@tool
def my_tool(name: str, count: int = 1) -> str:
# Does something useful with the provided parameters.
#
# Parameters:
# name: The name to process
# count: Number of times to process (default: 1)
#
# Returns:
# A message with the result
return f"Processed {name} {count} times"
agent = Agent(tools=[my_tool])
agent.my_tool(name="example", count=3)
# Returns: {
# "toolUseId": "123",
# "status": "success",
# "content": [{"text": "Processed example 3 times"}]
# }
Example with parameters
@tool(name="custom_tool", description="A tool with a custom name and description", context=True)
def my_tool(name: str, count: int = 1, tool_context: ToolContext) -> str:
tool_id = tool_context["tool_use"]["toolUseId"]
return f"Processed {name} {count} times with tool ID {tool_id}"
Source code in strands/tools/decorator.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 | |