TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Using zod to get structured and typed output from ChatGPT in TypeScript

35 pointsby olupabout 2 years ago

5 comments

teetertaterabout 2 years ago
Cool approach! The only thing I would change or allow customization for is the prompt you&#x27;re injecting. Most users will probably want to change this:<p>&quot;Output a json object or array generated with random content fitting this schema, based on the PORMPT section below.&quot; (sic)<p>My findings from one week of forcing chatgpt to produce structured output:<p>- it prefers a certain kind of format (the one it uses by default when you ask it without json&#x2F;markdown), and if you deviate too far, it won&#x27;t listen. E.g. I tried adding underscores to certain words, which it only did half the time.<p>- regex parsing the output is quite robust. Even if it adds sections you don&#x27;t want like intro or conclusion, the parser will just discard these
评论 #35573514 未加载
评论 #35573622 未加载
jondwillisabout 2 years ago
I started porting Auto-GPT to NextJS for fun yesterday. The python implementation requires output to be a predictable json schema, so this might be useful in the TypeScript version.
olupabout 2 years ago
Another idea would be to feed zod&#x27;s validation errors, if any, to the llm to let it have a second try automatically
jbwymeabout 2 years ago
Nice! I was just minutes ago writing a very similar pattern with zod and GPT. Thanks for validating my approach :)
leobgabout 2 years ago
Typo in the prompt:<p>&gt; based on the PORMPT