How to use sequential process in CrewAI
Quick answer
Use CrewAI's
Sequential class to chain multiple AI tasks in a defined order, passing outputs from one step as inputs to the next. Initialize each step with a model or function, then execute the sequence to get the final result.PREREQUISITES
Python 3.8+CrewAI SDK installed (pip install crewai)OpenAI API key or other AI provider API key set in environment variables
Setup
Install the CrewAI SDK and set your API key as an environment variable before running the code.
pip install crewai Step by step
This example demonstrates creating a sequential process in CrewAI that first summarizes text, then translates the summary to French.
import os
from crewai import CrewAI, Sequential
# Initialize CrewAI client with your API key
client = CrewAI(api_key=os.environ["OPENAI_API_KEY"])
# Define the first step: summarize text
summary_step = client.create_task(
model="gpt-4o",
prompt_template="Summarize the following text:\n{input_text}",
input_keys=["input_text"],
output_key="summary"
)
# Define the second step: translate summary to French
translate_step = client.create_task(
model="gpt-4o",
prompt_template="Translate this text to French:\n{summary}",
input_keys=["summary"],
output_key="french_translation"
)
# Create a sequential process chaining the two steps
sequence = Sequential(steps=[summary_step, translate_step])
# Run the sequence with initial input
result = sequence.run({"input_text": "CrewAI enables easy AI workflow orchestration."})
print("Summary:", result["summary"])
print("French translation:", result["french_translation"]) output
Summary: CrewAI enables simple orchestration of AI workflows. French translation: CrewAI permet une orchestration simple des flux de travail IA.
Common variations
You can use asynchronous execution with CrewAI's Sequential by calling await sequence.run_async() in an async context. Also, you can replace gpt-4o with other supported models or add more steps to the sequence for complex workflows.
import asyncio
async def async_run():
result = await sequence.run_async({"input_text": "CrewAI supports async workflows."})
print(result)
asyncio.run(async_run()) output
{'summary': 'CrewAI supports asynchronous workflows.', 'french_translation': "CrewAI prend en charge les flux de travail asynchrones."} Troubleshooting
- If you get a
KeyErrorfor missing inputs, ensure each step'sinput_keysmatches the output keys from previous steps. - If API calls fail, verify your API key is correctly set in
os.environand your network connection is stable. - For unexpected output formats, check your prompt templates and model compatibility.
Key Takeaways
- Use CrewAI's Sequential class to chain AI tasks with clear input-output mapping.
- Always define input_keys and output_key for each step to ensure data flows correctly.
- Leverage async methods for non-blocking sequential execution in CrewAI.
- Validate environment variables and prompt templates to avoid runtime errors.