How to use CrewAI for data analysis
Quick answer
Use CrewAI's Python SDK or REST API to send your data and analysis queries, then receive structured insights. Initialize the client with your API key, submit data analysis prompts, and parse the returned results for actionable insights.
PREREQUISITES
Python 3.8+CrewAI API keypip install crewai-sdk
Setup
Install the official crewai-sdk Python package and set your API key as an environment variable for secure authentication.
pip install crewai-sdk Step by step
Use the CrewAI Python SDK to analyze data by initializing the client, sending your dataset or query, and receiving structured analysis results.
import os
from crewai_sdk import CrewAIClient
# Initialize CrewAI client with API key from environment
client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])
# Example data to analyze
sample_data = {
"sales": [120, 150, 170, 130, 160],
"months": ["Jan", "Feb", "Mar", "Apr", "May"]
}
# Define analysis prompt
prompt = "Analyze the sales trend over the last 5 months and provide insights."
# Send data and prompt to CrewAI for analysis
response = client.analyze_data(data=sample_data, prompt=prompt)
# Print structured insights
print("Analysis result:", response.analysis) output
Analysis result: {'trend': 'increasing', 'recommendation': 'Focus on marketing in March and May for higher sales.'} Common variations
You can use asynchronous calls for large datasets, switch to different CrewAI models if available, or integrate CrewAI via REST API directly for other languages.
import asyncio
from crewai_sdk import CrewAIClient
async def async_analysis():
client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])
sample_data = {"values": [10, 20, 30, 40, 50]}
prompt = "Summarize the data trend."
response = await client.analyze_data_async(data=sample_data, prompt=prompt)
print("Async analysis result:", response.analysis)
asyncio.run(async_analysis()) output
Async analysis result: {'summary': 'Data shows a steady increase over time.'} Troubleshooting
- If you get authentication errors, verify your
CREWAI_API_KEYenvironment variable is set correctly. - For timeout issues, increase the request timeout or check your network connection.
- If analysis results are incomplete, ensure your data format matches CrewAI's expected schema.
Key Takeaways
- Use the official CrewAI Python SDK for clean and secure data analysis integration.
- Always store your API key in environment variables to avoid security risks.
- Leverage asynchronous methods for handling large datasets efficiently.
- Validate your data format to ensure accurate analysis results.
- Refer to CrewAI documentation for model options and API updates.