JsonOutputParser

Open in ColabOpen in GitHub

Overview

This tutorial covers the implementation of the JsonOutputParser.JsonOutputParser is a tool that allows users to specify the desired JSON schema. It is designed to enable an LLM(Large Language Model) to query data and return results in JSON format that adheres to the specified schema. To ensure that the LLM processes data accurately and efficiently, generating JSON in the desired format, the model must have sufficient capacity (e.g., intelligence). For instance, the llama-70B model has a larger capacity compared to the llama-8B model, making it more suitable for handling complex data.

[Note]

JSON (JavaScript Object Notation) is a lightweight data interchange format used for storing and structuring data. It plays a crucial role in web development and is widely used for communication between servers and clients. JSON is based on text that is easy to read and simple for machines to parse and generate.

JSON data consists of key-value pairs. Here, the "key" is a string, and the "value" can be various data types. JSON has two primary structures:

  • Object: A collection of key-value pairs enclosed in curly braces { }. Each key is associated with its value using a colon ( : ), and multiple key-value pairs are separated by commas ( , ).

  • Array: An ordered list of values enclosed in square brackets [ ]. Values within an array are separated by commas ( , ).

{
  "name": "John Doe",
  "age": 30,
  "is_student": false,
  "skills": ["Java", "Python", "JavaScript"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown"
  }
}

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set OPENAI_API_KEY in .env file and load it. [Note] This is not necessary if your've already set OPENAI_API_KEY in previous steps.

Using JsonOutputParser with Pydantic

If you need to generate output in JSON format, you can easily implement it using LangChain's JsonOutputParser. There are 2 ways to generate output in JSON format:

  • Using Pydantic

  • Not using Pydantic

Follow the steps below to implement it:

Start by importing the necessary modules.

Define the output data schema format.

Set up the parser using JsonOutputParser and inject instructions into the prompt template.

Using JsonOutputParser without Pydantic

You can generate output in JSON format without Pydantic.

Follow the steps below to implement it:

Last updated