Home / Class/ PydanticOutputFunctionsParser Class — langchain Architecture

PydanticOutputFunctionsParser Class — langchain Architecture

Architecture documentation for the PydanticOutputFunctionsParser class in openai_functions.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  9e82a47f_5a67_03df_5090_a9ef55b8c16d["PydanticOutputFunctionsParser"]
  7b7af3a3_20c2_a402_1324_527ad8324dcb["OutputFunctionsParser"]
  9e82a47f_5a67_03df_5090_a9ef55b8c16d -->|extends| 7b7af3a3_20c2_a402_1324_527ad8324dcb
  d083bb96_f642_0585_b68e_6b3fea430962["openai_functions.py"]
  9e82a47f_5a67_03df_5090_a9ef55b8c16d -->|defined in| d083bb96_f642_0585_b68e_6b3fea430962
  2aee1697_2766_1a3f_7ebb_71adb3a63836["validate_schema()"]
  9e82a47f_5a67_03df_5090_a9ef55b8c16d -->|method| 2aee1697_2766_1a3f_7ebb_71adb3a63836
  f94d0997_ab7e_4784_55f2_39a8faf9465f["parse_result()"]
  9e82a47f_5a67_03df_5090_a9ef55b8c16d -->|method| f94d0997_ab7e_4784_55f2_39a8faf9465f

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/openai_functions.py lines 179–292

class PydanticOutputFunctionsParser(OutputFunctionsParser):
    """Parse an output as a Pydantic object.

    This parser is used to parse the output of a chat model that uses OpenAI function
    format to invoke functions.

    The parser extracts the function call invocation and matches them to the Pydantic
    schema provided.

    An exception will be raised if the function call does not match the provided schema.

    Example:
        ```python
        message = AIMessage(
            content="This is a test message",
            additional_kwargs={
                "function_call": {
                    "name": "cookie",
                    "arguments": json.dumps({"name": "value", "age": 10}),
                }
            },
        )
        chat_generation = ChatGeneration(message=message)


        class Cookie(BaseModel):
            name: str
            age: int


        class Dog(BaseModel):
            species: str


        # Full output
        parser = PydanticOutputFunctionsParser(
            pydantic_schema={"cookie": Cookie, "dog": Dog}
        )
        result = parser.parse_result([chat_generation])
        ```

    """

    pydantic_schema: type[BaseModel] | dict[str, type[BaseModel]]
    """The Pydantic schema to parse the output with.

    If multiple schemas are provided, then the function name will be used to
    determine which schema to use.
    """

    @model_validator(mode="before")
    @classmethod
    def validate_schema(cls, values: dict[str, Any]) -> Any:
        """Validate the Pydantic schema.

        Args:
            values: The values to validate.

        Returns:
            The validated values.

        Raises:
            ValueError: If the schema is not a Pydantic schema.
        """
        schema = values["pydantic_schema"]
        if "args_only" not in values:
            values["args_only"] = (
                isinstance(schema, type)
                and not isinstance(schema, GenericAlias)
                and issubclass(schema, BaseModel)
            )
        elif values["args_only"] and isinstance(schema, dict):
            msg = (
                "If multiple pydantic schemas are provided then args_only should be"
                " False."
            )
            raise ValueError(msg)
        return values

    @override
    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:

Domain

Frequently Asked Questions

What is the PydanticOutputFunctionsParser class?
PydanticOutputFunctionsParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/openai_functions.py.
Where is PydanticOutputFunctionsParser defined?
PydanticOutputFunctionsParser is defined in libs/core/langchain_core/output_parsers/openai_functions.py at line 179.
What does PydanticOutputFunctionsParser extend?
PydanticOutputFunctionsParser extends OutputFunctionsParser.

Analyze Your Own Codebase

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

Try Supermodel Free