How GPT-Image2 Prompt Templates Are Structured: A Complete Guide

GPT-Image2 uses placeholder-driven markdown templates stored in docs/templates.md, combining section headers, bracketed placeholders like [产品类型], optional JSON variants, and avoid-pitfalls guidelines to ensure deterministic image generation.

The freestylefly/awesome-gpt-image-2 repository defines a systematic approach to prompt engineering through reusable templates. Understanding how GPT-Image2 prompt templates are structured allows developers to generate consistent, high-fidelity images by filling predefined placeholders with concrete values rather than crafting free-form descriptions.

Core Components of GPT-Image2 Prompt Templates

Section Headers

Templates are organized under markdown headings in docs/templates.md that group related visual domains. These section headers categorize prompts by use case—such as ### UI与界面 for interface design or infographics and poster layouts—enabling quick navigation for specific generation tasks.

Placeholder-Rich Prompt Blocks

Each template contains bracketed placeholders (e.g., [产品类型], [平台], [功能点A]) that act as variables to be replaced at runtime. These placeholders are surrounded by square brackets with descriptive Chinese labels, allowing the prompt structure to remain constant while the specific content changes based on user input or application state.

Optional JSON Variants

For programmatic consumption, templates provide machine-readable JSON versions alongside plain text. These structured objects mirror the placeholder logic but use key-value pairs, making them ideal for API integrations where agents prefer structured input over natural language strings.

Avoid-Pitfalls Guidelines

Every template includes a bullet list of constraints that specify which details must be explicitly locked to prevent model hallucination. These guidelines enforce concrete values—for example, mandating that 文字必须准确显示指定的中文 or specifying exact aspect ratios—to guarantee deterministic output quality.

Template Anatomy and Placeholder Syntax

The standard template layout in docs/templates.md follows a consistent pattern where placeholders define the variable components of the generation request:

为[产品类型]生成一张[平台,如 iOS/Android/Web]界面图。
核心功能:[功能点A]、[功能点B]、[功能点C]。
视觉风格:[极简/科技/拟物],主色[颜色],强调色[颜色]。
布局:[顶部导航/双栏/卡片流],信息层级清晰,留白充足。
输出:高保真UI截图,文字清晰可读,比例[9:16/16:9]。

When building a prompt, the implementation requires four distinct steps: selecting the appropriate domain template, supplying concrete values for every placeholder, optionally serializing the filled template to JSON for API calls, and honoring the avoid-pitfalls rules to prevent style mixing or unreadable text.

Programmatic Template Processing

Filling Text Templates with JavaScript

The repository demonstrates placeholder replacement through utility functions that parse bracketed syntax. The fillTemplate function in utils/promptBuilder.js uses regex to substitute values while enforcing that all required fields are present:

// utils/promptBuilder.js
export function fillTemplate(template, data) {
  return template.replace(/\[([^\]]+)]/g, (_, key) => {
    const value = data[key.trim()];
    if (value === undefined) throw new Error(`Missing value for ${key}`);
    return value;
  });
}

// Usage example
const uiTemplate = `
为[产品类型]生成一张[平台]界面图。
核心功能:[功能点A]、[功能点B]、[功能点C]。
视觉风格:[极简],主色[颜色],强调色[颜色]。
布局:[顶部导航],信息层级清晰,留白充足。
输出:高保真UI截图,文字清晰可读,比例[9:16]。
`;

const prompt = fillTemplate(uiTemplate, {
  '产品类型': '健身应用',
  '平台': 'iOS',
  '功能点A': '计步',
  '功能点B': '卡路里统计',
  '功能点C': '运动计划',
  '极简': '极简',
  '颜色': '深蓝',
  '顶部导航': '双栏',
});

Generating JSON-Structured Prompts

For API integrations, the buildJson helper recursively processes template objects to replace nested placeholders. This approach, utilized by scripts/generate-style-skill.mjs, ensures that structured prompts maintain the same validation rules as their text counterparts:

// utils/jsonTemplate.js
export function buildJson(templateObj, data) {
  const result = {};
  for (const [k, v] of Object.entries(templateObj)) {
    if (typeof v === 'string') {
      result[k] = v.replace(/\[([^\]]+)]/g, (_, key) => {
        const val = data[key.trim()];
        if (val === undefined) throw new Error(`Missing ${key}`);
        return val;
      });
    } else {
      result[k] = buildJson(v, data);
    }
  }
  return result;
}

// Example template structure
const jsonTemplate = {
  type: "UI Screenshot",
  platform: "[平台]",
  product: "[产品类型]",
  layout: "[布局]",
  style: {
    theme: "[视觉风格]",
    primary_color: "[颜色]"
  },
  constraints: "High fidelity, readable text, 9:16 aspect ratio"
};

const jsonPrompt = buildJson(jsonTemplate, {
  平台: "iOS",
  产品类型: "健身应用",
  布局: "Card‑based feed with bottom tab bar",
  视觉风格: "Dark Mode",
  颜色: "Neon Green"
});

Key Files in the Repository

The template system relies on specific files that define, process, and serve the prompt structures:

  • docs/templates.md — Master list of all prompt templates containing both plain-text and JSON forms, plus anti-pitfall guidelines for each visual domain.
  • scripts/generate-style-skill.mjs — Example script that reads markdown templates, substitutes placeholders, and posts generated prompts to the GPT-Image2 API.
  • src/main.jsx — Front-end interface where users select template categories and input values, ultimately calling helper functions to assemble the final prompt.
  • utils/promptBuilder.js — Utility implementing the placeholder-replacement logic used across the project for text-based templates.

Summary

  • GPT-Image2 prompt templates reside in docs/templates.md and use bracketed placeholders like [产品类型] for variable substitution.
  • Each template includes section headers for organization, placeholder-rich text for human editing, JSON variants for programmatic use, and avoid-pitfalls guidelines to ensure quality.
  • The placeholder syntax uses square brackets with descriptive labels that must be replaced with concrete values before generation.
  • JavaScript utilities such as fillTemplate and buildJson enforce complete placeholder replacement and support both text and structured JSON output formats.
  • Scripts like generate-style-skill.mjs demonstrate end-to-end usage by reading templates, filling values, and calling the GPT-Image2 API.

Frequently Asked Questions

What file contains the master list of GPT-Image2 prompt templates?

The complete collection of templates is stored in docs/templates.md at the repository root. This file contains all plain-text and JSON variants organized by visual domain, along with specific avoid-pitfalls guidelines for each category.

How do I replace placeholders in a template programmatically?

Use the fillTemplate function from utils/promptBuilder.js, which employs a regex pattern /\[([^\]]+)]/g to identify bracketed keys and substitute them with values from a data object. The function throws an error if any required placeholder lacks a corresponding value, ensuring complete template population.

What is the purpose of the avoid-pitfalls section in templates?

The avoid-pitfalls guidelines act as guardrails that specify which details must be explicitly locked—such as exact text content, aspect ratios, or color values—to prevent the model from generating ambiguous or inconsistent imagery. These constraints ensure deterministic output quality by removing degrees of freedom that could lead to hallucination.

Can GPT-Image2 prompts be generated as JSON instead of plain text?

Yes, templates provide optional JSON variants that use the same placeholder syntax within structured objects. The buildJson utility recursively processes these templates to produce machine-readable prompt objects suitable for direct API integration, while maintaining the same validation rules as text templates.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →