Swarm vs Agents SDK

⏱️ 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.

Paź 2024 Swarm Mar 2025 Agents SDK Gru 2025 Swarm deprecated

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

Aspekt Swarm Agents SDK
Status ❌ Eksperymentalny / Deprecated ✅ Produkcyjny
Import from swarm import Swarm, Agent from agents import Agent, Runner
Klient Swarm() Runner (statyczny)
Uruchomienie client.run() Runner.run_sync() lub await Runner.run()
Narzędzia functions=[fn] tools=[function_tool(fn)]
Handoffs return other_agent handoffs=[other_agent]
Guardrails ❌ Brak ✅ Wbudowane
Tracing ❌ Brak ✅ Wbudowany

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:
  1. from swarmfrom agents
  2. Swarm()Runner (statyczny)
  3. functions=tools= + @function_tool
  4. return agenthandoffs=[agent]
  5. client.run()Runner.run_sync()

📚 Bibliografia

  1. OpenAI. (2024). Swarm Repository (Archived). github.com/openai/swarm
  2. OpenAI. (2025). Agents SDK Documentation. openai.github.io
  3. OpenAI. (2025). Migration Guide: Swarm to Agents SDK. openai.github.io