langchain.chains.ernie_functions.base.create_structured_output_runnable(output_schema: Union[Dict[str, Any], Type[BaseModel]], llm: Runnable, prompt: BasePromptTemplate, *, output_parser: Optional[Union[BaseOutputParser, BaseGenerationOutputParser]] = None, **kwargs: Any) Runnable[source]¶

Create a runnable that uses an Ernie function to get a structured output.

  • output_schema (Union[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 (Runnable) – Language model to use, assumed to support the Ernie function-calling API.

  • prompt (BasePromptTemplate) – BasePromptTemplate to pass to the model.

  • output_parser (Optional[Union[BaseOutputParser, BaseGenerationOutputParser]]) – 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.

  • kwargs (Any) –


A runnable sequence that will pass the given function to the model when run.

Return type



from typing import Optional

from langchain.chains.ernie_functions import create_structured_output_chain
from langchain_community.chat_models import ErnieBotChat
from langchain_core.prompts import ChatPromptTemplate
from langchain.pydantic_v1 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: Optional[str] = Field(None, description="The dog's favorite food")

llm = ErnieBotChat(model_name="ERNIE-Bot-4")
prompt = ChatPromptTemplate.from_messages(
        ("user", "Use the given format to extract information from the following input: {input}"),
        ("assistant", "OK!"),
        ("user", "Tip: Make sure to answer in the correct format"),
chain = create_structured_output_chain(Dog, llm, prompt)
chain.invoke({"input": "Harry was a chubby brown beagle who loved chicken"})
# -> Dog(name="Harry", color="brown", fav_food="chicken")

Examples using create_structured_output_runnable¶