Home / Class/ DatetimeOutputParser Class — langchain Architecture

DatetimeOutputParser Class — langchain Architecture

Architecture documentation for the DatetimeOutputParser class in datetime.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  2f806e11_8650_3820_0781_ec967e39cb53["DatetimeOutputParser"]
  407ff90e_0562_d558_f076_78bb52713fb9["datetime.py"]
  2f806e11_8650_3820_0781_ec967e39cb53 -->|defined in| 407ff90e_0562_d558_f076_78bb52713fb9
  595e7325_93dc_d4e6_4162_86aa22edf649["get_format_instructions()"]
  2f806e11_8650_3820_0781_ec967e39cb53 -->|method| 595e7325_93dc_d4e6_4162_86aa22edf649
  bf9083d6_e023_29c3_8123_53243aee021c["parse()"]
  2f806e11_8650_3820_0781_ec967e39cb53 -->|method| bf9083d6_e023_29c3_8123_53243aee021c
  b19f7568_6231_f593_0b66_601caba65594["_type()"]
  2f806e11_8650_3820_0781_ec967e39cb53 -->|method| b19f7568_6231_f593_0b66_601caba65594

Relationship Graph

Source Code

libs/langchain/langchain_classic/output_parsers/datetime.py lines 8–58

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

Frequently Asked Questions

What is the DatetimeOutputParser class?
DatetimeOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/output_parsers/datetime.py.
Where is DatetimeOutputParser defined?
DatetimeOutputParser is defined in libs/langchain/langchain_classic/output_parsers/datetime.py at line 8.

Analyze Your Own Codebase

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

Try Supermodel Free