Natural Language Programming: Using Structured Prompts for Reliable AI Coding

    Learn how to define AI coding tasks with six elements: role, goal, context, constraints, workflow, and output. This guide compares Markdown, YAML, JSON, XML, and pseudocode, with practical examples for communicating requirements clearly and keeping implementation within scope.

    1. Treat AI as an Automation System

    Before using AI for programming, establish a useful mental model: AI is not a conversational partner that can read your mind. Treat it as an automation system that can interpret natural language, break down tasks, and, when equipped with tools, perform operations. A basic principle of automation applies: clearer inputs make outputs more controllable.

    In traditional software development, API documentation, interface contracts, and configuration files define how programs should behave. The same approach works for AI-assisted programming. Rather than asking AI to infer your intentions from unstructured text, use a structured task specification to explain what you need.

    When developers complain that AI-generated code does not meet expectations, the problem often lies in vague requirements rather than model capability alone. Consider this request:

    Help me optimize this Python project.

    The AI must infer several missing details: Does “optimize” mean better performance or improved readability? Which parts of the project can change? What technology stack does it use? Without those answers, it may fall back on common assumptions and produce changes that miss the intended goal.

    2. A Six-Part Framework for Defining Tasks

    A complete AI coding task can be organized into six elements:

    • Role: The perspective and expertise the AI should apply. A senior Python engineer and a beginner-focused teaching assistant will approach the same task differently.
    • Goal: The result you want to achieve. Make it specific and verifiable rather than asking the AI to “make it better” or “optimize it.”
    • Context: The project, environment, and existing conditions. Include relevant technology versions, directory structure, and known issues.
    • Constraints: The limits on what the AI may do. These boundaries help prevent unnecessary changes and scope expansion.
    • Workflow: The steps the AI should follow. An explicit sequence helps discourage jumping straight into implementation without analysis.
    • Output: The required deliverables and their format. A clear delivery checklist makes the result easier to review.

    Vague vs. Structured Requests

    A vague request:

    Help me optimize this Python project.

    A structured request:

    1. # Role
    2. You are a senior Python engineer.
    3. # Goal
    4. Improve the performance of the current project while preserving
    5. existing functionality.
    6. # Context
    7. - The project uses Python 3.12, primarily with FastAPI and SQLAlchemy.
    8. - Some API endpoints currently have slow response times.
    9. # Constraints
    10. - Do not change existing API interfaces.
    11. - Do not upgrade dependency versions without justification.
    12. - Keep code changes as limited as possible.
    13. # Workflow
    14. 1. Review the project structure and relevant code.
    15. 2. Analyze performance bottlenecks.
    16. 3. Propose optimizations and explain potential side effects.
    17. 4. Implement the changes.
    18. 5. Run tests and review the results.
    19. # Output
    20. - A list of modified files
    21. - The reason for each change
    22. - Expected performance changes and test results

    This structure provides two main benefits. First, it makes key decisions explicit, reducing the need for the AI to guess your intentions. Second, it helps keep a targeted optimization task from becoming a full-project refactor. That is the purpose of the constraints section.

    3. Choosing Among Five Prompt Formats

    Different formats offer different ways to express and parse requirements. Choose one that fits the task.

    Markdown: A Practical Default

    Markdown is flexible, hierarchical, and easy for both humans and AI to read. It works well for requirements, task breakdowns, code-change instructions, and project planning.

    For most everyday programming tasks, Markdown is a good starting point.

    YAML: For Parameter-Driven Tasks with Many Constraints

    YAML uses key-value pairs and indentation to represent structure. It is well suited to configuration details and batch-task parameters.

    For example, a batch image-compression task can be defined as follows:

    1. task:
    2. name: "Batch image compression"
    3. goal: "Compress images in the specified directory"
    4. input:
    5. directory: "./images"
    6. formats: [jpg, png]
    7. constraints:
    8. preserve_original: true
    9. max_width: 1920
    10. max_height: 1080
    11. workflow:
    12. - scan_directory
    13. - identify_images
    14. - compress_images
    15. - generate_report
    16. output:
    17. format: "csv"
    18. include: [filename, original_size, compressed_size, compression_ratio]

    The input directory, supported formats, dimension limits, original-file preservation setting, and report fields are all visible in one place. This reduces ambiguity about the specified parameters.

    YAML is useful for configuration generation, automation workflows, and batch operations.

    JSON: For Strictly Structured Inputs and Outputs

    JSON is appropriate when a task needs to integrate with software, such as API calls, data-processing pipelines, or agent-tool interactions. Its strict syntax supports machine parsing.

    However, JSON is more cumbersome to write manually and does not support standard comments, making it less convenient for everyday conversational tasks.

    XML: For Separating Content Blocks in Complex Prompts

    XML uses paired tags to distinguish different types of content:

    1. <instruction>Answer the question using the reference material.</instruction>
    2. <background>Project background information goes here...</background>
    3. <reference>Reference code snippets go here...</reference>
    4. <output_format>Use a numbered list.</output_format>

    When a prompt contains instructions, background information, reference text, and output requirements, tagged boundaries help distinguish reference material from instructions to follow.

    Pseudocode: For Complex Logic and Execution Flow

    For requirements involving conditional branches, state transitions, or execution order, pseudocode can be more precise than prose. It reduces ambiguity by making the intended logic explicit.

    1. FOR each order IN pending_orders:
    2. IF order.amount > 10000:
    3. REQUIRE manual_review
    4. ELSE:
    5. auto_approve(order)

    This makes the condition and the required action easier to interpret than a loosely worded description.

    4. Practical Guidelines

    1. Define boundaries before writing the requirements. Constraints can be just as important as the goal. Clearly stating what must not change can substantially reduce rework.
    2. Provide enough context, but avoid excess. Unrelated code can obscure the important information. Supply the modules and dependency details relevant to the task.
    3. Agree on the output format upfront. A consistent structure—such as modified files, reasons for changes, and validation results—makes review and subsequent automated checks easier.
    4. Break complex tasks into stages. For larger tasks, ask the AI to propose a plan first. Review it before authorizing implementation rather than leaving a long workflow to run unchecked.

    5. Conclusion

    Treating AI as an automation tool brings AI-assisted programming back to requirements engineering. The six elements—role, goal, context, constraints, workflow, and output—correspond to responsibilities, acceptance criteria, environment details, boundaries, execution steps, and delivery contracts.

    Clear task definitions, combined with an appropriate format such as Markdown, YAML, JSON, XML, or pseudocode, can improve the quality and predictability of AI-generated work. The aim is not to write a longer prompt, but to make the decisions that matter explicit.