Aller au contenu
Skillpedia
Prompts et LLM48k · #5091

llm-structured-output

Ce qu’il fait

Cette compétence aide l'IA à extraire des informations précises et organisées (sous forme de données au format JSON, par exemple) à partir d'un texte, au lieu de simplement générer un texte libre. Elle garantit que les informations extraites respectent un format défini et sont fiables pour être utilisées dans d'autres parties d'un programme.

Quand l’utiliser

  • ✓Lorsque vous avez besoin que l'IA renvoie des données dans un format exact, comme une liste d'articles ou des coordonnées.
  • ✓Lorsque vous construisez des systèmes où la sortie de l'IA doit être directement utilisée par d'autres logiciels (par exemple, pour l'enregistrer dans une base de données).
  • ✓Lorsque l'IA renvoie des données désordonnées ou mal formatées et que vous avez besoin d'une solution fiable.

Lien du skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/llm-structured-output/SKILL.md

Copiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.

Comment l’utiliser

Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.

Message prêt à coller

Utilise ce skill pour m’aider : https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/llm-structured-output/SKILL.md Lis le fichier SKILL.md de ce lien et suis ses instructions pour ma tâche.
Envie que votre IA trouve seule des skills comme celui-ci ?Installez le MCP gratuit de Skillpedia : une ligne, et elle cherche parmi 15 000+ skills avant chaque tâche.Installer en 1 minute →

Exemple de demande à votre IA

“Extrayez le nom, l'e-mail et le numéro de téléphone de ce message et renvoyez-les au format JSON : « Bonjour, je m'appelle Maria Silva et mes coordonnées sont maria.silva@email.com, téléphone 11 98765-4321. »”

5 nouveaux skills par semaine. 1 minute de lecture.

Comment ça marche

L'IA reçoit pour consigne de suivre un « schéma » (un modèle de la façon dont les données doivent être organisées) et utilise des outils spécifiques à chaque fournisseur d'IA (OpenAI, Anthropic, Google) pour que la sortie corresponde précisément au schéma.

Contenu technique (SKILL.md original)

Texte original de l’auteur, en anglais, directement depuis GitHub.

LLM Structured Output

What This Skill Does

Extract typed, validated data from LLM API responses instead of parsing free-text. This skill covers the three main approaches: OpenAI's response_format with JSON Schema, Anthropic's tool_use block for structured extraction, and Google's responseSchema in Gemini. You will learn when each approach works, when it breaks, and how to build retry logic around schema validation failures that every production system encounters.

When to Use This Skill

  • The user needs to extract structured data (JSON objects, arrays, enums) from an LLM response
  • The user is building a pipeline where LLM output feeds directly into code (database writes, API calls, UI rendering)
  • The user asks about response_format, json_mode, json_object, or json_schema in OpenAI
  • The user asks about using Anthropic's tool_use or tool_result blocks for data extraction (not for actual tool execution)
  • The user asks about Zod schemas with zodResponseFormat() from the openai npm package
  • The user needs to parse LLM output into Pydantic models using instructor, marvin, or manual validation
  • The user is getting malformed JSON, missing fields, or wrong types from LLM responses and needs a fix
  • The user asks about controlled generation, constrained decoding, or grammar-based sampling in local models

Do NOT use this skill when:

  • The user wants free-form text generation (summaries, essays, chat)
  • The user is asking about Zod for form validation or API input validation (use zod-validation-expert instead)
  • The user needs prompt engineering for better text quality (not structure)
  • The user wants to call real external tools/APIs (this skill covers using tool_use as a structured output hack, not actual tool orchestration)

Core Workflow

  1. Identify the target schema. Ask the user what fields they need extracted. Define every field with its type, whether it's required or optional, and valid enum values if applicable. Do not proceed without a concrete schema.

  2. Choose the provider-appropriate method:

    • OpenAI (gpt-4o, gpt-4o-mini): Use response_format: { type: "json_schema", json_schema: { ... } }. This enables Structured Outputs with guaranteed schema conformance via constrained decoding.
    • Anthropic (Claude): Define a single tool with the target schema as input_schema and set tool_choice: { type: "tool", name: "extract_data" }. Claude returns the structured data in the tool_use content block.
    • Google (Gemini): Use generationConfig.responseSchema with a JSON Schema object and set responseMimeType: "application/json".
    • Local models (llama.cpp, vLLM): Use GBNF grammars or --json-schema flag for constrained decoding at the token level.
  3. Write the schema definition in the user's language. For Python, define a Pydantic BaseModel. For TypeScript, define a Zod schema and convert it with zodResponseFormat(). For raw API calls, write JSON Schema directly.

  4. Include field-level descriptions in the schema. Every field should have a description string that tells the model what to put there. Models use these descriptions as implicit prompt instructions — a field described as "The user's sentiment as positive, negative, or neutral" produces better results than a bare sentiment: str with no context.

  5. Set the system prompt to reinforce structure. Tell the model its job is data extraction, not conversation. Example: "You are a data extraction system. Analyze the input and return the requested fields. Do not include explanations outside the JSON structure."

  6. If using OpenAI's json_schema mode, set "strict": true in the schema definition. This activates constrained decoding where the model can only output tokens that conform to the schema. Without strict: true, the model may still produce invalid JSON.

  7. If using Anthropic's tool_use approach, extract the structured data from response.content by finding the block where type == "tool_use" and reading its input field. Do not parse the text blocks — the structured data lives exclusively in the tool_use block.

  8. Validate the response against the schema in your application code. Even with constrained decoding, validate with Pydantic's model_validate() or Zod's .parse() before passing data downstream. This catches semantic issues (empty strings, out-of-range numbers) that schema conformance alone cannot prevent.

…

Questions fréquentes

Pourquoi l'IA renvoie-t-elle parfois un JSON invalide ?

Cela peut arriver si l'IA n'a pas reçu les bonnes instructions sur le format, ou si le fournisseur d'IA ne dispose pas d'un mécanisme fiable pour forcer la sortie à respecter le schéma. Cette compétence explique comment utiliser ces mécanismes.

Qu'est-ce qu'un « schéma » ?

Un schéma est comme un moule ou un plan qui définit la structure des données attendues : quels champs doivent exister, quel type d'information chaque champ contient (texte, nombre, etc.) et s'ils sont obligatoires ou facultatifs.

Cela fonctionne-t-il avec n'importe quelle IA ?

Cette compétence se concentre sur la façon de procéder avec les API des principaux fournisseurs, comme OpenAI, Anthropic et Google. Pour les modèles exécutés localement, des techniques similaires existent également.

Détails

Description originale de l’auteur

Get reliable JSON, enums, and typed objects from LLMs using response_format, tool_use, and schema-constrained decoding across OpenAI, Anthropic, and Google APIs.

repo: sickn33/agentic-awesome-skills

Popularité

Étoiles GitHub
48k
Position au classement
#5091
Licence
MIT
Mis à jour
15/10/2026

Ce skill vous est-il utile ?

0

Connectez-vous pour voter.

Commentaires

  • …

Autres skills de ce dépôt

Skills similaires