OpenAI Agents SDK
Integrate OpenAI's official Agents SDK with Agent Kernel.
Installation
pip install agentkernel[openai]
Basic Usage
from agents import Agent as OpenAIAgent
from agentkernel.cli import CLI
from agentkernel.openai import OpenAIModule
agent = OpenAIAgent(
name="assistant",
instructions="You are a helpful assistant.",
)
OpenAIModule([agent])
if __name__ == "__main__":
CLI.main()
Multi-Agent System
from agents import Agent as OpenAIAgent
from agentkernel.openai import OpenAIModule
# Define agents with handoff capabilities
general_agent = OpenAIAgent(
name="general",
handoff_description="Agent for general questions",
instructions="You provide general assistance.",
)
math_agent = OpenAIAgent(
name="math",
handoff_description="Specialist for math problems",
instructions="You solve math problems.",
)
OpenAIModule([general_agent, math_agent])
Configuration
export OPENAI_API_KEY=sk-...
export OPENAI_MODEL=gpt-4 # Optional, override default
Tool Binding
Use OpenAIToolBuilder to bind plain Python functions as tools to your OpenAI agents:
from agents import Agent as OpenAIAgent
from agentkernel.openai import OpenAIModule, OpenAIToolBuilder
def get_weather(city: str) -> str:
"""Returns the weather for a given city."""
return f"Weather in {city}: sunny, 25°C"
agent = OpenAIAgent(
name="weather",
instructions="You provide weather information.",
tools=OpenAIToolBuilder.bind([get_weather]),
)
OpenAIModule([agent])
See Tools for the full guide on writing and binding tools.
Structured Output
Configure structured output with the OpenAI Agents SDK's output_type parameter. Agent Kernel detects the structured result and returns an AgentReplyAny whose content is the result as a dict; no re-parsing of text needed:
from agents import Agent as OpenAIAgent
from pydantic import BaseModel
from agentkernel.openai import OpenAIModule
class CalendarEvent(BaseModel):
name: str
date: str
agent = OpenAIAgent(
name="extractor",
instructions="Extract the calendar event from the text.",
output_type=CalendarEvent,
)
OpenAIModule([agent])
Pydantic results are converted via model_dump(), and str(reply) returns the JSON-serialized content, so text-based consumers (chat integrations, logging) work unchanged. See Reply Types for how structured replies are surfaced, and Execution Hooks for how hooks receive them.
Structured output applies to non-streaming execution only. Streamed runs emit token-by-token text deltas.
Features
- ✅ Function calling
- ✅ Multi-agent handoff
- ✅ Streaming responses
- ✅ Structured output (
output_type→AgentReplyAny) - ✅ Session management
- ✅ Framework-agnostic tool binding
Example
See examples/cli/openai for complete examples.
For structured output, see examples/cli/openai_structured and examples/api/openai_structured (REST API + post-execution hook).
