DEV Community

Orbit Websites
Orbit Websites

Posted on

Specsmaxxing: Overcoming AI Psychosis and the YAML Advantage

Specsmaxxing: Overcoming AI Psychosis and the YAML Advantage

AI tools like LLMs are powerful—but they often hallucinate, misinterpret, or generate code that looks right but fails in practice. This phenomenon—dubbed AI psychosis—can derail projects, especially when building complex systems from vague prompts.

The solution? Specsmaxxing: the practice of maximizing clarity, structure, and precision in specifications to guide AI effectively. In this tutorial, you’ll learn how to use YAML as a lightweight, human-readable spec format to eliminate ambiguity and produce reliable AI-generated code.

By the end, you’ll:

  • Understand what AI psychosis is and how it impacts development
  • Learn to write precise specs in YAML
  • Use those specs to generate working Python code with AI
  • Validate outputs automatically

Let’s dive in.


What is AI Psychosis?

AI psychosis refers to when an AI confidently generates incorrect, inconsistent, or nonsensical output. For example:

Prompt: "Write a Python function to validate email addresses."

AI Output:

def validate_email(email):
    return "@" in email  # 🚨 Too simplistic!

This passes the test for user@example.com, but also for @@@, which is invalid.

Problem: Vague prompts → vague outputs.

Solution: Use structured specs to define behavior precisely.


Why YAML? The Specsmaxxing Advantage

YAML (YAML Ain't Markup Language) is ideal for specs because it’s:

  • Human-readable
  • Hierarchical
  • Widely supported
  • Easy to parse in Python

Instead of saying "validate emails," we’ll define:

  • Input/Output types
  • Validation rules
  • Test cases
  • Error messages

This forces clarity—and gives AI a blueprint.


Step 1: Define a Spec in YAML

Let’s create a spec for our email validator.

Create email_validator.yaml:

function: validate_email
description: Validates an email address based on format rules
inputs:
  email:
    type: string
    description: The email address to validate
outputs:
  valid:
    type: boolean
    description: True if email is valid, False otherwise
  error:
    type: string or null
    description: Error message if invalid, else null
rules:
  - "Must contain exactly one '@'"
  - "Local part (before @) must not be empty"
  - "Domain part (after @) must contain at least one '.'"
  - "Domain must not end with '.'"
  - "No spaces allowed"
test_cases:
  - input: { email: "user@example.com" }
    output: { valid: true, error: null }
  - input: { email: "user@.com" }
    output: { valid: false, error: "Domain must contain a valid label after '@'" }
  - input: { email: "user@@example.com" }
    output: { valid: false, error: "Must contain exactly one '@'" }
  - input: { email: "user example.com" }
    output: { valid: false, error: "No spaces allowed" }
  - input: { email: "@example.com" }
    output: { valid: false, error: "Local part cannot be empty" }
Enter fullscreen mode Exit fullscreen mode

This spec is unambiguous. AI can now generate code that matches exact behavior.


Step 2: Generate Code Using the Spec

Now, feed this spec to an AI (e.g., ChatGPT, Claude, or local LLM). Use a prompt like:

You are a Python developer. Generate a function based on this YAML spec. Do not add extra features. Follow the rules and test cases exactly.

The AI should output something like:

def validate_email(email):
    """
    Validates an email address based on format rules.
    """
    if not isinstance(email, str):
        return {"valid": False, "error": "Email must be a string"}

    if " " in email:
        return {"valid": False, "error": "No spaces allowed"}

    if email.count("@") != 1:
        return {"valid": False, "error": "Must contain exactly one '@'"}

    local, domain = email.split("@")

    if not local:
        return {"valid": False, "error": "Local part cannot be empty"}

    if "." not in domain:
        return {"valid": False, "error": "Domain part must contain at least one '.'"}

    if domain.endswith("."):
        return {"valid": False, "error": "Domain must not end with '.'"}

    return {"valid": True, "error": None}
Enter fullscreen mode Exit fullscreen mode

✅ Matches all rules

✅ Handles all test cases

✅ Returns structured output


Step 3: Automate Testing with the Spec

Let’s write a Python script to validate the generated function against the YAML test cases.

Install PyYAML:

pip install pyyaml
Enter fullscreen mode Exit fullscreen mode

Create test_validator.py:


python
import yaml

# Load spec
with open("email_validator.yaml", "r") as f:
    spec = yaml.safe_load(f)

# Import or define the function
def validate_email(email):
    # ... (paste the generated function here)
    pass

# Run tests
def run_tests():
    test_cases = spec["test_cases"]
    passed = 0
    total = len(test_cases)

    for i, case in enumerate(test_cases):
        email = case["input"]["email"]
        expected = case["output"]
        result = validate_email(email)

        if result == expected:
            print(f"✅ Test {i+

---

☕ **Appreciative**
Enter fullscreen mode Exit fullscreen mode

Top comments (0)