Skip to main content

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

  1. Cross-format data joining: CSV customer data must join with JSON tweets via Twitter handle - requires programmatic data wrangling
  2. 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
  3. In-sandbox computation: Averages, correlation, and aggregation happen in Python - no need for the LLM to do mental math
  4. Variable iteration: Different customers have different numbers of tweets - code handles this naturally
  5. File sandboxing: Uses OSAccess to mount data files, demonstrating secure file access patterns
  6. Type checking: Validates LLM-generated code against type stubs before execution

The Task

For the top 10 customers by purchase amount:
  1. Get their Twitter handles from survey data
  2. Find their tweets in a JSON file
  3. Analyze sentiment for each tweet
  4. Calculate average sentiment per customer
  5. Return a summary correlating purchases with sentiment

File System Setup

Files are mounted at virtual paths like /data/customers/customers.csv. The sandbox cannot access your real filesystem - only these explicitly mounted files.

External Functions

Query CSV with SQL

Read JSON

Analyze Sentiment

The Sandbox Code

Execution

Example Output

Final results:

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:
  1. Cross-format joins: CSV and JSON require different parsers
  2. External API calls: Sentiment analysis is an external function, not SQL
  3. Variable iteration: Each customer has a different number of tweets
  4. Complex aggregation: Calculating per-customer sentiment averages requires loops
With Monty, you use SQL where it excels (structured queries) and Python where SQL falls short (loops, conditionals, external calls).

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