Overview
This example demonstrates a task that cannot be solved with a single SQL query: analyzing customer purchase data (CSV) and correlating it with their social media sentiment (JSON tweets). It showcases Monty’s ability to:- Mount files securely with
OSAccess - Query CSV files with SQL (via DuckDB)
- Load and process JSON data
- Call external functions in loops
- Perform in-sandbox computations
Data is from the mafudge/datasets repository.
Why This Example is Interesting
- Cross-format data joining: CSV customer data must join with JSON tweets via Twitter handle - requires programmatic data wrangling
- Loop-based external calls: Sentiment analysis for each tweet happens in a loop - with JSON tool calling this would flood the context window with 50+ results
- In-sandbox computation: Averages, correlation, and aggregation happen in Python - no need for the LLM to do mental math
- Variable iteration: Different customers have different numbers of tweets - code handles this naturally
- File sandboxing: Uses
OSAccessto mount data files, demonstrating secure file access patterns - Type checking: Validates LLM-generated code against type stubs before execution
The Task
For the top 10 customers by purchase amount:- Get their Twitter handles from survey data
- Find their tweets in a JSON file
- Analyze sentiment for each tweet
- Calculate average sentiment per customer
- Return a summary correlating purchases with sentiment
File System Setup
External Functions
Query CSV with SQL
Read JSON
Analyze Sentiment
The Sandbox Code
Execution
Example Output
Key Patterns
1
SQL for Structured Queries
Use
query_csv() for operations SQL excels at: filtering, sorting, joining CSV data.2
Python for Complex Logic
Use loops and conditionals for tasks SQL can’t handle: cross-format joins, variable iteration, external API calls.
3
In-Loop External Calls
Analyze sentiment for each tweet in a loop. With traditional tool calling, this would create 50+ function call results in the context.
4
In-Sandbox Aggregation
Calculate averages and build result dictionaries in Python. The LLM doesn’t see intermediate data.
Security: File Mounting
- The sandbox sees files at
/data/customers/customers.csv, not your real filesystem - You explicitly choose which files to mount
- Paths are always POSIX-style (forward slashes) even on Windows
- Files are read-only by default
Running the Example
Type Stubs for SQL Functions
Why Not Just SQL?
This task cannot be solved with SQL alone because:- Cross-format joins: CSV and JSON require different parsers
- External API calls: Sentiment analysis is an external function, not SQL
- Variable iteration: Each customer has a different number of tweets
- Complex aggregation: Calculating per-customer sentiment averages requires loops
Next Steps
- Explore the full source in
examples/sql_playground/ - Try Web Scraper for browser automation
- See Data Analysis for async patterns without file mounting
