Home / File/ test_yaml_parser.py — langchain Source File

test_yaml_parser.py — langchain Source File

Architecture documentation for test_yaml_parser.py, a python file in the langchain codebase. 5 imports, 0 dependents.

File python CoreAbstractions MessageSchema 5 imports 3 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26["test_yaml_parser.py"]
  b188e880_71c6_b93e_127d_c22666293d37["enum"]
  4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26 --> b188e880_71c6_b93e_127d_c22666293d37
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  6385d093_5222_244a_e2e8_c260e67985d0["langchain_classic.output_parsers.yaml"]
  4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26 --> 6385d093_5222_244a_e2e8_c260e67985d0
  style 4dc6ba4b_c1ce_d180_4eae_ef4f7897fe26 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test yamlOutputParser."""

from enum import Enum

import pytest
from langchain_core.exceptions import OutputParserException
from pydantic import BaseModel, Field

from langchain_classic.output_parsers.yaml import YamlOutputParser


class Actions(Enum):
    SEARCH = "Search"
    CREATE = "Create"
    UPDATE = "Update"
    DELETE = "Delete"


class TestModel(BaseModel):
    action: Actions = Field(description="Action to be performed")
    action_input: str = Field(description="Input to be used in the action")
    additional_fields: str | None = Field(
        description="Additional fields",
        default=None,
    )
    for_new_lines: str = Field(description="To be used to test newlines")


# Prevent pytest from trying to run tests on TestModel
TestModel.__test__ = False  # type: ignore[attr-defined]


DEF_RESULT = """```yaml
---

action: Update
action_input: The yamlOutputParser class is powerful
additional_fields: null
for_new_lines: |
  not_escape_newline:
   escape_newline:

```"""
DEF_RESULT_NO_BACKTICKS = """
action: Update
action_input: The yamlOutputParser class is powerful
additional_fields: null
for_new_lines: |
  not_escape_newline:
   escape_newline:

"""

# action 'update' with a lowercase 'u' to test schema validation failure.
DEF_RESULT_FAIL = """```yaml
action: update
action_input: The yamlOutputParser class is powerful
additional_fields: null
```"""

DEF_EXPECTED_RESULT = TestModel(
    action=Actions.UPDATE,
    action_input="The yamlOutputParser class is powerful",
    additional_fields=None,
    for_new_lines="not_escape_newline:\n escape_newline:\n",
)


@pytest.mark.parametrize("result", [DEF_RESULT, DEF_RESULT_NO_BACKTICKS])
def test_yaml_output_parser(result: str) -> None:
    """Test yamlOutputParser."""
    yaml_parser: YamlOutputParser[TestModel] = YamlOutputParser(
        pydantic_object=TestModel,
    )

    model = yaml_parser.parse(result)
    print("parse_result:", result)  # noqa: T201
    assert model == DEF_EXPECTED_RESULT


def test_yaml_output_parser_fail() -> None:
    """Test YamlOutputParser where completion result fails schema validation."""
    yaml_parser: YamlOutputParser[TestModel] = YamlOutputParser(
        pydantic_object=TestModel,
    )

    with pytest.raises(OutputParserException) as exc_info:
        yaml_parser.parse(DEF_RESULT_FAIL)

    assert "Failed to parse TestModel from completion" in str(exc_info.value)


def test_yaml_output_parser_output_type() -> None:
    """Test YamlOutputParser OutputType."""
    yaml_parser = YamlOutputParser[TestModel](pydantic_object=TestModel)
    assert yaml_parser.OutputType is TestModel

Subdomains

Dependencies

  • enum
  • langchain_classic.output_parsers.yaml
  • langchain_core.exceptions
  • pydantic
  • pytest

Frequently Asked Questions

What does test_yaml_parser.py do?
test_yaml_parser.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in test_yaml_parser.py?
test_yaml_parser.py defines 3 function(s): test_yaml_output_parser, test_yaml_output_parser_fail, test_yaml_output_parser_output_type.
What does test_yaml_parser.py depend on?
test_yaml_parser.py imports 5 module(s): enum, langchain_classic.output_parsers.yaml, langchain_core.exceptions, pydantic, pytest.
Where is test_yaml_parser.py in the architecture?
test_yaml_parser.py is located at libs/langchain/tests/unit_tests/output_parsers/test_yaml_parser.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain/tests/unit_tests/output_parsers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free