Skip to main content

Enum parser

This notebook shows how to use an Enum output parser.

from langchain.output_parsers.enum import EnumOutputParser
from enum import Enum


class Colors(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
parser = EnumOutputParser(enum=Colors)
parser.parse("red")
<Colors.RED: 'red'>
# Can handle spaces
parser.parse(" green")
<Colors.GREEN: 'green'>
# And new lines
parser.parse("blue\n")
<Colors.BLUE: 'blue'>
# And raises errors when appropriate
parser.parse("yellow")
OutputParserException: Response 'yellow' is not one of the expected values: ['red', 'green', 'blue']