Home / Function/ test_pydantic_tools_parser_with_optional_fields() — langchain Function Reference

test_pydantic_tools_parser_with_optional_fields() — langchain Function Reference

Architecture documentation for the test_pydantic_tools_parser_with_optional_fields() function in test_openai_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  391c2d59_4deb_34d1_ecd7_77bfc97996d2["test_pydantic_tools_parser_with_optional_fields()"]
  0f94a062_f577_31c7_fb9c_0f526b273e64["test_openai_tools.py"]
  391c2d59_4deb_34d1_ecd7_77bfc97996d2 -->|defined in| 0f94a062_f577_31c7_fb9c_0f526b273e64
  style 391c2d59_4deb_34d1_ecd7_77bfc97996d2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/output_parsers/test_openai_tools.py lines 1193–1353

def test_pydantic_tools_parser_with_optional_fields() -> None:
    """Test PydanticToolsParser with optional fields in v1 and v2 models."""
    if sys.version_info >= (3, 14):
        ProductV1 = pydantic.v1.create_model(  # noqa: N806
            "ProductV1",
            __doc__="Product with optional fields using Pydantic v1.",
            name=(str, ...),
            price=(float, ...),
            description=(str | None, None),
            stock=(int, 0),
        )
    else:

        class ProductV1(pydantic.v1.BaseModel):
            """Product with optional fields using Pydantic v1."""

            name: str
            price: float
            description: str | None = None
            stock: int = 0

    # v2 model with optional fields
    class UserV2(BaseModel):
        """User with optional fields using Pydantic v2."""

        username: str
        email: str
        bio: str | None = None
        age: int | None = None

    # Test v1 with all fields provided
    parser_v1_full = PydanticToolsParser(tools=[ProductV1])
    message_v1_full = AIMessage(
        content="",
        tool_calls=[
            {
                "id": "call_product_full",
                "name": "ProductV1",
                "args": {
                    "name": "Laptop",
                    "price": 999.99,
                    "description": "High-end laptop",
                    "stock": 50,
                },
            }
        ],
    )
    generation_v1_full = ChatGeneration(message=message_v1_full)
    result_v1_full = parser_v1_full.parse_result([generation_v1_full])

    assert len(result_v1_full) == 1
    assert isinstance(result_v1_full[0], ProductV1)
    assert result_v1_full[0].name == "Laptop"  # type: ignore[attr-defined,unused-ignore]
    assert result_v1_full[0].price == 999.99  # type: ignore[attr-defined,unused-ignore]
    assert result_v1_full[0].description == "High-end laptop"  # type: ignore[attr-defined,unused-ignore]
    assert result_v1_full[0].stock == 50  # type: ignore[attr-defined,unused-ignore]

    # Test v1 with only required fields
    parser_v1_minimal = PydanticToolsParser(tools=[ProductV1])
    message_v1_minimal = AIMessage(
        content="",
        tool_calls=[
            {
                "id": "call_product_minimal",
                "name": "ProductV1",
                "args": {"name": "Mouse", "price": 29.99},
            }
        ],
    )
    generation_v1_minimal = ChatGeneration(message=message_v1_minimal)
    result_v1_minimal = parser_v1_minimal.parse_result([generation_v1_minimal])

    assert len(result_v1_minimal) == 1
    assert isinstance(result_v1_minimal[0], ProductV1)
    assert result_v1_minimal[0].name == "Mouse"  # type: ignore[attr-defined,unused-ignore]
    assert result_v1_minimal[0].price == 29.99  # type: ignore[attr-defined,unused-ignore]
    assert result_v1_minimal[0].description is None  # type: ignore[attr-defined,unused-ignore]
    assert result_v1_minimal[0].stock == 0  # type: ignore[attr-defined,unused-ignore]

    # Test v2 with all fields provided
    parser_v2_full = PydanticToolsParser(tools=[UserV2])

Domain

Subdomains

Frequently Asked Questions

What does test_pydantic_tools_parser_with_optional_fields() do?
test_pydantic_tools_parser_with_optional_fields() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/output_parsers/test_openai_tools.py.
Where is test_pydantic_tools_parser_with_optional_fields() defined?
test_pydantic_tools_parser_with_optional_fields() is defined in libs/core/tests/unit_tests/output_parsers/test_openai_tools.py at line 1193.

Analyze Your Own Codebase

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

Try Supermodel Free