Skip to main content
from praisonaiagents import Agent

agent = Agent(name="generator", instructions="Auto-generate content using configured providers.")
agent.start("Generate a product description for a smart speaker.")
Auto mode picks a provider for you: LiteLLM first (100+ providers), OpenAI as a safety net. The user describes a task in auto mode; the generator picks LiteLLM or falls back to OpenAI and writes the resulting agents YAML.

Quick Start

1

Default (OpenAI only)

Install PraisonAI and set your OpenAI key:
pip install "praisonai-frameworks[crewai]"
export OPENAI_API_KEY=sk-...
Run auto mode:
praisonai --auto "research AI trends"
Or programmatically:
from praisonai import AutoGenerator

generator = AutoGenerator(topic="research AI trends")
result = generator.generate()
print(result)
2

Unlock 100+ Providers with LiteLLM

Install LiteLLM to use Anthropic, Google, Azure, Bedrock, Ollama, and more:
pip install litellm
Switch to Anthropic Claude:
export ANTHROPIC_API_KEY=sk-ant-...
export OPENAI_MODEL_NAME=anthropic/claude-sonnet-4
praisonai --auto "research AI trends"
Switch to Google Gemini:
export GEMINI_API_KEY=...
export OPENAI_MODEL_NAME=gemini/gemini-2.0-flash
praisonai --auto "research AI trends"
Switch to a local Ollama model:
export OPENAI_MODEL_NAME=ollama/llama3
praisonai --auto "research AI trends"
3

Programmatic Use (Sync & Async)

Both sync and async paths honour the LiteLLM → OpenAI fallback:
from praisonai import AutoGenerator

generator = AutoGenerator(topic="summarise recent AI papers")
agents_yaml = generator.generate()
print(agents_yaml)
Async version:
import asyncio
from praisonai import AutoGenerator

async def main():
    generator = AutoGenerator(topic="summarise recent AI papers")
    agents_yaml = await generator.agenerate()
    print(agents_yaml)

asyncio.run(main())
4

Calling from an existing event loop (FastAPI, Jupyter)

AutoGenerator.generate() can now be called from inside a running event loop — it offloads to a worker thread automatically instead of raising RuntimeError: This event loop is already running.
from fastapi import FastAPI
from praisonai import AutoGenerator

app = FastAPI()

@app.get("/generate")
async def generate(topic: str):
    generator = AutoGenerator(topic=topic)
    # Safe to call .generate() here — no RuntimeError
    agents_yaml = generator.generate()
    return {"yaml": agents_yaml}
Prefer await generator.agenerate() when you are already in an async context — it avoids the thread-offload overhead. The sync .generate() form is provided as a safe fallback.

How It Works

StepWhat happens
1. LiteLLM attemptCalls litellm.completion(model=..., response_format=PydanticModel)
2. Runtime fallbackIf LiteLLM raises (auth error, timeout, unsupported model), logs a warning and continues
3. OpenAI fallbackCalls client.beta.chat.completions.parse(...) using the OpenAI SDK
4. No providersRaises ImportError with install instructions
The fallback warning message is: LiteLLM structured completion failed (...); falling back to OpenAI SDK. Watch your logs for this — it means LiteLLM is degrading silently.

Which Provider Should I Use?


Install Matrix

InstalledAuto mode behaviour
openai onlyOpenAI SDK used directly via beta.chat.completions.parse
litellm onlyLiteLLM used; if it raises, the call fails (no fallback)
bothLiteLLM first; on runtime error, falls back to OpenAI with a warning
neitherImportError: Structured output requires either litellm or openai...

Configuration

Environment variableWhat it does
OPENAI_API_KEYAPI key used by both LiteLLM and OpenAI paths
OPENAI_MODEL_NAMEModel passed to whichever provider runs. For LiteLLM use prefixed names: anthropic/claude-sonnet-4, gemini/gemini-2.0-flash, ollama/llama3
OPENAI_API_BASEForwarded as base_url to the OpenAI client (overrides default endpoint)
ANTHROPIC_API_KEYRequired when OPENAI_MODEL_NAME=anthropic/...
GEMINI_API_KEYRequired when OPENAI_MODEL_NAME=gemini/...

Auto Module SDK Reference

Full API reference for AutoGenerator, WorkflowAutoGenerator, and BaseAutoGenerator

Common Patterns

Switch from OpenAI to Anthropic:
pip install litellm
export ANTHROPIC_API_KEY=sk-ant-...
export OPENAI_MODEL_NAME=anthropic/claude-sonnet-4
praisonai --auto "write a market analysis report"
Run against a local Ollama model (no cloud keys needed):
pip install litellm
# Ollama must be running locally on port 11434
export OPENAI_MODEL_NAME=ollama/llama3
praisonai --auto "summarise today's news"
Belt-and-braces production setup (LiteLLM + OpenAI fallback):
pip install litellm openai
export ANTHROPIC_API_KEY=sk-ant-...
export OPENAI_API_KEY=sk-...
export OPENAI_MODEL_NAME=anthropic/claude-sonnet-4
# LiteLLM runs first; transient errors fall back to OpenAI automatically
praisonai --auto "generate a weekly status report"
Async programmatic use:
import asyncio
from praisonai import AutoGenerator

async def run():
    async with AutoGenerator(topic="competitor analysis") as gen:
        yaml_config = await gen.agenerate()
        print(yaml_config)

asyncio.run(run())

Best Practices

Install both packages so transient LiteLLM failures (rate limits, network blips, provider outages) don’t break agent generation:
pip install litellm openai
LiteLLM runs first; OpenAI silently catches any runtime errors.
The warning LiteLLM structured completion failed (...); falling back to OpenAI SDK. means LiteLLM silently degraded. Check:
  • Is your provider API key set correctly?
  • Does the model support response_format (structured output)?
  • Is the provider having an outage?
Investigate before relying on the fallback long-term.
If your code runs multiple AutoGenerator instances in parallel, set model names in config_list rather than OPENAI_MODEL_NAME to avoid environment variable races:
from praisonai import AutoGenerator

gen = AutoGenerator(
    topic="topic",
    config_list=[{
        "model": "anthropic/claude-sonnet-4",
        "api_key": "sk-ant-...",
    }]
)
Lazy loading is on by default — litellm is never imported at module load time. Do not add import litellm at the top of your files. Let AutoGenerator pull it in on demand. This keeps startup fast and avoids import-time side effects.

Auto Mode CLI

Command-line reference for praisonai --auto

AutoAgents

Automatically create and run agents from a prompt

Structured Output

Control agent output format with Pydantic models

YAML Workflows

The agents.yaml format generated by auto mode