LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-classicchainsopenai_functionsbasecreate_structured_output_chain
    Function●Since v1.0Deprecated

    create_structured_output_chain

    [Legacy] Create an LLMChain that uses an OpenAI function to get a structured output.

    Copy
    create_structured_output_chain(
      output_schema: dict[str, Any] | type[BaseModel],
      llm: BaseLanguageModel,
      prompt: BasePromptTemplate,
      *,
      output_key: str = 'function',
      output_parser: BaseLLMOutputParser | None = None,
      **kwargs: Any = {}
    ) -> LLMChain

    Example:

    from typing import Optional
    
    from langchain_classic.chains.openai_functions import create_structured_output_chain
    from langchain_openai import ChatOpenAI
    from langchain_core.prompts import ChatPromptTemplate
    
    from pydantic import BaseModel, Field
    
    class Dog(BaseModel):
        """Identifying information about a dog."""
    
        name: str = Field(..., description="The dog's name")
        color: str = Field(..., description="The dog's color")
        fav_food: str | None = Field(None, description="The dog's favorite food")
    
    model = ChatOpenAI(model="gpt-3.5-turbo-0613", temperature=0)
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", "You are a world class algorithm for extracting information in structured formats."),
            ("human", "Use the given format to extract information from the following input: {input}"),
            ("human", "Tip: Make sure to answer in the correct format"),
        ]
    )
    chain = create_structured_output_chain(Dog, model, prompt)
    chain.run("Harry was a chubby brown beagle who loved chicken")
    # -> Dog(name="Harry", color="brown", fav_food="chicken")
    

    Used in Docs

    • Activeloop Deep memory integration

    Parameters

    NameTypeDescription
    output_schema*dict[str, Any] | type[BaseModel]

    Either a dictionary or pydantic.BaseModel class. If a dictionary is passed in, it's assumed to already be a valid JsonSchema. For best results, pydantic.BaseModels should have docstrings describing what the schema represents and descriptions for the parameters.

    llm*BaseLanguageModel

    Language model to use, assumed to support the OpenAI function-calling API.

    prompt*BasePromptTemplate

    BasePromptTemplate to pass to the model.

    output_keystr
    Default:'function'

    The key to use when returning the output in LLMChain.call.

    output_parserBaseLLMOutputParser | None
    Default:None

    BaseLLMOutputParser to use for parsing model outputs. By default will be inferred from the function types. If pydantic.BaseModels are passed in, then the OutputParser will try to parse outputs using those. Otherwise model outputs will simply be parsed as JSON.

    **kwargsAny
    Default:{}

    Additional keyword arguments to pass to LLMChain.

    View source on GitHub