Agent skill
structured-outputs
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/structured-outputs
SKILL.md
Structured Outputs
Overview
Structured Outputs ensure that an LLM's response always conforms to a predefined JSON schema. This moves beyond simple prompt engineering to guaranteed syntactic and structural correctness, essential for programmatic integration.
When to Use
- API Integration: When LLM output must be parsed by a machine (e.g., generating database records).
- UI Components: When the LLM generates data to populate a specific frontend interface.
- Data Extraction: Converting unstructured text into standardized JSON objects.
Decision Tree
- Do you need 100% guarantee of schema adherence?
- YES: Use Structured Output mode (e.g., OpenAI's
strict: trueor Gemini'sresponse_mime_type). - NO: JSON Mode may suffice.
- YES: Use Structured Output mode (e.g., OpenAI's
- Is the structure complex or nested?
- YES: Use Pydantic models and
model_rebuildif recursive.
- YES: Use Pydantic models and
- Do you need to handle safety refusals programmatically?
- YES: Check for the
refusalfield in the response.
- YES: Check for the
Workflows
1. Defining a Pydantic-based Schema
- Define a Python class inheriting from
BaseModel. - Use specific types (e.g.,
int,List[str]) andFielddescriptions to guide the model. - Pass the model directly to the SDK's parse method (e.g.,
client.beta.chat.completions.parse). - Access the parsed result via
response.choices[0].message.parsed.
2. Handling Optional and Nullable Fields
- In the JSON schema, define types as an array:
["string", "null"]. - In Pydantic, use
Optional[str] = None. - Instruct the model in the field
descriptionwhen to use null vs. an empty value.
3. Streaming Structured JSON
- Initiate a
generate_content_streamcall with the JSON schema configuration. - Iterate over chunks as they arrive from the model.
- Concatenate the partial JSON strings; note that the full object is only valid JSON once the stream finishes.
Non-Obvious Insights
- Order Matters: Models typically produce outputs in the same order as the keys defined in the schema.
- Refusals as First-Class Citizens: Safety-based model refusals are now programmatically detectable as a separate field, preventing the parser from failing on malformed JSON when the model won't answer.
- Beyond JSON Mode: Structured Outputs guarantee schema adherence, whereas JSON Mode only guarantees valid JSON syntax without specific structure enforcement.
Evidence
- "Structured Outputs is a feature that ensures the model will always generate responses that adhere to your supplied JSON Schema." - OpenAI
- "The model will produce outputs in the same order as the keys in the schema." - Google AI
- "Safety-based model refusals are now programmatically detectable." - OpenAI
Scripts
scripts/structured-outputs_tool.py: Pydantic model definition and parsing logic.scripts/structured-outputs_tool.js: Equivalent JSON schema definition for Node.js.
Dependencies
pydanticopenai >= 1.40.0orgoogle-generativeai
References
- references/README.md
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
prompt-engineering
Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.
adk-rag-agent
Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.
headless-cli-agents
Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.
notion-knowledge-capture
Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
gh-fix-ci
Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.
Didn't find tool you were looking for?