Home / File/ test_output_parsers.py — langchain Source File

test_output_parsers.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 5 imports 5 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  16bf8cf5_92a2_117b_1919_921f5de1d396["test_output_parsers.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  16bf8cf5_92a2_117b_1919_921f5de1d396 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  16bf8cf5_92a2_117b_1919_921f5de1d396 --> d758344f_537f_649e_f467_b9d7442e86df
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  16bf8cf5_92a2_117b_1919_921f5de1d396 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  16bf8cf5_92a2_117b_1919_921f5de1d396 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  32316456_be3b_4885_5b73_c95be1fe112d["langchain_anthropic.output_parsers"]
  16bf8cf5_92a2_117b_1919_921f5de1d396 --> 32316456_be3b_4885_5b73_c95be1fe112d
  style 16bf8cf5_92a2_117b_1919_921f5de1d396 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Any, Literal

from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration
from pydantic import BaseModel

from langchain_anthropic.output_parsers import ToolsOutputParser

_CONTENT: list = [
    {
        "type": "text",
        "text": "thought",
    },
    {"type": "tool_use", "input": {"bar": 0}, "id": "1", "name": "_Foo1"},
    {
        "type": "text",
        "text": "thought",
    },
    {"type": "tool_use", "input": {"baz": "a"}, "id": "2", "name": "_Foo2"},
]

_RESULT: list = [ChatGeneration(message=AIMessage(_CONTENT))]  # type: ignore[misc]


class _Foo1(BaseModel):
    bar: int


class _Foo2(BaseModel):
    baz: Literal["a", "b"]


def test_tools_output_parser() -> None:
    output_parser = ToolsOutputParser()
    expected = [
        {
            "name": "_Foo1",
            "args": {"bar": 0},
            "id": "1",
            "index": 1,
            "type": "tool_call",
        },
        {
            "name": "_Foo2",
            "args": {"baz": "a"},
            "id": "2",
            "index": 3,
            "type": "tool_call",
        },
    ]
    actual = output_parser.parse_result(_RESULT)
    assert expected == actual


def test_tools_output_parser_args_only() -> None:
    output_parser = ToolsOutputParser(args_only=True)
    expected = [
        {"bar": 0},
        {"baz": "a"},
    ]
    actual = output_parser.parse_result(_RESULT)
    assert expected == actual

    expected = []
    actual = output_parser.parse_result([ChatGeneration(message=AIMessage(""))])  # type: ignore[misc]
    assert expected == actual


def test_tools_output_parser_first_tool_only() -> None:
    output_parser = ToolsOutputParser(first_tool_only=True)
    expected: Any = {
        "name": "_Foo1",
        "args": {"bar": 0},
        "id": "1",
        "index": 1,
        "type": "tool_call",
    }
    actual = output_parser.parse_result(_RESULT)
    assert expected == actual

    expected = None
    actual = output_parser.parse_result([ChatGeneration(message=AIMessage(""))])  # type: ignore[misc]
    assert expected == actual


def test_tools_output_parser_pydantic() -> None:
    output_parser = ToolsOutputParser(pydantic_schemas=[_Foo1, _Foo2])
    expected = [_Foo1(bar=0), _Foo2(baz="a")]
    actual = output_parser.parse_result(_RESULT)
    assert expected == actual


def test_tools_output_parser_empty_content() -> None:
    class ChartType(BaseModel):
        chart_type: Literal["pie", "line", "bar"]

    output_parser = ToolsOutputParser(
        first_tool_only=True,
        pydantic_schemas=[ChartType],
    )
    message = AIMessage(
        "",
        tool_calls=[
            {
                "name": "ChartType",
                "args": {"chart_type": "pie"},
                "id": "foo",
                "type": "tool_call",
            },
        ],
    )
    actual = output_parser.invoke(message)
    expected = ChartType(chart_type="pie")
    assert expected == actual

Subdomains

Dependencies

  • langchain_anthropic.output_parsers
  • langchain_core.messages
  • langchain_core.outputs
  • pydantic
  • typing

Frequently Asked Questions

What does test_output_parsers.py do?
test_output_parsers.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_output_parsers.py?
test_output_parsers.py defines 5 function(s): test_tools_output_parser, test_tools_output_parser_args_only, test_tools_output_parser_empty_content, test_tools_output_parser_first_tool_only, test_tools_output_parser_pydantic.
What does test_output_parsers.py depend on?
test_output_parsers.py imports 5 module(s): langchain_anthropic.output_parsers, langchain_core.messages, langchain_core.outputs, pydantic, typing.
Where is test_output_parsers.py in the architecture?
test_output_parsers.py is located at libs/partners/anthropic/tests/unit_tests/test_output_parsers.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/partners/anthropic/tests/unit_tests).

Analyze Your Own Codebase

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

Try Supermodel Free