---
name: gemini-text-audit
description: Use when asked to audit, edit, proofread, improve, rewrite, or review text quality using Gemini AI. Triggers on "audit this", "improve this article", "edit for style/tone/length", "check writing quality", "rewrite this", "fix grammar". Uses Gemini 3.1 Pro.
---

# Gemini Text Audit

## Overview

Send text tasks (audit, edit, rewrite, proofread) to Gemini 3.1 Pro via google-genai SDK.

## Setup

```bash
pip install -q google-genai
```

## Core Pattern

```python
from google import genai

client = genai.Client(api_key="YOUR_KEY")
response = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents=prompt
)
print(response.text)
```

---

## LLM-as-Judge: Yes/No Audit (preferred for structured audits)

Use yes/no questions instead of free-form critique. This produces objective, automatable results with a precise defect count.

**Why:** Free-form critique is vague and self-serving — the model "forgives" its own earlier output. Yes/no questions force binary answers, are parseable, and generate exact fix instructions only where needed.

### Pattern

```python
import re
from google import genai

API_KEY = "AIza..."
MODEL = "gemini-3.1-pro-preview"

with open("original.txt", encoding="utf-8") as f:
    original = f.read()
with open("edited.txt", encoding="utf-8") as f:
    edited = f.read()

# Python word count — ground truth, never ask Gemini
word_count = len(edited.strip().split())

JUDGE_PROMPT = f"""אתה שופט איכות עצמאי. לפניך הטקסט המקורי והגרסה הערוכה.
ענה על כל שאלה בפורמט: N. [כן/לא] — [הסבר קצר אם "לא", "✓" אם "כן"]
חשוב: ענה "כן" או "לא" בלבד בתחילת כל תשובה. אל תרחיב אם התשובה "כן".

==== שלמות טיעונית ====
1. האם [element X] נשמר?
2. האם [element Y] נשמר?

==== דיוק עובדתי ====
N. האם שמות/תאריכים/ציטוטים מדויקים?
N. האם לא נוסף מידע שלא היה במקור?

==== קול וסגנון ====
N. האם הסגנון של [author] נשמר?
N. האם הטון לא נוטרל?

==== שפה ====
N. האם אין שגיאות כתיב?
N. האם אין שגיאות תחביר?

==== סיכום ====
סה"כ ❌: [מספר]
אם יש "לא" — פרט: מה חסר/שגוי + תיקון מוצע מילה במילה.

==== הטקסט המקורי ====
{original}

==== הגרסה הערוכה ====
{edited}
"""

# Fresh client — completely separate from any prior shortening call
client = genai.Client(api_key=API_KEY)
response = client.models.generate_content(model=MODEL, contents=JUDGE_PROMPT)
audit_result = response.text

# Count failures
no_count = len(re.findall(r'\d+\.\s+לא\b', audit_result))
print(audit_result)
print(f'\nסה"כ "לא": {no_count}')
print(f'Word count: {word_count}')
```

### Key rules
- **Fresh `genai.Client`** for the audit — never reuse the client from the shortening call
- **Python counts words** — `len(edited.strip().split())` — never ask Gemini
- **Parse failures** with regex: `re.findall(r'\d+\.\s+לא\b', text)`
- ~15–20 questions for an op-ed; group by: שלמות / עובדתי / סגנון / שפה
- Request word-for-word fix suggestions for each "לא"

---

## Free-form Text Audit

```python
from google import genai

def audit_text(text: str, api_key: str, instructions: str = None) -> str:
    client = genai.Client(api_key=api_key)
    system = instructions or (
        "You are a professional editor. Audit the given text for: "
        "grammar, clarity, style, flow, and tone. "
        "Return: 1) Issues found, 2) Improved version."
    )
    prompt = f"{system}\n\nText to audit:\n{text}"
    response = client.models.generate_content(
        model="gemini-3.1-pro-preview",
        contents=prompt
    )
    return response.text
```

## Model

Always use: `gemini-3.1-pro-preview`
Never use: `gemini-2.5-pro` or flash variants for quality text work.

## Common Tasks

| Task | Prompt hint |
|------|-------------|
| Proofread | "Fix grammar and spelling, keep original style" |
| Rewrite | "Rewrite for [audience/tone], same meaning" |
| Shorten | "Shorten to ~N words, keep key points" |
| Format | "Format with headers/bullets matching [example]" |
| Translate | "Translate to [language], natural phrasing" |

## Notes
- For Hebrew text: specify "in Hebrew" in instructions
- Pin to `gemini-3.1-pro-preview` explicitly
