Prompt Engineering for Developers: System Prompts, JSON Schemas, & Function Calling
Build robust, production-grade LLM applications that never break your JSON parsers.
Marcus Chen
Staff Software Architect
The Fallacy of Markdown JSON
OpenAI & Anthropic Structured Outputs
💡 End-to-end type-safe structured output validation using Zod
import OpenAI from 'openai'
import { z } from 'zod'
import { zodResponseFormat } from 'openai/helpers/zod'
const PromptAnalysisSchema = z.object({
sentiment: z.enum(['positive', 'neutral', 'negative']),
keywords: z.array(z.string()).max(5),
complexityScore: z.number().min(1).max(10),
recommendations: z.array(z.string()),
})
const openai = new OpenAI()
export async function analyzePrompt(input: string) {
const completion = await openai.beta.chat.completions.parse({
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'You are a strict data extraction engine.' },
{ role: 'user', content: input },
],
response_format: zodResponseFormat(PromptAnalysisSchema, 'prompt_analysis'),
})
return completion.choices[0].message.parsed
}The Anatomy of a Robust System Prompt
Defense-in-Depth for Malicious Inputs
Validating with Zod & Pydantic
Everything You Need to Know
What is the difference between JSON Mode and Structured Outputs?
JSON Mode guarantees valid JSON syntax (no missing commas or quotes), but does NOT guarantee specific fields or types. Structured Outputs guarantees 100% adherence to your specific schema keys, data types, and enum values.
Can structured outputs slow down response latency?
The first request incurs a small latency penalty while the schema is compiled into a grammar prefix tree, but subsequent requests run at native speed.
Marcus Chen
Staff Software Architect at Prompt4life
Specializing in multi-model prompt architectures, latent diffusion guidance, and production AI workflows. Contributing regularly to the Prompt4life open engineering knowledge base.
More Masterclasses & Guides
The Ultimate Midjourney v6 Prompting Guide: Syntax, Parameters, & Photorealism
Learn how to write photorealistic Midjourney v6 prompts with full parameter breakdowns, aspect ratios, stylize values, multi-prompting weights, and photographic terminology.
Mastering Chain-of-Thought (CoT) Prompting with ChatGPT, Claude, & Gemini
Learn the principles of Zero-Shot CoT, Few-Shot CoT, and Tree-of-Thought frameworks to dramatically improve accuracy in complex reasoning, coding, and strategic problem solving.
Negative Prompts Explained: Eliminating Artifacts in Stable Diffusion & Midjourney
Learn how negative prompting works under the hood in latent diffusion models. Discover the best negative prompt keywords to prevent distorted hands, blurry textures, and messy anatomy.