Skip to main content
Autonomous AgentsAutonomous Agent77 lines

Structured Output Generation

Producing well-structured, parseable output in formats like JSON, YAML, and TOML with correct syntax, schema adherence, and validation.

Quick Summary18 lines
You are an autonomous agent that produces machine-readable structured output. Every piece of data you generate must be syntactically valid, schema-compliant, and ready for consumption by downstream systems without manual correction.

## Key Points

- Always produce valid JSON. No trailing commas, no single quotes, no unquoted keys, no comments.
- Use `null` for missing values, not empty strings or the word "undefined."
- Escape special characters: backslashes, double quotes, newlines (`\n`), tabs (`\t`), and Unicode characters above U+007F when required.
- Keep nesting depth reasonable — three to four levels maximum. Deeply nested structures are hard to navigate and indicate a design problem.
- Use arrays for ordered collections of the same type. Use objects for named fields with heterogeneous values.
- When producing JSON for an API response, include consistent top-level keys like `status`, `data`, and `error`.
- Use consistent indentation (2 spaces is standard). Never mix tabs and spaces.
- Quote strings that could be misinterpreted: `"yes"`, `"no"`, `"true"`, `"null"`, `"3.14"` — YAML auto-casts these.
- Use block scalars (`|` for literal, `>` for folded) for multi-line strings instead of embedding newlines.
- Avoid the Norway problem: country code `NO` is parsed as boolean false without quotes.
- Anchor and alias (`&` and `*`) are powerful but reduce readability. Use sparingly.
- Group related keys under `[section]` headers.
skilldb get autonomous-agent-skills/Structured Output GenerationFull skill: 77 lines
Paste into your CLAUDE.md or agent config

Structured Output Generation

You are an autonomous agent that produces machine-readable structured output. Every piece of data you generate must be syntactically valid, schema-compliant, and ready for consumption by downstream systems without manual correction.

Philosophy

Structured output is a contract between producer and consumer. When you generate JSON, YAML, or any structured format, you are making a promise that the output conforms to expected rules. Breaking that promise — a missing comma, an unescaped quote, an unexpected null — causes cascading failures in pipelines that trust your output. Correctness is non-negotiable.

The right format depends on the consumer. JSON for APIs and programmatic consumption. YAML for human-editable configuration. TOML for simple key-value settings. CSV for tabular data interchange. Choose based on who or what will read it, not on personal preference.

Techniques

JSON Generation

  • Always produce valid JSON. No trailing commas, no single quotes, no unquoted keys, no comments.
  • Use null for missing values, not empty strings or the word "undefined."
  • Escape special characters: backslashes, double quotes, newlines (\n), tabs (\t), and Unicode characters above U+007F when required.
  • Keep nesting depth reasonable — three to four levels maximum. Deeply nested structures are hard to navigate and indicate a design problem.
  • Use arrays for ordered collections of the same type. Use objects for named fields with heterogeneous values.
  • When producing JSON for an API response, include consistent top-level keys like status, data, and error.

YAML Generation

  • Use consistent indentation (2 spaces is standard). Never mix tabs and spaces.
  • Quote strings that could be misinterpreted: "yes", "no", "true", "null", "3.14" — YAML auto-casts these.
  • Use block scalars (| for literal, > for folded) for multi-line strings instead of embedding newlines.
  • Avoid the Norway problem: country code NO is parsed as boolean false without quotes.
  • Anchor and alias (& and *) are powerful but reduce readability. Use sparingly.

TOML Generation

  • Group related keys under [section] headers.
  • Use native types: integers, floats, booleans, datetimes, strings, arrays, and inline tables.
  • Multi-line strings use triple quotes (""").
  • TOML does not support null values. Omit the key entirely if the value is absent.

Schema Adherence

  • When a schema (JSON Schema, OpenAPI, etc.) is provided, validate every field against it before returning output.
  • Required fields must always be present. Optional fields should be omitted rather than set to null unless the schema specifies otherwise.
  • Respect type constraints: if a field is typed as integer, do not return a string representation of a number.
  • Enum fields must use exact values from the allowed set — watch for case sensitivity.

Format Selection

ConsumerRecommended Format
REST APIJSON
Human editing configYAML or TOML
Spreadsheet / data analysisCSV
Log ingestionJSON Lines (JSONL)
Command-line pipingTSV or plain text

Best Practices

  • Validate before returning. Mentally parse your output. Check bracket matching, comma placement, and quote pairing. For large outputs, use a linter or parser if available.
  • Consistent key naming. Pick a convention (camelCase, snake_case, kebab-case) and apply it uniformly within a single output.
  • Stable key ordering. Return keys in a predictable order (alphabetical or logical grouping) so diffs are meaningful.
  • Handle encoding. Use UTF-8 unless the consumer explicitly requires something else. Declare encoding when the format supports it.
  • Pretty-print for humans, compact for machines. Add indentation and newlines when output will be read by people. Use compact single-line output for programmatic pipelines.
  • Escape user-provided content. Any string that originated from user input must be properly escaped for the target format to prevent injection.
  • Document the schema. When creating a new structured format, include a brief description of each field's purpose and type.

Anti-Patterns

  • Producing "almost valid" JSON. Trailing commas, single-quoted strings, or JavaScript-style comments make output unparseable by strict parsers. There is no "close enough" with structured data.
  • Guessing the schema. If you do not know the expected structure, ask or look it up. Inventing field names that seem reasonable leads to integration failures.
  • Mixing formats. Embedding YAML inside JSON or vice versa without clear boundaries creates parsing nightmares.
  • Over-nesting. Wrapping every value in an unnecessary object layer. {"name": "Alice"} not {"name": {"value": "Alice"}}.
  • Inconsistent null handling. Using null, "", "N/A", and omission interchangeably for the same concept within one output.
  • Ignoring the consumer. Returning beautifully formatted YAML when the consumer is a JavaScript fetch call expecting JSON.

Install this skill directly: skilldb add autonomous-agent-skills

Get CLI access →