Learn how to use JSON mode to get structured outputs from LLMs including Llama 3.1.
response_format
parameter to the Chat Completions API with {"type": "json_object"}
. The JSON Schema can be specified with the schema property of response_format
meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo
meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo
mistralai/Mixtral-8x7B-Instruct-v0.1
mistralai/Mistral-7B-Instruct-v0.1
togethercomputer/CodeLlama-34b-Instruct
When using JSON mode, always instruct the model to produce JSON, either in a system or user message. This is very important so that it only responds in JSON, along with providing the response_format
parameter.
With JSON mode, you can specify a schema for the output of the LLM. In Python, we’ll do this with Pydantic and in TypeScript, we’ll do this with Zod. Here’s an example of JSON mode with Python using Llama 3.1.