Home / File/ test_structured.py — langchain Source File

test_structured.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 13 imports 6 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  3513030b_4904_ecfd_7a0d_cc92be4dda69["test_structured.py"]
  c990f2d7_9509_7cea_ca95_51ad57dbe5c6["functools"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> c990f2d7_9509_7cea_ca95_51ad57dbe5c6
  614e7b9f_ed51_0780_749c_ff40b74963fc["inspect"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 614e7b9f_ed51_0780_749c_ff40b74963fc
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 91721f45_4909_e489_8c1f_084f8bd87145
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  f10c7807_dbfb_545d_042b_5250f9fd7d51["langchain_core.load.dump"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> f10c7807_dbfb_545d_042b_5250f9fd7d51
  553b7d61_3a8f_8bb0_a4d7_a5aed262f254["langchain_core.load.load"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 553b7d61_3a8f_8bb0_a4d7_a5aed262f254
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> d758344f_537f_649e_f467_b9d7442e86df
  ce97bd37_9a2d_26ed_d26a_6028988cce73["langchain_core.prompts.structured"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> ce97bd37_9a2d_26ed_d26a_6028988cce73
  c764ccae_0d75_abec_7c23_6d5d1949a7ba["langchain_core.runnables.base"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> c764ccae_0d75_abec_7c23_6d5d1949a7ba
  066d04b7_8dea_5927_9d18_3715ec142d6f["langchain_core.utils.mustache"]
  3513030b_4904_ecfd_7a0d_cc92be4dda69 --> 066d04b7_8dea_5927_9d18_3715ec142d6f
  style 3513030b_4904_ecfd_7a0d_cc92be4dda69 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from functools import partial
from inspect import isclass
from typing import Any, cast

import pytest
from pydantic import BaseModel
from typing_extensions import override

from langchain_core.language_models import FakeListChatModel
from langchain_core.load.dump import dumps
from langchain_core.load.load import loads
from langchain_core.messages import HumanMessage
from langchain_core.prompts.structured import StructuredPrompt
from langchain_core.runnables.base import Runnable, RunnableLambda
from langchain_core.utils.mustache import ChevronError


def _fake_runnable(
    _: Any, *, schema: dict[str, Any] | type[BaseModel], value: Any = 42, **_kwargs: Any
) -> BaseModel | dict[str, Any]:
    if isclass(schema) and issubclass(schema, BaseModel):
        return schema(name="yo", value=value)
    params = cast("dict[str, Any]", schema)["parameters"]
    return {k: 1 if k != "value" else value for k, v in params.items()}


class FakeStructuredChatModel(FakeListChatModel):
    """Fake chat model for testing purposes."""

    @override
    def with_structured_output(
        self, schema: dict | type[BaseModel], **kwargs: Any
    ) -> Runnable:
        return RunnableLambda(partial(_fake_runnable, schema=schema, **kwargs))

    @property
    def _llm_type(self) -> str:
        return "fake-messages-list-chat-model"


def test_structured_prompt_pydantic() -> None:
    class OutputSchema(BaseModel):
        name: str
        value: int

    prompt = StructuredPrompt(
        [
            ("human", "I'm very structured, how about you?"),
        ],
        OutputSchema,
    )

    model = FakeStructuredChatModel(responses=[])

    chain = prompt | model

    assert chain.invoke({"hello": "there"}) == OutputSchema(name="yo", value=42)  # type: ignore[comparison-overlap]


def test_structured_prompt_dict() -> None:
// ... (84 more lines)

Subdomains

Dependencies

  • functools
  • inspect
  • langchain_core.language_models
  • langchain_core.load.dump
  • langchain_core.load.load
  • langchain_core.messages
  • langchain_core.prompts.structured
  • langchain_core.runnables.base
  • langchain_core.utils.mustache
  • pydantic
  • pytest
  • typing
  • typing_extensions

Frequently Asked Questions

What does test_structured.py do?
test_structured.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_structured.py?
test_structured.py defines 6 function(s): _fake_runnable, test_structured_prompt_dict, test_structured_prompt_kwargs, test_structured_prompt_pydantic, test_structured_prompt_template_empty_vars, test_structured_prompt_template_format.
What does test_structured.py depend on?
test_structured.py imports 13 module(s): functools, inspect, langchain_core.language_models, langchain_core.load.dump, langchain_core.load.load, langchain_core.messages, langchain_core.prompts.structured, langchain_core.runnables.base, and 5 more.
Where is test_structured.py in the architecture?
test_structured.py is located at libs/core/tests/unit_tests/prompts/test_structured.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/tests/unit_tests/prompts).

Analyze Your Own Codebase

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

Try Supermodel Free