*! version 2.3.2 27apr2026

Title

gemini -- Use Gemini AI directly from the Stata command line to support research, coding, and data tasks.

Syntax

gemini query [, options]

csv Format results as a CSV table and import into Stata memory.

json Format results as JSON and import into Stata memory (requires Python).

clear Required if csv or json is used and data is already in memory.

path(string) Specify the absolute path to the gemini executable.

model(string) Specify the Gemini model (e.g., gemini-1.5-flash).

Description

gemini integrates the Gemini CLI with Stata. It allows you to perform live web searches,
generate synthetic datasets, get coding assistance, and extract structured data directly into Stata
using Google's Gemini models. This command requires the @google/gemini-cli to be installed
on your system.

Options

csv: Instructs Gemini to return data in CSV format, which is then automatically imported into Stata.

json: Instructs Gemini to return data in JSON format. This requires Python with the pandas library installed to convert the data for Stata import.

clear: Standard Stata option to clear existing data from memory before importing new results.

path(string): Use this if the command cannot find your gemini installation. Provide the full path (e.g., /usr/local/bin/gemini or C:\Users\Name\AppData\Roaming\npm\gemini.cmd).

model(string): Specify the model to use. Common options include gemini-1.5-flash (fast/cheap) and gemini-1.5-pro (complex tasks).

Examples

1. General Research

. gemini "What are the current top 5 largest cities in Texas by population?"

2. Data Extraction

. gemini "List the top 5 largest cities in Texas with their 2020 and 2024 estimated population", csv clear

3. Synthetic Data

. gemini "Generate a synthetic dataset of 100 observations with 'age', 'income', and 'education' (HS, College, Grad)", csv clear

4. Stata Coding Help

. gemini "How do I use the -margins- command after a probit regression with an interaction term?"

Installation Guide

This command is a wrapper for the Gemini CLI. You MUST install the CLI first.

Step 1: Install Node.js

macOS: Install via Homebrew: brew install node

Windows: Download the installer from https://nodejs.org/ (LTS version recommended).

Linux (Ubuntu/Debian): sudo apt update && sudo apt install nodejs npm

Step 2: Install Gemini CLI

Open your terminal (Terminal on Mac/Linux, PowerShell on Windows) and run:

npm install -g @google/gemini-cli

Step 3: Authentication

In your terminal, run:

gemini

Follow the prompts to log in to your Google account and authorize the CLI.

Troubleshooting

Command not found in Stata

Stata's shell environment may have a different PATH than your terminal.
Run which gemini (Mac/Linux) or where gemini (Windows) in your terminal to find the path, then use the path() option in Stata:

gemini "test", path("/usr/local/bin/gemini")

Permission Errors (Mac/Linux)

If npm install fails, you may need to use sudo or fix your npm permissions.
Using a version manager like nvm is recommended to avoid permission issues.

Execution Policy (Windows)

If PowerShell blocks the script, run PowerShell as Administrator and execute:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

JSON Import Fails

Ensure Python is installed and accessible to Stata (python query).
Install pandas: pip install pandas.

Bonus: Alternative 'stata_ai' (Local LLM via Ollama)

For users on macOS who require total data privacy, you can install and run a local LLM (Sandboxed) using Ollama. This allows you to generate Stata code without any data leaving your machine.

Copy and paste the following block into your Terminal. This script will install Ollama, download the top-performing Stata model (Gemma 3), and set up an alias to generate code instantly from your terminal or via !stata_ai in Stata.

# Install Ollama runner

brew install ollama

# Start Ollama in the background

nohup ollama serve > /dev/null 2>&1 &

# Pull Gemma 3 (12B) - High-accuracy for Stata

ollama pull gemma3:12b

# Create a 'stata_ai' alias for precision

alias stata_ai='ollama run gemma3:12b "Act as an expert Stata programmer. Provide only the code, no explanation unless asked. My query: "'

Why this setup?

1. Gemma 3: Identified as a top local performer for Stata (77.8%+ accuracy).

2. Low Temp: The alias forces logical consistency over creativity.

3. Privacy: The entire pipeline is local; sensitive data never leaves your computer.

Author

Eric A. Booth

eric.a.booth@gmail.com

Texas 2036 (https://texas2036.org/)

https://github.com/ericbooth/Gemini-stata