*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to use prompting alone (no tool calling) to do extraction

On this page

How to use prompting alone (no tool calling) to do extraction
=============================================================

Tool calling features are not required for generating structured output from LLMs. LLMs that are able to follow prompt instructions well can be tasked with outputting information in a given format.

This approach relies on designing good prompts and then parsing the output of the LLMs to make them extract information well.

To extract data without tool-calling features:

1.  Instruct the LLM to generate text following an expected format (e.g., JSON with a certain schema);
2.  Use [output parsers](/v0.2/docs/concepts/#output-parsers) to structure the model response into a desired Python object.

First we select a LLM:

*   OpenAI
*   Anthropic
*   Azure
*   Google
*   Cohere
*   FireworksAI
*   Groq
*   MistralAI
*   TogetherAI

    pip install -qU langchain-openai

    import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass()from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-3.5-turbo-0125")

    pip install -qU langchain-anthropic

    import getpassimport osos.environ["ANTHROPIC_API_KEY"] = getpass.getpass()from langchain_anthropic import ChatAnthropicmodel = ChatAnthropic(model="claude-3-sonnet-20240229")

    pip install -qU langchain-openai

    import getpassimport osos.environ["AZURE_OPENAI_API_KEY"] = getpass.getpass()from langchain_openai import AzureChatOpenAImodel = AzureChatOpenAI(    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],    azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],    openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],)

    pip install -qU langchain-google-vertexai

    import getpassimport osos.environ["GOOGLE_API_KEY"] = getpass.getpass()from langchain_google_vertexai import ChatVertexAImodel = ChatVertexAI(model="gemini-pro")

    pip install -qU langchain-cohere

    import getpassimport osos.environ["COHERE_API_KEY"] = getpass.getpass()from langchain_cohere import ChatCoheremodel = ChatCohere(model="command-r")

    pip install -qU langchain-fireworks

    import getpassimport osos.environ["FIREWORKS_API_KEY"] = getpass.getpass()from langchain_fireworks import ChatFireworksmodel = ChatFireworks(model="accounts/fireworks/models/mixtral-8x7b-instruct")

    pip install -qU langchain-groq

    import getpassimport osos.environ["GROQ_API_KEY"] = getpass.getpass()from langchain_groq import ChatGroqmodel = ChatGroq(model="llama3-8b-8192")

    pip install -qU langchain-mistralai

    import getpassimport osos.environ["MISTRAL_API_KEY"] = getpass.getpass()from langchain_mistralai import ChatMistralAImodel = ChatMistralAI(model="mistral-large-latest")

    pip install -qU langchain-openai

    import getpassimport osos.environ["TOGETHER_API_KEY"] = getpass.getpass()from langchain_openai import ChatOpenAImodel = ChatOpenAI(    base_url="https://api.together.xyz/v1",    api_key=os.environ["TOGETHER_API_KEY"],    model="mistralai/Mixtral-8x7B-Instruct-v0.1",)

tip

This tutorial is meant to be simple, but generally should really include reference examples to squeeze out performance!

