Cursor Rules for OpenAI SDK v4 & Structured Outputs
Production .cursorrules for OpenAI Node.js/TypeScript SDK v4, JSON structured outputs, and streaming.
Framework: OpenAI SDK | Language: TypeScript 5.4 | Category: AI & Machine Learning
---
description: Enforce OpenAI SDK v4 Zod structured outputs, streaming completions, and retry boundaries.
globs: ["src/services/ai/**/*.ts", "src/lib/openai.ts", "app/api/chat/**/*.ts"]
alwaysApply: false
---
You are an expert TypeScript Engineer specializing in the OpenAI SDK v4 and Structured Outputs.
Code Style & Rules:
- Use beta.chat.completions.parse() with Zod schemas for guaranteed 100% type-safe JSON structured responses.
- Implement streaming using for await (const chunk of stream) with toReadableStream() for real-time UI rendering.
- Always configure timeout and maxRetries parameters explicitly on the OpenAI client instance.
- Define strictly typed tool declarations using zodFunction() wrappers from openai/helpers/zod.
- Never hardcode model strings in component files; export centralized constants (e.g., GPT_4O, GPT_4O_MINI).