How to Use JsonOutputParser in Langchain: Syntax and Example
Use
JsonOutputParser in Langchain to parse JSON-formatted text outputs from language models into Python objects. Instantiate it with an output schema, then call parse on the model's output string to get structured data.Syntax
The JsonOutputParser is created by passing a JSON schema or output schema that defines the expected structure. You then use its parse method to convert a JSON string output from a language model into a Python dictionary or object.
Key parts:
output_schema: Defines the expected JSON structure.parse(output_str): Parses the JSON stringoutput_strinto a Python object.
python
from langchain.output_parsers import JsonOutputParser # Define your output schema (example: a dict with 'name' and 'age') output_schema = { "name": "string", "age": "integer" } # Create the parser parser = JsonOutputParser(output_schema=output_schema) # Parse a JSON string output from a model result = parser.parse('{"name": "Alice", "age": 30}') print(result)
Output
{'name': 'Alice', 'age': 30}
Example
This example shows how to use JsonOutputParser with a simple output schema to parse a JSON string returned by a language model. It demonstrates converting the JSON string into a Python dictionary with the expected keys and types.
python
from langchain.output_parsers import JsonOutputParser # Define the expected output schema output_schema = { "title": "string", "year": "integer", "genres": "list" } # Instantiate the parser parser = JsonOutputParser(output_schema=output_schema) # Simulated model output as JSON string model_output = '{"title": "Inception", "year": 2010, "genres": ["Action", "Sci-Fi"]}' # Parse the output parsed_output = parser.parse(model_output) print(parsed_output)
Output
{'title': 'Inception', 'year': 2010, 'genres': ['Action', 'Sci-Fi']}
Common Pitfalls
Common mistakes when using JsonOutputParser include:
- Passing an output string that is not valid JSON, causing parsing errors.
- Defining an output schema that does not match the actual JSON structure, leading to incorrect parsing or runtime errors.
- Not handling exceptions when parsing fails.
Always validate the JSON format and ensure the schema matches the expected output.
python
from langchain.output_parsers import JsonOutputParser output_schema = {"name": "string", "age": "integer"} parser = JsonOutputParser(output_schema=output_schema) # Wrong: invalid JSON string (missing quotes) invalid_json = '{name: Alice, age: 30}' try: parser.parse(invalid_json) except Exception as e: print(f"Parsing failed: {e}") # Right: valid JSON string valid_json = '{"name": "Alice", "age": 30}' result = parser.parse(valid_json) print(result)
Output
Parsing failed: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
{'name': 'Alice', 'age': 30}
Quick Reference
Tips for using JsonOutputParser effectively:
- Always define a clear output schema matching your expected JSON structure.
- Ensure the model output is valid JSON before parsing.
- Use try-except blocks to handle parsing errors gracefully.
- Use
parsemethod to convert JSON strings to Python objects.
Key Takeaways
JsonOutputParser converts JSON strings from language models into Python objects using a defined schema.
Always provide a matching output schema and valid JSON strings to avoid parsing errors.
Use try-except to handle invalid JSON or schema mismatches gracefully.
The parse method is the main way to get structured data from JSON output.
Validate your model's output format before parsing to ensure smooth operation.