AWS Builder Center
Simplify Your Bedrock Converse API Integration with the bedrock-tools Python Library

Simplify Your Bedrock Converse API Integration with the bedrock-tools Python Library

A Python library that simplifies Amazon Bedrock Converse API tool use

The  Amazon Bedrock Converse API  enables developers to build conversational AI applications and leverage custom tools and functions, enhancing their AI agents' capabilities. Integrating these tools with the API can be time-consuming due to boilerplate code.
 bedrock-tools  is a Python library that simplifies integrating native Python functions with the Bedrock Converse API, enabling conversational AI application development.

Key Features of bedrock-tools:

  1. Simple Integration: You can author AI tools as standard Python functions without the need for complex frameworks.
  1. Automatic Type Handling: The library generates the necessary JSON schema based on your functions' type annotations, handling various data types.
  1. Transparent Tool Invocation: When the Bedrock Converse API requests a tool, the library handles the invocation and exception handling, returning the result in the expected format.
Using bedrock-tools allows you to focus on building your application's core functionality while the library handles the integration details.
Here's an example of how to use the library. First, install the library.
sh
pip install bedrock-tools
Then, use it in your code.
Python
from bedrock_tools import BedrockTools

# Define Python functions as tools
def add_numbers(a: int, b: int) -> int:
return a + b

def greet(name: str) -> str:
return f"Hello, {name}!"

# Set up BedrockTools and add functions
tools = BedrockTools()
tools.add_function(add_numbers)
tools.add_function(greet)

# Use the tool configuration in your API call
response = bedrock.converse(
modelId=model_id,
toolConfig=tools.get_tool_config(),
messages=messages,
)

# Invoke the tool when requested
if "toolUse" in content_block:
result = tools.invoke(content_block["toolUse"])
tool_results.append(result)

message = {"role": "user", "content": tool_results}
The following is an example from the  Bedrock Converse API documentation  without and with the use of the library:
Code without and with the use of the library

Code without and with the use of the library

The bedrock-tools library simplifies the integration of custom Python functions with the Amazon Bedrock Converse API, enabling you to build more sophisticated conversational AI applications efficiently. Contributions to improve the library are welcome.  
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article