Using OpenAI Agents Python SDK with Amazon Bedrock using LiteLLM integration
This blog was written by an AI Agent and reviewed by a human, based on the content of this GitHub Gist and this LinkedIn Post.
As of v0.0.12, OpenAI Agents Python SDK has introduced support for LiteLLM as model provider. LiteLLM is an open-source library that provides a unified, OpenAI-compatible interface for accessing, managing, and integrating with over 100 large language models (LLMs) from multiple providers, simplifying tasks like completion, embedding, cost tracking, and model fallback across diverse AI platforms.
In this blog post, we will explore how to use the OpenAI Agents Python SDK with Amazon Bedrock using LiteLLM integration. This integration allows you to leverage the power of Amazon Bedrock models within the OpenAI Agents framework.
Setting Up
To get started, ensure you have the following installed:
- Python 3.8 or higher
- The
openai-agents
,litellm
andboto3
Python packages
You can install these packages using pip:
pip install openai-agents litellm boto3 --quiet --upgrade
Converting an OpenAI Tool to a Bedrock Tool
Note that OpenAI tool definition and Bedrock tool definition are different (ref: Use a tool to complete an Amazon Bedrock model response). The following function demonstrates how to convert an OpenAI tool to a Bedrock tool:
def convert_openai_tool_to_bedrock_tool(tool: dict) -> FunctionTool:
"""
Converts an OpenAI tool to a Bedrock tool.
"""
return FunctionTool(
name=tool["name"],
description=tool["description"],
params_json_schema={
"type": "object",
"properties": {
k: v for k, v in tool["params_json_schema"]["properties"].items()
},
"required": tool["params_json_schema"].get("required", []),
},
on_invoke_tool=tool["on_invoke_tool"],
)
Define tools
@function_tool
def get_weather(city: str):
"""
Get the weather for a given city.
Args:
city (str): The name of the city.
Returns:
str: The weather in the city.
"""
print(f"[debug] getting weather for {city}")
return f"The weather in {city} is sunny."
Using an Agent with Amazon Bedrock
The following code snippet shows how to create an agent that uses a Bedrock model from LiteLLM:
agent = Agent(
name="Assistant",
instructions="You only respond in haikus.",
model="litellm/bedrock/us.amazon.nova-pro-v1:0",
tools=[convert_openai_tool_to_bedrock_tool(get_weather.__dict__)],
)
result = await Runner.run(agent, "What's the weather in Tokyo today, April 22 2025?")
print(result.final_output)
In this example, the agent is instructed to respond only in haikus and uses the Amazon Nova Pro model from Amazon Bedrock via LiteLLM. The get_weather
function is converted to a Bedrock tool and added to the agent’s tools.
Conclusion
By following the steps outlined in this blog post, you can integrate Amazon Bedrock models with the OpenAI Agents Python SDK using LiteLLM. This integration opens up new possibilities for leveraging powerful models within the OpenAI Agents framework.
For more information, refer to the GitHub Gist and the LinkedIn Post.