OpenAI has just released an exciting new version of their OpenAI Agents SDK, promising to revolutionize the way AI systems collaborate and function. This production-ready upgrade stems from their experimental project called SWARM, and it comes with a wealth of powerful features aimed at enhancing the versatility and safety of AI applications.
What’s New in OpenAI’s Agents SDK?
The latest version of the Agents SDK brings a variety of improvements, making it an essential tool for developers working with AI systems. Let’s dive into the key features:
Tools and Function Calling: Now, agents are equipped with the ability to use external tools or call functions, expanding their capabilities and enabling them to perform more complex tasks.
Agent Hand-offs: This feature is a game-changer, particularly in multi-agent applications. Agents can now delegate tasks to other agents, ensuring more efficient task management and collaboration between agents.
Built-in Agent Loop: The SDK includes a built-in agent loop that handles tool calls and sends results back to the LLM (Large Language Model), ensuring the process loops until the task is completed. This makes the interaction between agents and the LLM more seamless and efficient.
Guardrails: One of the most important features, Guardrails ensures that all inputs and outputs going through the agent are sanitized. The input guardrail checks user queries to ensure they’re safe for processing, while the output guardrail ensures that the results from the LLM are appropriate and safe.
Tracing: To help with debugging and optimization, the SDK includes a powerful built-in tracing tool that allows you to visualize and monitor the entire agent flow, providing a clearer picture of how data and instructions are processed.
How to Get Started with the OpenAI Agents SDK
Getting started with this powerful new SDK is quick and simple. Just follow these steps:
- Install the SDK: You can install the OpenAI Agents SDK via pip:
pip install openai-agents
Import Libraries:
Import all the necessary libraries and dependencies required to work with the SDK.-
Run the Agent:
OpenAI provides three methods to execute an agent:-
Runner.run()
– Async method for non-blocking execution -
Runner.run_sync()
– Sync (blocking) method for running the agent in a linear flow -
Runner.run_streamed()
– Async method that streams responses back to the user or application, perfect for real-time interaction.
-
Guardrails for Safe AI Interactions
The introduction of Guardrails in this SDK is a crucial step toward ensuring safety in AI interactions. The input_guardrail ensures that the data being sent to the LLM is safe, preventing harmful or inappropriate queries from being processed. Meanwhile, the output_guardrail checks the LLM’s responses to ensure the outputs are valid and aligned with the intended purpose.
Why This Matters
This new version of the OpenAI Agents SDK opens up exciting possibilities for developers working with AI. With the ability to create more intelligent, collaborative, and safe AI agents, this SDK is a significant step forward in the evolution of AI systems. Whether you're building complex, multi-agent systems or just looking to enhance the capabilities of your AI tools, the OpenAI Agents SDK provides the foundation you need to create the future of AI.
Agent SDK: Building Versatile AI Assistants with OpenAI IntegrationRetryClaude can make mistakes. Please double-check responses.
import os
from agents import Agent, Runner
from dotenv import load_dotenv
import asyncio
from openai.types.responses import ResponseTextDeltaEvent
from agents import function_tool
from pydantic import BaseModel
class SreeniOutputFormatter(BaseModel):
name: str
age: int
city: str
country: str
state: str
title: str
# Load environment variables
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
@function_tool
def get_current_weather(location: str):
"""Get the current weather in a given location"""
weather = {
"New York": "sunny",
"Los Angeles": "cloudy",
"San Francisco": "foggy",
"Texas": "hot"
}
return weather.get(location, "unknown")
# Create agents
agent = Agent(
name="Assistant",
instructions=("You are a helpful assistant. remember to use provided tools whenever possible, do not make up information use tools to help answer the question."),
model="gpt-4o-mini",
tools=[get_current_weather]
)
structured_output_agent = Agent(
name="Structured Output Agent",
instructions=("You are a helpful assistant. remember to use provided tools whenever possible, do not make up information use tools to help answer the question."),
model="gpt-4o-mini",
tools=[get_current_weather],
output_type=SreeniOutputFormatter
)
async def run_all_examples():
# Async version
print("\n--- Running Async Version ---")
print("Agent created and ready to go running async version")
question = "What is the current political situation in India?"
result = await Runner.run(agent, question)
print(result.final_output)
# Stream version
print("\n--- Running Stream Version ---")
print("Agent created and ready to go running stream version")
question = "What is the current political situation in India?"
# Fix: run_streamed returns a RunResultStreaming object that isn't awaitable
# Get the object first, then use it
result = Runner.run_streamed(agent, question)
async for data in result.stream_events():
if data.type == "raw_response_event" and isinstance(data.data, ResponseTextDeltaEvent):
print(data.data.delta, end="", flush=True)
print() # Add a newline after streaming
# Function calling version
print("\n--- Running Function Calling Version ---")
print("Agent created and ready to go running function calling version")
question = "What is the current weather in New York?"
result = await Runner.run(agent, question)
print(result.final_output)
# Structured output version
print("\n--- Running Structured Output Version ---")
print("Agent created and ready to go running structured output version")
question = "I am Sreeni i live in Dallas, Texas, i am 30 years old, i am a software engineer, i am from India"
result = await Runner.run(starting_agent=structured_output_agent, input=question)
print(result.final_output)
if __name__ == "__main__":
asyncio.run(run_all_examples())
In Part II, we will explore how to leverage input and output guardrails.
Thanks
Sreeni Ramadorai
Top comments (0)