Home / Class/ StructuredPrompt Class — langchain Architecture

StructuredPrompt Class — langchain Architecture

Architecture documentation for the StructuredPrompt class in structured.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  14de95af_eb88_e7a8_1ab1_98d03dd541e0["StructuredPrompt"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6["ChatPromptTemplate"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|extends| 6be4a9a5_5fe4_e64f_c374_e63767576bf6
  cd3c2a54_68ad_0d2e_ac51_e9be79fd1958["BaseLanguageModel"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|extends| cd3c2a54_68ad_0d2e_ac51_e9be79fd1958
  7bca06c0_7ad1_b26c_4875_0c69faace0f6["structured.py"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|defined in| 7bca06c0_7ad1_b26c_4875_0c69faace0f6
  860d9f0c_4ab4_1325_4192_4e882423b69c["__init__()"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|method| 860d9f0c_4ab4_1325_4192_4e882423b69c
  3a844540_f005_7d51_4704_c6c16b57b29e["get_lc_namespace()"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|method| 3a844540_f005_7d51_4704_c6c16b57b29e
  d82b3abe_4d8c_033b_9412_c35509b27346["from_messages_and_schema()"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|method| d82b3abe_4d8c_033b_9412_c35509b27346
  c93652b8_42d7_bf97_e416_56bd747ea444["__or__()"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|method| c93652b8_42d7_bf97_e416_56bd747ea444
  aa20e50e_3f08_075e_f059_e71b8aad4032["pipe()"]
  14de95af_eb88_e7a8_1ab1_98d03dd541e0 -->|method| aa20e50e_3f08_075e_f059_e71b8aad4032

Relationship Graph

Source Code

libs/core/langchain_core/prompts/structured.py lines 28–183

class StructuredPrompt(ChatPromptTemplate):
    """Structured prompt template for a language model."""

    schema_: dict | type
    """Schema for the structured prompt."""

    structured_output_kwargs: dict[str, Any] = Field(default_factory=dict)

    def __init__(
        self,
        messages: Sequence[MessageLikeRepresentation],
        schema_: dict | type[BaseModel] | None = None,
        *,
        structured_output_kwargs: dict[str, Any] | None = None,
        template_format: PromptTemplateFormat = "f-string",
        **kwargs: Any,
    ) -> None:
        """Create a structured prompt template.

        Args:
            messages: Sequence of messages.
            schema_: Schema for the structured prompt.
            structured_output_kwargs: Additional kwargs for structured output.
            template_format: Template format for the prompt.

        Raises:
            ValueError: If schema is not provided.
        """
        schema_ = schema_ or kwargs.pop("schema", None)
        if not schema_:
            err_msg = (
                "Must pass in a non-empty structured output schema. Received: "
                f"{schema_}"
            )
            raise ValueError(err_msg)
        structured_output_kwargs = structured_output_kwargs or {}
        for k in set(kwargs).difference(get_pydantic_field_names(self.__class__)):
            structured_output_kwargs[k] = kwargs.pop(k)
        super().__init__(
            messages=messages,
            schema_=schema_,
            structured_output_kwargs=structured_output_kwargs,
            template_format=template_format,
            **kwargs,
        )

    @classmethod
    def get_lc_namespace(cls) -> list[str]:
        """Get the namespace of the LangChain object.

        For example, if the class is `langchain.llms.openai.OpenAI`, then the namespace
        is `["langchain", "llms", "openai"]`

        Returns:
            The namespace of the LangChain object.
        """
        return cls.__module__.split(".")

    @classmethod
    def from_messages_and_schema(
        cls,
        messages: Sequence[MessageLikeRepresentation],
        schema: dict | type,
        **kwargs: Any,
    ) -> ChatPromptTemplate:
        """Create a chat prompt template from a variety of message formats.

        Examples:
            Instantiation from a list of message templates:

            ```python
            from langchain_core.prompts import StructuredPrompt


            class OutputSchema(BaseModel):
                name: str
                value: int


            template = StructuredPrompt(
                [

Frequently Asked Questions

What is the StructuredPrompt class?
StructuredPrompt is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/structured.py.
Where is StructuredPrompt defined?
StructuredPrompt is defined in libs/core/langchain_core/prompts/structured.py at line 28.
What does StructuredPrompt extend?
StructuredPrompt extends ChatPromptTemplate, BaseLanguageModel.

Analyze Your Own Codebase

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

Try Supermodel Free