Skip to content

The agent engineering platform.

Add unit test to output parsers (#3911)

Romad15f48135255
diff --git a/tests/unit_tests/output_parsers/test_boolean_parser.py b/tests/unit_tests/output_parsers/test_boolean_parser.py
new file mode 100644
index 0000000000..3daaf61062
--- /dev/null
+++ b/tests/unit_tests/output_parsers/test_boolean_parser.py
@@ -0,0 +1,20 @@
+from langchain.output_parsers.boolean import BooleanOutputParser
+
+
+def test_boolean_output_parser_parse() -> None:
+ parser = BooleanOutputParser()
+
+ # Test valid input
+ result = parser.parse("YES")
+ assert result is True
+
+ # Test valid input
+ result = parser.parse("NO")
+ assert result is False
+
+ # Test invalid input
+ try:
+ parser.parse("INVALID")
+ assert False, "Should have raised ValueError"
+ except ValueError:
+ pass
diff --git a/tests/unit_tests/output_parsers/test_list_parser.py b/tests/unit_tests/output_parsers/test_list_parser.py
new file mode 100644
index 0000000000..84be4db946
--- /dev/null
+++ b/tests/unit_tests/output_parsers/test_list_parser.py
@@ -0,0 +1,13 @@
+from langchain.output_parsers.list import CommaSeparatedListOutputParser
+
+
+def test_single_item() -> None:
+ """Test that a string with a single item is parsed to a list with that item."""
+ parser = CommaSeparatedListOutputParser()
+ assert parser.parse("foo") == ["foo"]
+
+
+def test_multiple_items() -> None:
+ """Test that a string with multiple comma-separated items is parsed to a list."""
+ parser = CommaSeparatedListOutputParser()
+ assert parser.parse("foo, bar, baz") == ["foo", "bar", "baz"]
diff --git a/tests/unit_tests/output_parsers/test_structured_parser.py b/tests/unit_tests/output_parsers/test_structured_parser.py
new file mode 100644
index 0000000000..abf19a7c09
--- /dev/null
+++ b/tests/unit_tests/output_parsers/test_structured_parser.py
@@ -0,0 +1,25 @@
+from langchain.output_parsers import ResponseSchema, StructuredOutputParser
+from langchain.schema import OutputParserException
+
+
+def test_parse() -> None:
+ 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:
+ assert False, f"Expected OutputParserException, but got {parser.parse(text)}"