GC-301c · Module 3
Structured Output & Format Constraints
3 min read
Structured output transforms Gemini CLI from a conversational tool into a programmatic one. When you need JSON, YAML, CSV, or any machine-parseable format, explicit format constraints in your GEMINI.md or custom commands ensure the model produces parseable output every time. Without constraints, the model wraps structured data in explanatory prose, adds markdown formatting, or includes comments that break parsers. With constraints, the output is clean, consistent, and pipe-ready.
The most reliable approach is specifying output schema in your prompt or GEMINI.md. Define the exact JSON structure you expect, including field names, types, and whether fields are required or optional. The model follows explicit schemas with high fidelity. For recurring structured outputs, encode the schema in a custom command so every invocation produces the same format. This is how you build reliable automation on top of Gemini CLI — structured, predictable, parseable output that downstream scripts can consume without fragile regex parsing.
[command]
name = "analyze-deps"
description = "Analyze dependencies and output structured JSON"
prompt = """
Analyze the project dependencies in !{cat package.json}.
Output ONLY a JSON object with this exact schema (no markdown, no explanation):
{
"total": <number>,
"outdated": [
{
"name": "<package-name>",
"current": "<installed-version>",
"latest": "<latest-known-version>",
"severity": "major|minor|patch",
"risk": "<one sentence risk assessment>"
}
],
"unused_suspected": ["<package-names that appear in deps but not in source>"],
"security_concerns": ["<packages with known CVEs or maintenance issues>"]
}
"""
Do This
- Specify exact output schemas with field names, types, and examples
- Use "output ONLY" directives to suppress conversational wrapping
- Test structured output commands with edge cases to verify format consistency
Avoid This
- Ask for "JSON output" without specifying the schema — the model will improvise field names
- Assume structured output is always parseable — always validate before piping to downstream tools
- Mix structured and conversational output in the same command — separate analysis from data