Home / File/ test_prompt.py — langchain Source File

test_prompt.py — langchain Source File

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

File python CoreAbstractions Serialization 12 imports 31 functions

Entity Profile

Dependency Diagram

graph LR
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3["test_prompt.py"]
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  d47804b2_9963_be65_f855_93c7c6a0eab8["tempfile"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> d47804b2_9963_be65_f855_93c7c6a0eab8
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6ebcaae2_3bc1_badf_b751_e164ff2776c4["unittest"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 6ebcaae2_3bc1_badf_b751_e164ff2776c4
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  0b794afa_fd3d_a810_77fd_ed00bf90e394["packaging"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 0b794afa_fd3d_a810_77fd_ed00bf90e394
  66d6194f_d8c1_55b6_f522_311fdad57877["syrupy.assertion"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 66d6194f_d8c1_55b6_f522_311fdad57877
  c17bcf07_a2ef_b992_448f_5088d46a1e79["langchain_core.prompts.prompt"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> c17bcf07_a2ef_b992_448f_5088d46a1e79
  e10bb307_3784_1031_cf6b_680e7c362c93["langchain_core.prompts.string"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> e10bb307_3784_1031_cf6b_680e7c362c93
  b2f3f5a5_2c04_3f89_138f_ced45a8e8edd["langchain_core.tracers.run_collector"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> b2f3f5a5_2c04_3f89_138f_ced45a8e8edd
  1014fe78_cb2c_fde4_a624_ae899aa7ca8e["langchain_core.utils.pydantic"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 1014fe78_cb2c_fde4_a624_ae899aa7ca8e
  0c2180a7_0be1_b691_e68d_3d66de6d36e9["tests.unit_tests.pydantic_utils"]
  af1b998b_a7bf_9d5c_32ef_bab507a6bee3 --> 0c2180a7_0be1_b691_e68d_3d66de6d36e9
  style af1b998b_a7bf_9d5c_32ef_bab507a6bee3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test functionality related to prompts."""

import re
from tempfile import NamedTemporaryFile
from typing import Any, Literal
from unittest import mock

import pytest
from packaging import version
from syrupy.assertion import SnapshotAssertion

from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.prompts.string import PromptTemplateFormat
from langchain_core.tracers.run_collector import RunCollectorCallbackHandler
from langchain_core.utils.pydantic import PYDANTIC_VERSION
from tests.unit_tests.pydantic_utils import _normalize_schema

PYDANTIC_VERSION_AT_LEAST_29 = version.parse("2.9") <= PYDANTIC_VERSION


def test_prompt_valid() -> None:
    """Test prompts can be constructed."""
    template = "This is a {foo} test."
    input_variables = ["foo"]
    prompt = PromptTemplate(input_variables=input_variables, template=template)
    assert prompt.template == template
    assert prompt.input_variables == input_variables


def test_from_file_encoding() -> None:
    """Test that we can load a template from a file with a non utf-8 encoding."""
    template = "This is a {foo} test with special character €."
    input_variables = ["foo"]

    # First write to a file using CP-1252 encoding.
    with NamedTemporaryFile(delete=True, mode="w", encoding="cp1252") as f:
        f.write(template)
        f.flush()
        file_name = f.name

        # Now read from the file using CP-1252 encoding and test
        prompt = PromptTemplate.from_file(file_name, encoding="cp1252")
        assert prompt.template == template
        assert prompt.input_variables == input_variables

        # Now read from the file using UTF-8 encoding and test
        with pytest.raises(UnicodeDecodeError):
            PromptTemplate.from_file(file_name, encoding="utf-8")


def test_prompt_from_template() -> None:
    """Test prompts can be constructed from a template."""
    # Single input variable.
    template = "This is a {foo} test."
    prompt = PromptTemplate.from_template(template)
    expected_prompt = PromptTemplate(template=template, input_variables=["foo"])
    assert prompt == expected_prompt

    # Multiple input variables.
    template = "This {bar} is a {foo} test."
// ... (671 more lines)

Subdomains

Dependencies

  • langchain_core.prompts.prompt
  • langchain_core.prompts.string
  • langchain_core.tracers.run_collector
  • langchain_core.utils.pydantic
  • packaging
  • pytest
  • re
  • syrupy.assertion
  • tempfile
  • tests.unit_tests.pydantic_utils
  • typing
  • unittest

Frequently Asked Questions

What does test_prompt.py do?
test_prompt.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in test_prompt.py?
test_prompt.py defines 31 function(s): test_basic_sandboxing_with_jinja2, test_from_file_encoding, test_mustache_prompt_from_template, test_partial, test_partial_init_func, test_partial_init_string, test_prompt_ainvoke_with_metadata, test_prompt_empty_input_variable, test_prompt_falsy_vars, test_prompt_from_examples_valid, and 21 more.
What does test_prompt.py depend on?
test_prompt.py imports 12 module(s): langchain_core.prompts.prompt, langchain_core.prompts.string, langchain_core.tracers.run_collector, langchain_core.utils.pydantic, packaging, pytest, re, syrupy.assertion, and 4 more.
Where is test_prompt.py in the architecture?
test_prompt.py is located at libs/core/tests/unit_tests/prompts/test_prompt.py (domain: CoreAbstractions, subdomain: Serialization, 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