News for hackers. Stuff that matters.
Re:Show HN: Using zod to get structured and typed output fro... by jbwyme on Friday April 14, 2023 @12:06AM (#35565396)
Nice! I was just minutes ago writing a very similar pattern with zod and GPT. Thanks for validating my approach :)
Re:Show HN: Using zod to get structured and typed output fro... by teetertater on Friday April 14, 2023 @05:08AM (#35567202)
Cool approach! The only thing I would change or allow customization for is the prompt you're injecting. Most users will probably want to change this:

"Output a json object or array generated with random content fitting this schema, based on the PORMPT section below." (sic)

My findings from one week of forcing chatgpt to produce structured output:

- it prefers a certain kind of format (the one it uses by default when you ask it without json/markdown), and if you deviate too far, it won't listen. E.g. I tried adding underscores to certain words, which it only did half the time.

- regex parsing the output is quite robust. Even if it adds sections you don't want like intro or conclusion, the parser will just discard these

Re:Show HN: Using zod to get structured and typed output fro... by olup on Friday April 14, 2023 @03:14PM (#35573514)
Thanks ! if you feel like making a PR i'll be happy to merge it !
Re:Show HN: Using zod to get structured and typed output fro... by olup on Friday April 14, 2023 @03:27PM (#35573622)
added the customization suggestion (task and chatCompletion ptions override).
Re:Show HN: Using zod to get structured and typed output fro... by leobg on Friday April 14, 2023 @05:09AM (#35567206)
Typo in the prompt:

> based on the PORMPT

Re:Show HN: Using zod to get structured and typed output fro... by jondwillis on Friday April 14, 2023 @10:12AM (#35569787)
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.
Re:Show HN: Using zod to get structured and typed output fro... by olup on Friday April 14, 2023 @03:18PM (#35573545)
Another idea would be to feed zod's validation errors, if any, to the llm to let it have a second try automatically