Home / File/ test_pydantic_serde.py — langchain Source File

test_pydantic_serde.py — langchain Source File

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

File python CoreAbstractions MessageSchema 3 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  0d9a712e_1c82_221f_c74a_e9b7382702a5["test_pydantic_serde.py"]
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  0d9a712e_1c82_221f_c74a_e9b7382702a5 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  0d9a712e_1c82_221f_c74a_e9b7382702a5 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  0d9a712e_1c82_221f_c74a_e9b7382702a5 --> d758344f_537f_649e_f467_b9d7442e86df
  style 0d9a712e_1c82_221f_c74a_e9b7382702a5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test pydantic SerDe.

A set of tests that verifies that Union discrimination works correctly with
the various pydantic base models.

These tests can uncover issues that will also arise during regular instantiation
of the models (i.e., not necessarily from loading or dumping JSON).
"""

import pytest
from pydantic import RootModel, ValidationError

from langchain_core.messages import (
    AIMessage,
    AIMessageChunk,
    AnyMessage,
    ChatMessage,
    ChatMessageChunk,
    FunctionMessage,
    FunctionMessageChunk,
    HumanMessage,
    HumanMessageChunk,
    SystemMessage,
    SystemMessageChunk,
)


def test_serde_any_message() -> None:
    """Test AnyMessage() serder."""
    lc_objects = [
        HumanMessage(content="human"),
        HumanMessageChunk(content="human"),
        AIMessage(content="ai"),
        AIMessageChunk(content="ai"),
        SystemMessage(content="sys"),
        SystemMessageChunk(content="sys"),
        FunctionMessage(
            name="func",
            content="func",
        ),
        FunctionMessageChunk(
            name="func",
            content="func",
        ),
        ChatMessage(
            role="human",
            content="human",
        ),
        ChatMessageChunk(
            role="human",
            content="human",
        ),
    ]

    model = RootModel[AnyMessage]

    for lc_object in lc_objects:
        d = lc_object.model_dump()
        assert "type" in d, f"Missing key `type` for {type(lc_object)}"
        obj1 = model.model_validate(d)
        assert type(obj1.root) is type(lc_object), f"failed for {type(lc_object)}"

    with pytest.raises((TypeError, ValidationError)):
        # Make sure that specifically validation error is raised
        model.model_validate({})

Subdomains

Dependencies

  • langchain_core.messages
  • pydantic
  • pytest

Frequently Asked Questions

What does test_pydantic_serde.py do?
test_pydantic_serde.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in test_pydantic_serde.py?
test_pydantic_serde.py defines 1 function(s): test_serde_any_message.
What does test_pydantic_serde.py depend on?
test_pydantic_serde.py imports 3 module(s): langchain_core.messages, pydantic, pytest.
Where is test_pydantic_serde.py in the architecture?
test_pydantic_serde.py is located at libs/core/tests/unit_tests/test_pydantic_serde.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/tests/unit_tests).

Analyze Your Own Codebase

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

Try Supermodel Free