Tutorial
Python + AI: Combine OpenClaw with Python for intelligent data analysis, automated ML workflows, and accelerated scientific computing.
Installing the Python SDK
# Install via pip
pip install openclaw
# Or with uv for faster installs
uv pip install openclaw
# Verify installation
python -c "import openclaw; print(openclaw.__version__)"Basic Usage
import openclaw
# Initialize client
client = openclaw.Client()
# Simple query
response = client.query("Explain the difference between list and tuple in Python")
print(response.text)Data Analysis Assistance
import pandas as pd
import openclaw
# Load your data
df = pd.read_csv("data.csv")
# Get AI insights
claw = openclaw.Client()
# Describe the dataset
insights = claw.query(
f"Analyze this dataset: {df.describe().to_dict()}. "
"Identify potential patterns, outliers, and areas for further investigation."
)
# Suggest visualizations
viz_suggestions = claw.query(
f"Based on these columns: {df.columns.tolist()}, "
"suggest 5 effective visualizations with plot types and reasoning."
)Pandas Code Generation
# Ask for specific data operations
prompt = """
I have a DataFrame 'df' with columns: date, category, amount, status.
Write pandas code to:
1. Filter for status='completed' transactions
2. Group by category and sum the amounts
3. Sort by amount descending
4. Create a bar plot
"""
code = claw.query(prompt)
print(code.text)
# Execute the generated code (after review!)
exec(code.text)Machine Learning Workflows
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import openclaw
claw = openclaw.Client()
# Get feature engineering suggestions
X, y = load_data()
suggestions = claw.query(f"""
I have a dataset with {X.shape[1]} features: {X.columns.tolist()}.
Target is binary classification.
Suggest:
1. Feature engineering steps
2. Scaling/normalization approach
3. Potential feature selection methods
""")
# Generate ML pipeline code
pipeline_code = claw.query("""
Write scikit-learn code for:
1. Train/test split with stratification
2. StandardScaler for numeric features
3. RandomForestClassifier with hyperparameter tuning
4. Cross-validation with 5 folds
5. Feature importance extraction
""")Jupyter Notebook Integration
# In your Jupyter notebook
%load_ext openclaw.magics
# Quick queries
%claw Explain what this code does: [paste code]
# Generate documentation
%claw --type docstring Write a docstring for this function: [paste function]
# Debug assistance
%claw --type debug Why is this loop not working? [paste code]Data Cleaning Help
import pandas as pd
import openclaw
claw = openclaw.Client()
df = pd.read_csv("messy_data.csv")
# Analyze data quality
quality_report = claw.query(f"""
Analyze this DataFrame info:
{df.info()}
{df.isnull().sum()}
Provide:
1. Data quality assessment
2. Missing value strategy recommendations
3. Outlier detection approach
4. Data type corrections needed
""")Statistical Analysis
# Get statistical guidance
stats_help = claw.query("""
I'm comparing two groups (n1=45, n2=52).
Data: approximately normal distribution, equal variance assumption questionable.
Recommend appropriate statistical test and provide scipy code to run it.
""")
# Experimental design advice
design = claw.query("""
I want to test if a new website design increases conversion.
Current conversion rate: 3.5%
Expected improvement: 20%
Calculate required sample size for 80% power at alpha=0.05.
""")Visualization Assistance
# Generate matplotlib/seaborn code
viz_prompt = """
Create a dashboard with 4 subplots:
1. Time series line chart with trend line
2. Distribution histogram with KDE
3. Correlation heatmap
4. Box plot by category
Use seaborn style, color palette: 'husl'
"""
viz_code = claw.query(viz_prompt)
# Or use OpenClaw to explain visualizations
explain = claw.query("Explain what this correlation matrix tells me about the relationships between variables.")Automated Reporting
def generate_data_report(df, title):
claw = openclaw.Client()
report = f"""
# {title}
## Dataset Overview
- Shape: {df.shape}
- Columns: {', '.join(df.columns)}
## Key Insights
{claw.query(f"Summarize key insights from: {df.describe().to_dict()}").text}
## Recommendations
{claw.query("Based on the dataset characteristics, suggest 3 areas for further analysis.").text}
"""
return reportScientific Computing Integration
import numpy as np
import openclaw
claw = openclaw.Client()
# NumPy operations help
numpy_help = claw.query("""
I have a 3D array of shape (100, 50, 30).
I need to:
1. Normalize along axis 0
2. Apply a moving average filter
3. Extract slices where condition is met
Show efficient NumPy code.
""")
# Scientific workflow optimization
workflow = claw.query("""
Optimize this computation for memory efficiency:
[paste your code]
Consider: chunking, memory mapping, vectorization.
""")Best Practices
- Review generated code: Always verify AI-generated code before running on important data
- Provide context: Include column names, data types, and business context in prompts
- Use version control: Track both your code and the prompts that generated it
- Iterate prompts: Refine based on output quality
- Privacy matters: Don't send sensitive personal data to external models
Error Handling
import openclaw
try:
client = openclaw.Client(timeout=30)
response = client.query("Analyze this dataset", max_tokens=1000)
except openclaw.TimeoutError:
print("Request timed out - try a shorter prompt")
except openclaw.APIError as e:
print(f"API error: {e}")
except openclaw.RateLimitError:
print("Rate limited - waiting before retry")