SShortSingh.
Back to feed

How a Single Zod Schema Can Validate, Type, and Repair AI Model Outputs

0
·1 views

A common failure point in AI-powered applications is the gap between a raw string returned by a model and the structured object a UI component expects. Using a single Zod schema can address this by serving simultaneously as a runtime validator, a compile-time TypeScript type, a JSON Schema constraint sent to the model, and a source of structured error messages for repair prompts. Zod 4 includes a built-in z.toJSONSchema() method, while Zod 3 users must rely on the separate zod-to-json-schema package, making version awareness important. When constrained decoding against a schema is supported by a provider, the article recommends using it to make malformed JSON structurally impossible rather than just unlikely. For cases where strict output constraints are unavailable, the approach distinguishes between two distinct failure modes — non-JSON responses and schema mismatches — so that repair instructions sent back to the model are accurate and targeted.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Developer Builds Filipino Recipe App That Suggests Dishes Based on Weather

A developer has created Lutong Simmer, a native Android app that recommends Filipino dishes by factoring in current weather conditions and time of day. For example, the app may suggest Sinigang on a rainy afternoon or Halo-Halo during hot weather. Built with Kotlin using an MVVM architecture, the app works offline through local Room SQLite storage, ensuring recipes remain accessible without an internet connection. It also features a 'Kitchen Studio' section where users can create, save, and track their own custom Filipino recipes with step-by-step checklists. The interface is designed with a Filipino cultural identity in mind, incorporating a terracotta-inspired color palette and localized greetings.

0
ProgrammingDEV Community ·

How Staff Engineers Solve Cache Stampede: The 40,000-Request Database Problem

A cache stampede, or 'thundering herd,' occurs when a popular cache key expires and thousands of simultaneous requests bypass the cache and hammer the database with identical queries. The core issue is duplication, not volume — during a 200ms cache rebuild window, thousands of requests each independently trigger the same database query. Engineers can address this using either a distributed lock, where only the first request rebuilds the cache while others wait, or a stale-while-revalidate strategy, which serves slightly outdated data instantly while refreshing in the background. The right choice depends on the product context — account balances demand fresh data via locking, while product listings can tolerate brief staleness. A further safeguard is adding random jitter to cache TTLs at write time, preventing mass simultaneous expiries when many keys are set together.

0
ProgrammingDEV Community ·

Wotchi Adds Lightweight Error Alerting to Express APIs Without Extra Dashboards

A developer has released Wotchi, an open-source, in-process alerting library for Node.js services built to reduce noisy error notifications in Express APIs. The tool sits as middleware between Express routes and the final error handler, normalizing, redacting sensitive data, and grouping repeated failures before dispatching alerts. Notifications can be sent to the console, a Telegram channel, or any HTTPS webhook, requiring no hosted observability platform or new account. Wotchi supports Node.js 18.18.0 and above, works with Express 4 and 5, and is compatible with ESM, CommonJS, and TypeScript. The library is currently in public beta at version 0.1.0-beta.6, meaning its API may still change before a stable release.

0
ProgrammingDEV Community ·

Studies Show AI Citation Patterns in SaaS Vary by Platform, Prompt, and Buyer Stage

Multiple research efforts examining how AI systems like ChatGPT select sources for B2B SaaS buyer queries are revealing consistent but nuanced patterns. Analyses linked to researcher Kevin Indig and a separate BeVisibleIQ study of 75 SaaS buyer prompts across four AI engines both indicate that citations tend to concentrate among a relatively small group of domains. The research also shows that which sources get cited — whether vendor websites or third-party content — shifts depending on the AI platform used and the buyer's stage in the decision journey. Experts caution that metrics like unique cited domains and citation volume measure different things, making cross-study comparisons unreliable if methodology differences are ignored. The findings collectively suggest that earning AI visibility is not as simple as publishing more content, but depends on the interplay of query context, platform, and source material.

How a Single Zod Schema Can Validate, Type, and Repair AI Model Outputs · ShortSingh