llm-json-output

verified

470363d3-bbe1-42ee-9453-663d7e2e6e83

Get reliable structured JSON from LLMs — schema enforcement, delimiters, validation, and retry loops.

Metadata

Skill ID
470363d3-bbe1-42ee-9453-663d7e2e6e83
Version
1
Owner
global
Tags
llmjsonstructured-outputaifunction-callingparsing
Signature
verified
Integrity
OK
Content hash
ebaee55f842265be869bd5c23a492a57d5be66de4db3062b0e5f42a2e5c6469e
Created
2026-08-05T18:29:00Z

Skill file

Raw skill file (markdown source)
# Reliable Structured JSON from LLMs

Use when an LLM must return machine-parseable JSON — for tool results, configs,
or data you validate downstream.

## Prefer native structured output when available

Many providers offer a JSON schema / structured-output mode that constrains
generation. If available, pass your Pydantic schema and skip manual prompt hacks.

## Fallback: prompt with a schema

```text
Return a JSON object with exactly these keys:
{"name": string, "port": int, "enabled": bool}
No other text, no markdown fences.
```

Add 1-2 examples of the exact shape you want.

## Always validate + retry

Never trust the raw string. Parse, validate against your model, and on failure
re-prompt with the error (`Invalid JSON: {e}. Please retry`). Cap retries (2-3);
if still failing, fall back to a degraded default.

## Strip markdown fences

Models often wrap JSON in ` ```json ` fences — strip them before `json.loads`.

## Pitfalls

- JSON is order-sensitive to the model's tokens, not to you: keep the schema small.
- Booleans/numbers coerce surprisingly; validate types, not just that it parses.
- Don't paste secrets or huge schemas into the prompt — it costs tokens and dilutes output.

Attached files