Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsHow toAsync APIDifferent call methodsCustom chainDebugging chainsLoading from LangChainHubAdding memory (state)Using OpenAI functionsSerializationFoundationalDocumentsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesChainsHow toUsing OpenAI functionsOn this pageUsing OpenAI functionsThis walkthrough demonstrates how to incorporate OpenAI function-calling API's in a chain. We'll go over: How to use functions to get structured outputs from ChatOpenAIHow to create a generic chain that uses (multiple) functionsHow to create a chain that actually executes the chosen functionfrom typing import Optionalfrom langchain.chains.openai_functions import (    create_openai_fn_chain,    create_structured_output_chain,)from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplateGetting structured outputs‚ÄãWe can take advantage of OpenAI functions to try and force the model to return a particular kind of structured output. We'll use create_structured_output_chain to create our chain, which takes the desired structured output either as a Pydantic class or as JsonSchema.See here for relevant reference docs.Using Pydantic classes‚ÄãWhen passing in Pydantic classes to structure our text, we need to make sure to have a docstring description for the class. It also helps to have descriptions for each of the classes attributes.from langchain.pydantic_v1 import BaseModel, Fieldclass Person(BaseModel):    """Identifying information about a person."""    name: str = Field(..., description="The person's name")    age: int = Field(..., description="The person's age")    fav_food: Optional[str] = Field(None, description="The person's favorite food")# If we pass in
This walkthrough demonstrates how to incorporate OpenAI function-calling API's in a chain. We'll go over:
This walkthrough demonstrates how to incorporate OpenAI function-calling API's in a chain. We'll go over: ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsHow toAsync APIDifferent call methodsCustom chainDebugging chainsLoading from LangChainHubAdding memory (state)Using OpenAI functionsSerializationFoundationalDocumentsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesChainsHow toUsing OpenAI functionsOn this pageUsing OpenAI functionsThis walkthrough demonstrates how to incorporate OpenAI function-calling API's in a chain. We'll go over: How to use functions to get structured outputs from ChatOpenAIHow to create a generic chain that uses (multiple) functionsHow to create a chain that actually executes the chosen functionfrom typing import Optionalfrom langchain.chains.openai_functions import (    create_openai_fn_chain,    create_structured_output_chain,)from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplateGetting structured outputs‚ÄãWe can take advantage of OpenAI functions to try and force the model to return a particular kind of structured output. We'll use create_structured_output_chain to create our chain, which takes the desired structured output either as a Pydantic class or as JsonSchema.See here for relevant reference docs.Using Pydantic classes‚ÄãWhen passing in Pydantic classes to structure our text, we need to make sure to have a docstring description for the class. It also helps to have descriptions for each of the classes attributes.from langchain.pydantic_v1 import BaseModel, Fieldclass Person(BaseModel):    """Identifying information about a person."""    name: str = Field(..., description="The person's name")    age: int = Field(..., description="The person's age")    fav_food: Optional[str] = Field(None, description="The person's favorite food")# If we pass in