Home / File/ test_retry.py — langchain Source File

test_retry.py — langchain Source File

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

File python CoreAbstractions MessageSchema 11 imports 16 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  dede4f38_4194_95e0_7303_b7ab0ebaee2a["test_retry.py"]
  af34f08b_0ede_2b87_0db6_983d74ed0249["datetime"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> af34f08b_0ede_2b87_0db6_983d74ed0249
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 120e2591_3e15_b895_72b6_cb26195e40a6
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  5b417886_56dd_6afa_13ab_a3cfc1dbcccd["langchain_core.prompt_values"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 5b417886_56dd_6afa_13ab_a3cfc1dbcccd
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 91721f45_4909_e489_8c1f_084f8bd87145
  224ab9fb_0538_7d3c_bef3_5f1c82d3a53a["langchain_classic.output_parsers.boolean"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 224ab9fb_0538_7d3c_bef3_5f1c82d3a53a
  251f3c33_ad92_a635_5d45_4edf27b17150["langchain_classic.output_parsers.datetime"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 251f3c33_ad92_a635_5d45_4edf27b17150
  10431e66_4bc8_4906_3270_dcfcae045eab["langchain_classic.output_parsers.retry"]
  dede4f38_4194_95e0_7303_b7ab0ebaee2a --> 10431e66_4bc8_4906_3270_dcfcae045eab
  style dede4f38_4194_95e0_7303_b7ab0ebaee2a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from datetime import datetime as dt
from datetime import timezone
from typing import Any, TypeVar

import pytest
from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.prompt_values import PromptValue, StringPromptValue
from langchain_core.runnables import Runnable, RunnableLambda, RunnablePassthrough
from typing_extensions import override

from langchain_classic.output_parsers.boolean import BooleanOutputParser
from langchain_classic.output_parsers.datetime import DatetimeOutputParser
from langchain_classic.output_parsers.retry import (
    NAIVE_RETRY_PROMPT,
    NAIVE_RETRY_WITH_ERROR_PROMPT,
    RetryOutputParser,
    RetryWithErrorOutputParser,
)

T = TypeVar("T")


class SuccessfulParseAfterRetries(BaseOutputParser[str]):
    parse_count: int = 0  # Number of times parse has been called
    attemp_count_before_success: int  # Number of times to fail before succeeding
    error_msg: str = "error"

    @override
    def parse(self, *args: Any, **kwargs: Any) -> str:
        self.parse_count += 1
        if self.parse_count <= self.attemp_count_before_success:
            raise OutputParserException(self.error_msg)
        return "parsed"


def test_retry_output_parser_parse_with_prompt() -> None:
    n: int = 5  # Success on the (n+1)-th attempt
    base_parser = SuccessfulParseAfterRetries(attemp_count_before_success=n)
    parser = RetryOutputParser[str](
        parser=base_parser,
        retry_chain=RunnablePassthrough(),
        max_retries=n,  # n times to retry, that is, (n+1) times call
        legacy=False,
    )
    actual = parser.parse_with_prompt("completion", StringPromptValue(text="dummy"))
    assert actual == "parsed"
    assert base_parser.parse_count == n + 1


def test_retry_output_parser_parse_with_prompt_fail() -> None:
    n: int = 5  # Success on the (n+1)-th attempt
    base_parser = SuccessfulParseAfterRetries(attemp_count_before_success=n)
    parser = RetryOutputParser[str](
        parser=base_parser,
        retry_chain=RunnablePassthrough(),
        max_retries=n - 1,  # n-1 times to retry, that is, n times call
        legacy=False,
    )
    with pytest.raises(OutputParserException):
// ... (261 more lines)

Subdomains

Dependencies

  • datetime
  • langchain_classic.output_parsers.boolean
  • langchain_classic.output_parsers.datetime
  • langchain_classic.output_parsers.retry
  • langchain_core.exceptions
  • langchain_core.output_parsers
  • langchain_core.prompt_values
  • langchain_core.runnables
  • pytest
  • typing
  • typing_extensions

Frequently Asked Questions

What does test_retry.py do?
test_retry.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_retry.py?
test_retry.py defines 16 function(s): test_retry_output_parser_aparse_with_prompt, test_retry_output_parser_aparse_with_prompt_fail, test_retry_output_parser_aparse_with_prompt_with_retry_chain, test_retry_output_parser_output_type, test_retry_output_parser_parse_is_not_implemented, test_retry_output_parser_parse_with_prompt, test_retry_output_parser_parse_with_prompt_fail, test_retry_output_parser_parse_with_prompt_with_retry_chain, test_retry_with_error_output_parser_aparse_with_prompt, test_retry_with_error_output_parser_aparse_with_prompt_fail, and 6 more.
What does test_retry.py depend on?
test_retry.py imports 11 module(s): datetime, langchain_classic.output_parsers.boolean, langchain_classic.output_parsers.datetime, langchain_classic.output_parsers.retry, langchain_core.exceptions, langchain_core.output_parsers, langchain_core.prompt_values, langchain_core.runnables, and 3 more.
Where is test_retry.py in the architecture?
test_retry.py is located at libs/langchain/tests/unit_tests/output_parsers/test_retry.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain/tests/unit_tests/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free