YamlOutputParser Class — langchain Architecture
Architecture documentation for the YamlOutputParser class in yaml.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD ace14fd9_5f64_6948_e02f_713d598d0b6f["YamlOutputParser"] ddb3c915_19e4_cf5f_d10f_fead86b24a26["yaml.py"] ace14fd9_5f64_6948_e02f_713d598d0b6f -->|defined in| ddb3c915_19e4_cf5f_d10f_fead86b24a26 77eefb4e_ef8f_219b_b461_97fe5badd155["parse()"] ace14fd9_5f64_6948_e02f_713d598d0b6f -->|method| 77eefb4e_ef8f_219b_b461_97fe5badd155 088d2991_fd82_beab_10cb_087a70f2cfa9["get_format_instructions()"] ace14fd9_5f64_6948_e02f_713d598d0b6f -->|method| 088d2991_fd82_beab_10cb_087a70f2cfa9 36bf9afe_358d_162b_d93d_ad7399b3834f["_type()"] ace14fd9_5f64_6948_e02f_713d598d0b6f -->|method| 36bf9afe_358d_162b_d93d_ad7399b3834f aa221a16_49e5_95dc_1d76_7e75abf97012["OutputType()"] ace14fd9_5f64_6948_e02f_713d598d0b6f -->|method| aa221a16_49e5_95dc_1d76_7e75abf97012
Relationship Graph
Source Code
libs/langchain/langchain_classic/output_parsers/yaml.py lines 18–69
class YamlOutputParser(BaseOutputParser[T]):
"""Parse YAML output using a Pydantic model."""
pydantic_object: type[T]
"""The Pydantic model to parse."""
pattern: re.Pattern = re.compile(
r"^```(?:ya?ml)?(?P<yaml>[^`]*)",
re.MULTILINE | re.DOTALL,
)
"""Regex pattern to match yaml code blocks
within triple backticks with optional yaml or yml prefix."""
@override
def parse(self, text: str) -> T:
try:
# Greedy search for 1st yaml candidate.
match = re.search(self.pattern, text.strip())
# If no backticks were present, try to parse the entire output as yaml.
yaml_str = match.group("yaml") if match else text
json_object = yaml.safe_load(yaml_str)
return self.pydantic_object.model_validate(json_object)
except (yaml.YAMLError, ValidationError) as e:
name = self.pydantic_object.__name__
msg = f"Failed to parse {name} from completion {text}. Got: {e}"
raise OutputParserException(msg, llm_output=text) from e
@override
def get_format_instructions(self) -> str:
# Copy schema to avoid altering original Pydantic schema.
schema = dict(self.pydantic_object.model_json_schema().items())
# Remove extraneous fields.
reduced_schema = schema
if "title" in reduced_schema:
del reduced_schema["title"]
if "type" in reduced_schema:
del reduced_schema["type"]
# Ensure yaml in context is well-formed with double quotes.
schema_str = json.dumps(reduced_schema)
return YAML_FORMAT_INSTRUCTIONS.format(schema=schema_str)
@property
def _type(self) -> str:
return "yaml"
@property
@override
def OutputType(self) -> type[T]:
return self.pydantic_object
Domain
Source
Frequently Asked Questions
What is the YamlOutputParser class?
YamlOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/output_parsers/yaml.py.
Where is YamlOutputParser defined?
YamlOutputParser is defined in libs/langchain/langchain_classic/output_parsers/yaml.py at line 18.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free