LangChain 02 - JsonOutputParser and Streaming JSON Data Processing Guide

This article mainly explains how to use JsonOutputParser in LangChain and streaming JSON data processing.

1. Install Dependencies

pip install --upgrade --quiet langchain-core langchain-community langchain-openai

2. JsonOutputParser Features

  • Basic Functions: JSON string parsing, data type conversion, error handling
  • Usage Scenarios: API response processing, configuration file reading, data exchange

3. Practical Code

Using async method to stream JSON data:

from langchain_core.output_parsers import JsonOutputParser
from langchain_openai.chat_models import ChatOpenAI

async def main():
    model = ChatOpenAI(model="gpt-3.5-turbo")
    chain = model | JsonOutputParser()
    async for text in chain.astream('要求模型输出JSON格式数据'):
        print(text, flush=True)

Streaming output will progressively show the JSON building process, gradually filling the complete data from an empty object.

4. Extract Specific Fields from JSON

Define function to extract required data from streaming JSON, link to chain through pipe operator:

def _extract_country_names(inputs):
    if not isinstance(inputs, dict):
        return ""
    if "countries" not in inputs:
        return ""
    countries = inputs["countries"]
    country_names = [country.get("name") for country in countries if isinstance(country, dict)]
    return country_names