Home / File/ test_structured_parser.py — langchain Source File

test_structured_parser.py — langchain Source File

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

File python CoreAbstractions Serialization 3 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  6749a520_576d_e122_fae8_32694fd87fe2["test_structured_parser.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  6749a520_576d_e122_fae8_32694fd87fe2 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  6749a520_576d_e122_fae8_32694fd87fe2 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  48ed1a89_a8c1_890c_8db6_cafc60317e2f["langchain_classic.output_parsers"]
  6749a520_576d_e122_fae8_32694fd87fe2 --> 48ed1a89_a8c1_890c_8db6_cafc60317e2f
  style 6749a520_576d_e122_fae8_32694fd87fe2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Any

from langchain_core.exceptions import OutputParserException

from langchain_classic.output_parsers import ResponseSchema, StructuredOutputParser


def test_parse() -> None:
    """Test parsing structured output."""
    response_schemas = [
        ResponseSchema(name="name", description="desc"),
        ResponseSchema(name="age", description="desc"),
    ]
    parser = StructuredOutputParser.from_response_schemas(response_schemas)

    # Test valid JSON input
    text = '```json\n{"name": "John", "age": 30}\n```'
    expected_result = {"name": "John", "age": 30}
    result = parser.parse(text)
    assert result == expected_result, f"Expected {expected_result}, but got {result}"

    # Test invalid JSON input
    text = '```json\n{"name": "John"}\n```'
    try:
        parser.parse(text)
    except OutputParserException:
        pass  # Test passes if OutputParserException is raised
    else:
        msg = f"Expected OutputParserException, but got {parser.parse(text)}"
        raise AssertionError(msg)


def test_output_type() -> None:
    """Test the output type of the structured output parser is Dict[str, Any]."""
    response_schemas = [
        ResponseSchema(name="name", description="desc"),
        ResponseSchema(name="age", description="desc"),
    ]
    parser = StructuredOutputParser.from_response_schemas(response_schemas)
    assert parser.OutputType == dict[str, Any]

Subdomains

Dependencies

  • langchain_classic.output_parsers
  • langchain_core.exceptions
  • typing

Frequently Asked Questions

What does test_structured_parser.py do?
test_structured_parser.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in test_structured_parser.py?
test_structured_parser.py defines 2 function(s): test_output_type, test_parse.
What does test_structured_parser.py depend on?
test_structured_parser.py imports 3 module(s): langchain_classic.output_parsers, langchain_core.exceptions, typing.
Where is test_structured_parser.py in the architecture?
test_structured_parser.py is located at libs/langchain/tests/unit_tests/output_parsers/test_structured_parser.py (domain: CoreAbstractions, subdomain: Serialization, 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