Using PydanticOutputParser[​](#using-pydanticoutputparser "Direct link to Using PydanticOutputParser")
------------------------------------------------------------------------------------------------------

The following example uses the built-in `PydanticOutputParser` to parse the output of a chat model.

    from typing import List, Optionalfrom langchain_core.output_parsers import PydanticOutputParserfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.pydantic_v1 import BaseModel, Field, validatorclass Person(BaseModel):    """Information about a person."""    name: str = Field(..., description="The name of the person")    height_in_meters: float = Field(        ..., description="The height of the person expressed in meters."    )class People(BaseModel):    """Identifying information about all people in a text."""    people: List[Person]# Set up a parserparser = PydanticOutputParser(pydantic_object=People)# Promptprompt = ChatPromptTemplate.from_messages(    [        (            "system",            "Answer the user query. Wrap the output in `json` tags\n{format_instructions}",        ),        ("human", "{query}"),    ]).partial(format_instructions=parser.get_format_instructions())

**API Reference:**[PydanticOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.pydantic.PydanticOutputParser.html) | [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html)

Let's take a look at what information is sent to the model

    query = "Anna is 23 years old and she is 6 feet tall"

    print(prompt.format_prompt(query=query).to_string())

    System: Answer the user query. Wrap the output in `json` tagsThe output should be formatted as a JSON instance that conforms to the JSON schema below.As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.Here is the output schema:

{"description": "Identifying information about all people in a text.", "properties": {"people": {"title": "People", "type": "array", "items": {"$ref": "#/definitions/Person"}}}, "required": \["people"\], "definitions": {"Person": {"title": "Person", "description": "Information about a person.", "type": "object", "properties": {"name": {"title": "Name", "description": "The name of the person", "type": "string"}, "height\_in\_meters": {"title": "Height In Meters", "description": "The height of the person expressed in meters.", "type": "number"}}, "required": \["name", "height\_in\_meters"\]}}}

    Human: Anna is 23 years old and she is 6 feet tall

Having defined our prompt, we simply chain together the prompt, model and output parser:

    chain = prompt | model | parserchain.invoke({"query": query})

    People(people=[Person(name='Anna', height_in_meters=1.83)])

Check out the associated [Langsmith trace](https://smith.langchain.com/public/92ed52a3-92b9-45af-a663-0a9c00e5e396/r).

Note that the schema shows up in two places:

1.  In the prompt, via `parser.get_format_instructions()`;
2.  In the chain, to receive the formatted output and structure it into a Python object (in this case, the Pydantic object `People`).

Custom Parsing[​](#custom-parsing "Direct link to Custom Parsing")
------------------------------------------------------------------

If desired, it's easy to create a custom prompt and parser with `LangChain` and `LCEL`.

To create a custom parser, define a function to parse the output from the model (typically an [AIMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.ai.AIMessage.html)) into an object of your choice.

See below for a simple implementation of a JSON parser.

    import jsonimport refrom typing import List, Optionalfrom langchain_anthropic.chat_models import ChatAnthropicfrom langchain_core.messages import AIMessagefrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.pydantic_v1 import BaseModel, Field, validatorclass Person(BaseModel):    """Information about a person."""    name: str = Field(..., description="The name of the person")    height_in_meters: float = Field(        ..., description="The height of the person expressed in meters."    )class People(BaseModel):    """Identifying information about all people in a text."""    people: List[Person]# Promptprompt = ChatPromptTemplate.from_messages(    [        (            "system",            "Answer the user query. Output your answer as JSON that  "            "matches the given schema: ```json\n{schema}\n```. "            "Make sure to wrap the answer in ```json and ``` tags",        ),        ("human", "{query}"),    ]).partial(schema=People.schema())# Custom parserdef extract_json(message: AIMessage) -> List[dict]:    """Extracts JSON content from a string where JSON is embedded between ```json and ``` tags.    Parameters:        text (str): The text containing the JSON content.    Returns:        list: A list of extracted JSON strings.    """    text = message.content    # Define the regular expression pattern to match JSON blocks    pattern = r"```json(.*?)```"    # Find all non-overlapping matches of the pattern in the string    matches = re.findall(pattern, text, re.DOTALL)    # Return the list of matched JSON strings, stripping any leading or trailing whitespace    try:        return [json.loads(match.strip()) for match in matches]    except Exception:        raise ValueError(f"Failed to parse: {message}")

**API Reference:**[ChatAnthropic](https://api.python.langchain.com/en/latest/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html) | [AIMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.ai.AIMessage.html) | [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html)

    query = "Anna is 23 years old and she is 6 feet tall"print(prompt.format_prompt(query=query).to_string())

    System: Answer the user query. Output your answer as JSON that  matches the given schema: ```json{'title': 'People', 'description': 'Identifying information about all people in a text.', 'type': 'object', 'properties': {'people': {'title': 'People', 'type': 'array', 'items': {'$ref': '#/definitions/Person'}}}, 'required': ['people'], 'definitions': {'Person': {'title': 'Person', 'description': 'Information about a person.', 'type': 'object', 'properties': {'name': {'title': 'Name', 'description': 'The name of the person', 'type': 'string'}, 'height_in_meters': {'title': 'Height In Meters', 'description': 'The height of the person expressed in meters.', 'type': 'number'}}, 'required': ['name', 'height_in_meters']}}}```. Make sure to wrap the answer in ```json and ``` tagsHuman: Anna is 23 years old and she is 6 feet tall

    chain = prompt | model | extract_jsonchain.invoke({"query": query})

    [{'people': [{'name': 'Anna', 'height_in_meters': 1.83}]}]

Other Libraries[​](#other-libraries "Direct link to Other Libraries")
---------------------------------------------------------------------

If you're looking at extracting using a parsing approach, check out the [Kor](https://eyurtsev.github.io/kor/) library. It's written by one of the `LangChain` maintainers and it helps to craft a prompt that takes examples into account, allows controlling formats (e.g., JSON or CSV) and expresses the schema in TypeScript. It seems to work pretty!

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/extraction_parse.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to handle long text when doing extraction

](/v0.2/docs/how_to/extraction_long_text/)[

Next

How to add fallbacks to a runnable

](/v0.2/docs/how_to/fallbacks/)

*   [Using PydanticOutputParser](#using-pydanticoutputparser)
*   [Custom Parsing](#custom-parsing)
*   [Other Libraries](#other-libraries)