⏱️ Czas czytania: ~10 minut | 📊 Poziom: Średni | 📅 Aktualizacja: Grudzień 2025
Swarm był eksperymentalnym frameworkiem OpenAI do budowy systemów multi-agent. W marcu 2025 OpenAI oficjalnie zastąpiło go produkcyjnym Agents SDK. Ten artykuł wyjaśnia różnice i pokazuje, jak zmigrować istniejący kod.
Oś czasu: ewolucja od Swarm do Agents SDK
⚠️ Ważne:
Swarm nie jest już rozwijany. Jeśli masz projekty oparte na Swarm, zalecana jest migracja do Agents SDK.
Porównanie: Swarm vs Agents SDK
Migracja krok po kroku
Krok 1: Zmiana importów
# ❌ Swarm (stare)
from swarm import Swarm, Agent
# ✅ Agents SDK (nowe)
from agents import Agent, Runner, function_tool
Krok 2: Definicja agenta
# ❌ Swarm
agent = Agent(
name="Assistant",
instructions="You are helpful.",
functions=[my_function] # zwykła funkcja
)
# ✅ Agents SDK
agent = Agent(
name="Assistant",
instructions="You are helpful.",
tools=[my_function] # z dekoratorem @function_tool
)
Krok 3: Narzędzia
# ❌ Swarm - zwykła funkcja
def get_weather(city: str):
"""Get weather for city"""
return f"Sunny in {city}"
# ✅ Agents SDK - z dekoratorem
from agents import function_tool
@function_tool
def get_weather(city: str) -> str:
"""Get weather for city
Args:
city: Name of the city
"""
return f"Sunny in {city}"
Krok 4: Uruchomienie
# ❌ Swarm
client = Swarm()
response = client.run(agent=agent, messages=[{"role": "user", "content": "Hello"}])
print(response.messages[-1]["content"])
# ✅ Agents SDK (sync)
result = Runner.run_sync(agent, "Hello")
print(result.final_output)
# ✅ Agents SDK (async)
result = await Runner.run(agent, "Hello")
print(result.final_output)
Krok 5: Handoffs
# ❌ Swarm - handoff przez return w funkcji
def transfer_to_sales():
"""Transfer to sales agent"""
return sales_agent # zwraca agenta
agent = Agent(
functions=[transfer_to_sales]
)
# ✅ Agents SDK - deklaratywne handoffs
triage = Agent(
name="Triage",
instructions="Route to appropriate agent",
handoffs=[sales_agent, support_agent] # lista agentów
)
Pełny przykład migracji
❌ Swarm (przed):
from swarm import Swarm, Agent
def calculate(expr: str):
return str(eval(expr))
def transfer_to_math():
return math_agent
math_agent = Agent(
name="Math",
instructions="You do calculations",
functions=[calculate]
)
main_agent = Agent(
name="Main",
instructions="Route math questions",
functions=[transfer_to_math]
)
client = Swarm()
response = client.run(main_agent, [{"role": "user", "content": "2+2?"}])
✅ Agents SDK (po):
from agents import Agent, Runner, function_tool
@function_tool
def calculate(expr: str) -> str:
"""Calculate math expression
Args:
expr: Math expression like "2+2"
"""
return str(eval(expr))
math_agent = Agent(
name="Math",
instructions="You do calculations",
tools=[calculate]
)
main_agent = Agent(
name="Main",
instructions="Route math questions to Math agent",
handoffs=[math_agent]
)
result = Runner.run_sync(main_agent, "2+2?")
print(result.final_output)
📝 Podsumowanie zmian:
from swarm→from agentsSwarm()→Runner(statyczny)functions=→tools=+@function_toolreturn agent→handoffs=[agent]client.run()→Runner.run_sync()
📚 Bibliografia
- OpenAI. (2024). Swarm Repository (Archived). github.com/openai/swarm
- OpenAI. (2025). Agents SDK Documentation. openai.github.io
- OpenAI. (2025). Migration Guide: Swarm to Agents SDK. openai.github.io