Home / File/ datetime.py — langchain Source File

datetime.py — langchain Source File

Architecture documentation for datetime.py, a python file in the langchain codebase. 4 imports, 1 dependents.

File python OutputParsing StreamingParsers 4 imports 1 dependents 1 classes

Entity Profile

Dependency Diagram

graph LR
  407ff90e_0562_d558_f076_78bb52713fb9["datetime.py"]
  407ff90e_0562_d558_f076_78bb52713fb9["datetime.py"]
  407ff90e_0562_d558_f076_78bb52713fb9 --> 407ff90e_0562_d558_f076_78bb52713fb9
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  407ff90e_0562_d558_f076_78bb52713fb9 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  407ff90e_0562_d558_f076_78bb52713fb9 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  f4d905c6_a2b2_eb8f_be9b_7808b72f6a16["langchain_core.utils"]
  407ff90e_0562_d558_f076_78bb52713fb9 --> f4d905c6_a2b2_eb8f_be9b_7808b72f6a16
  407ff90e_0562_d558_f076_78bb52713fb9["datetime.py"]
  407ff90e_0562_d558_f076_78bb52713fb9 --> 407ff90e_0562_d558_f076_78bb52713fb9
  style 407ff90e_0562_d558_f076_78bb52713fb9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from datetime import datetime, timedelta, timezone

from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.utils import comma_list


class DatetimeOutputParser(BaseOutputParser[datetime]):
    """Parse the output of an LLM call to a datetime."""

    format: str = "%Y-%m-%dT%H:%M:%S.%fZ"
    """The string value that is used as the datetime format.

    Update this to match the desired datetime format for your application.
    """

    def get_format_instructions(self) -> str:
        """Returns the format instructions for the given format."""
        if self.format == "%Y-%m-%dT%H:%M:%S.%fZ":
            examples = comma_list(
                [
                    "2023-07-04T14:30:00.000000Z",
                    "1999-12-31T23:59:59.999999Z",
                    "2025-01-01T00:00:00.000000Z",
                ],
            )
        else:
            try:
                now = datetime.now(tz=timezone.utc)
                examples = comma_list(
                    [
                        now.strftime(self.format),
                        (now.replace(year=now.year - 1)).strftime(self.format),
                        (now - timedelta(days=1)).strftime(self.format),
                    ],
                )
            except ValueError:
                # Fallback if the format is very unusual
                examples = f"e.g., a valid string in the format {self.format}"

        return (
            f"Write a datetime string that matches the "
            f"following pattern: '{self.format}'.\n\n"
            f"Examples: {examples}\n\n"
            f"Return ONLY this string, no other words!"
        )

    def parse(self, response: str) -> datetime:
        """Parse a string into a datetime object."""
        try:
            return datetime.strptime(response.strip(), self.format)  # noqa: DTZ007
        except ValueError as e:
            msg = f"Could not parse datetime string: {response}"
            raise OutputParserException(msg) from e

    @property
    def _type(self) -> str:
        return "datetime"

Domain

Subdomains

Dependencies

  • datetime.py
  • langchain_core.exceptions
  • langchain_core.output_parsers
  • langchain_core.utils

Frequently Asked Questions

What does datetime.py do?
datetime.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What does datetime.py depend on?
datetime.py imports 4 module(s): datetime.py, langchain_core.exceptions, langchain_core.output_parsers, langchain_core.utils.
What files import datetime.py?
datetime.py is imported by 1 file(s): datetime.py.
Where is datetime.py in the architecture?
datetime.py is located at libs/langchain/langchain_classic/output_parsers/datetime.py (domain: OutputParsing, subdomain: StreamingParsers, directory: libs/langchain/langchain_classic/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free