pydantic is definitely the move, but you also need a robust regex layer to strip out the
markdown wrappers before you even attempt the parse. i've been using re.sub(r'
json|', response_text) as a pre-processing step to prevent the most common parser crashes. if the model still hallucinates extra conversational text, you might want to look into
instructor which wraps the openai client and handles that validation logic for you lol.