Enforce and Validate LLM Output with Pydantic

Large Language Models (LLMs) excel in generating text but often struggle to produce structured output. By leveraging Pydantic’s type validation and prompt engineering, we can enforce and validate the output generated by LLMs. All code examples in this blog post are written in Python. The LLM used is OpenAI’s gpt-3.5-turbo. Query the LLM To query the LLM, we use the following function: import openai def query(prompt: str) -> str: """Query the LLM with the given prompt....

August 7, 2023 · 5 min · 891 words