Home / Function/ test_pydantic_tools_parser_with_mixed_pydantic_versions() — langchain Function Reference

test_pydantic_tools_parser_with_mixed_pydantic_versions() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/core/tests/unit_tests/output_parsers/test_openai_tools.py lines 898–986

def test_pydantic_tools_parser_with_mixed_pydantic_versions() -> None:
    """Test PydanticToolsParser with both Pydantic v1 and v2 models."""
    # For Python 3.14+ compatibility, use create_model for Pydantic v1
    if sys.version_info >= (3, 14):
        WeatherV1 = pydantic.v1.create_model(  # noqa: N806
            "WeatherV1",
            __doc__="Weather information using Pydantic v1.",
            temperature=(int, ...),
            conditions=(str, ...),
        )
    else:

        class WeatherV1(pydantic.v1.BaseModel):
            """Weather information using Pydantic v1."""

            temperature: int
            conditions: str

    class LocationV2(BaseModel):
        """Location information using Pydantic v2."""

        city: str
        country: str

    # Test with Pydantic v1 model
    parser_v1 = PydanticToolsParser(tools=[WeatherV1])
    message_v1 = AIMessage(
        content="",
        tool_calls=[
            {
                "id": "call_weather",
                "name": "WeatherV1",
                "args": {"temperature": 25, "conditions": "sunny"},
            }
        ],
    )
    generation_v1 = ChatGeneration(message=message_v1)
    result_v1 = parser_v1.parse_result([generation_v1])

    assert len(result_v1) == 1
    assert isinstance(result_v1[0], WeatherV1)
    assert result_v1[0].temperature == 25  # type: ignore[attr-defined,unused-ignore]
    assert result_v1[0].conditions == "sunny"  # type: ignore[attr-defined,unused-ignore]

    # Test with Pydantic v2 model
    parser_v2 = PydanticToolsParser(tools=[LocationV2])
    message_v2 = AIMessage(
        content="",
        tool_calls=[
            {
                "id": "call_location",
                "name": "LocationV2",
                "args": {"city": "Paris", "country": "France"},
            }
        ],
    )
    generation_v2 = ChatGeneration(message=message_v2)
    result_v2 = parser_v2.parse_result([generation_v2])

    assert len(result_v2) == 1
    assert isinstance(result_v2[0], LocationV2)
    assert result_v2[0].city == "Paris"
    assert result_v2[0].country == "France"

    # Test with both v1 and v2 models
    parser_mixed = PydanticToolsParser(tools=[WeatherV1, LocationV2])
    message_mixed = AIMessage(
        content="",
        tool_calls=[
            {
                "id": "call_weather",
                "name": "WeatherV1",
                "args": {"temperature": 20, "conditions": "cloudy"},
            },
            {
                "id": "call_location",
                "name": "LocationV2",
                "args": {"city": "London", "country": "UK"},
            },
        ],
    )

Domain

Subdomains

Frequently Asked Questions

What does test_pydantic_tools_parser_with_mixed_pydantic_versions() do?
test_pydantic_tools_parser_with_mixed_pydantic_versions() 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_mixed_pydantic_versions() defined?
test_pydantic_tools_parser_with_mixed_pydantic_versions() is defined in libs/core/tests/unit_tests/output_parsers/test_openai_tools.py at line 898.

Analyze Your Own Codebase

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

Try Supermodel Free