Home / File/ extraction.py — langchain Source File

extraction.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  373909ee_9e38_721c_c1e8_048209e29545["extraction.py"]
  b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"]
  373909ee_9e38_721c_c1e8_048209e29545 --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  373909ee_9e38_721c_c1e8_048209e29545 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  e621cf3d_5d65_d51d_0d81_469c2c9e4846["langchain_core.output_parsers.openai_tools"]
  373909ee_9e38_721c_c1e8_048209e29545 --> e621cf3d_5d65_d51d_0d81_469c2c9e4846
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  373909ee_9e38_721c_c1e8_048209e29545 --> e6b4f61e_7b98_6666_3641_26b069517d4a
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  373909ee_9e38_721c_c1e8_048209e29545 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  a6f93333_6d02_5473_677b_843422594588["langchain_core.utils.function_calling"]
  373909ee_9e38_721c_c1e8_048209e29545 --> a6f93333_6d02_5473_677b_843422594588
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  373909ee_9e38_721c_c1e8_048209e29545 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  style 373909ee_9e38_721c_c1e8_048209e29545 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from langchain_core._api import deprecated
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers.openai_tools import PydanticToolsParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import Runnable
from langchain_core.utils.function_calling import (
    convert_to_openai_function as convert_pydantic_to_openai_function,
)
from pydantic import BaseModel

_EXTRACTION_TEMPLATE = """Extract and save the relevant entities mentioned \
in the following passage together with their properties.

If a property is not present and is not required in the function parameters, do not include it in the output."""  # noqa: E501


@deprecated(
    since="0.1.14",
    message=(
        "LangChain has introduced a method called `with_structured_output` that"
        "is available on ChatModels capable of tool calling."
        "You can read more about the method here: "
        "<https://docs.langchain.com/oss/python/langchain/models#structured-outputs>. "
        "Please follow our extraction use case documentation for more guidelines"
        "on how to do information extraction with LLMs."
        "<https://python.langchain.com/docs/use_cases/extraction/>. "
        "with_structured_output does not currently support a list of pydantic schemas. "
        "If this is a blocker or if you notice other issues, please provide "
        "feedback here:"
        "<https://github.com/langchain-ai/langchain/discussions/18154>"
    ),
    removal="1.0",
    alternative=(
        """
            from pydantic import BaseModel, Field
            from langchain_anthropic import ChatAnthropic

            class Joke(BaseModel):
                setup: str = Field(description="The setup of the joke")
                punchline: str = Field(description="The punchline to the joke")

            # Or any other chat model that supports tools.
            # Please reference to the documentation of structured_output
            # to see an up to date list of which models support
            # with_structured_output.
            model = ChatAnthropic(model="claude-opus-4-1-20250805", temperature=0)
            structured_model = model.with_structured_output(Joke)
            structured_model.invoke("Tell me a joke about cats.
                Make sure to call the Joke function.")
            """
    ),
)
def create_extraction_chain_pydantic(
    pydantic_schemas: list[type[BaseModel]] | type[BaseModel],
    llm: BaseLanguageModel,
    system_message: str = _EXTRACTION_TEMPLATE,
) -> Runnable:
    """Creates a chain that extracts information from a passage.

    Args:
        pydantic_schemas: The schema of the entities to extract.
        llm: The language model to use.
        system_message: The system message to use for extraction.

    Returns:
        A runnable that extracts information from a passage.
    """
    if not isinstance(pydantic_schemas, list):
        pydantic_schemas = [pydantic_schemas]
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", system_message),
            ("user", "{input}"),
        ],
    )
    functions = [convert_pydantic_to_openai_function(p) for p in pydantic_schemas]
    tools = [{"type": "function", "function": d} for d in functions]
    model = llm.bind(tools=tools)
    return prompt | model | PydanticToolsParser(tools=pydantic_schemas)

Subdomains

Dependencies

  • langchain_core._api
  • langchain_core.language_models
  • langchain_core.output_parsers.openai_tools
  • langchain_core.prompts
  • langchain_core.runnables
  • langchain_core.utils.function_calling
  • pydantic

Frequently Asked Questions

What does extraction.py do?
extraction.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in extraction.py?
extraction.py defines 1 function(s): create_extraction_chain_pydantic.
What does extraction.py depend on?
extraction.py imports 7 module(s): langchain_core._api, langchain_core.language_models, langchain_core.output_parsers.openai_tools, langchain_core.prompts, langchain_core.runnables, langchain_core.utils.function_calling, pydantic.
Where is extraction.py in the architecture?
extraction.py is located at libs/langchain/langchain_classic/chains/openai_tools/extraction.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains/openai_tools).

Analyze Your Own Codebase

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

Try Supermodel Free