DEV Community

Discussion on: Why Asking an LLM for JSON Isn’t Enough

Collapse
 
trinhcuong-ast profile image
Kai Alder

One thing I've been dealing with on the JS/TS side — if you're not using Python and Pydantic, Zod + the OpenAI SDK's zodResponseFormat helper is a game changer for this exact problem. You define your schema once in Zod, pass it as the response format, and get typed, validated output back. No more writing JSON schemas by hand.

The part about prompt injection affecting structured output is something I don't see enough people talk about. Even with json_schema mode, the content of the values can still be manipulated by injection — the schema just ensures the shape is right. So you still need to sanitize/validate the actual values in your business logic.

Curious — have you run into issues with the strict schema mode and optional fields? I found that handling nullable vs missing fields gets tricky when additionalProperties: false is set.

Collapse
 
dev-in-progress profile image
Vaishali

Thanks for sharing this — the Zod + OpenAI SDK approach sounds really useful for the JS/TS side.

I’m currently exploring the JS/TS ecosystem around this and planning a small project to experiment with structured outputs more deeply. I haven’t hit the strict schema + optional field edge cases yet, but I’ll definitely watch for that as I build it.

Really appreciate you pointing that out — I’ll share what I learn once I’ve had a chance to experiment with it more.