yaml.py — langchain Source File
Architecture documentation for yaml.py, a python file in the langchain codebase. 9 imports, 1 dependents.
Entity Profile
Dependency Diagram
graph LR ddb3c915_19e4_cf5f_d10f_fead86b24a26["yaml.py"] f47849be_05b7_05b0_91a8_97c1c98d3ae1["json.py"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> f47849be_05b7_05b0_91a8_97c1c98d3ae1 67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 ddb3c915_19e4_cf5f_d10f_fead86b24a26["yaml.py"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> ddb3c915_19e4_cf5f_d10f_fead86b24a26 75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 75137834_4ba7_dc43_7ec5_182c05eceedf 83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b 1f6af6bc_5660_a369_0bfb_4b5c8ed28743["pydantic.py"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 1f6af6bc_5660_a369_0bfb_4b5c8ed28743 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 91721f45_4909_e489_8c1f_084f8bd87145 8a49663d_c016_a6c1_e96e_621fdb612ef3["langchain_classic.output_parsers.format_instructions"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> 8a49663d_c016_a6c1_e96e_621fdb612ef3 ddb3c915_19e4_cf5f_d10f_fead86b24a26["yaml.py"] ddb3c915_19e4_cf5f_d10f_fead86b24a26 --> ddb3c915_19e4_cf5f_d10f_fead86b24a26 style ddb3c915_19e4_cf5f_d10f_fead86b24a26 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import json
import re
from typing import TypeVar
import yaml
from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import BaseOutputParser
from pydantic import BaseModel, ValidationError
from typing_extensions import override
from langchain_classic.output_parsers.format_instructions import (
YAML_FORMAT_INSTRUCTIONS,
)
T = TypeVar("T", bound=BaseModel)
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
Subdomains
Classes
Dependencies
- json.py
- langchain_classic.output_parsers.format_instructions
- langchain_core.exceptions
- langchain_core.output_parsers
- pydantic.py
- re
- typing
- typing_extensions
- yaml.py
Source
Frequently Asked Questions
What does yaml.py do?
yaml.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What does yaml.py depend on?
yaml.py imports 9 module(s): json.py, langchain_classic.output_parsers.format_instructions, langchain_core.exceptions, langchain_core.output_parsers, pydantic.py, re, typing, typing_extensions, and 1 more.
What files import yaml.py?
yaml.py is imported by 1 file(s): yaml.py.
Where is yaml.py in the architecture?
yaml.py is located at libs/langchain/langchain_classic/output_parsers/yaml.py (domain: OutputParsing, subdomain: StreamingParsers, directory: libs/langchain/langchain_classic/output_parsers).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free