!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
        *   [Quickstart](/v0.1/docs/modules/model_io/quick_start/)
        *   [Concepts](/v0.1/docs/modules/model_io/concepts/)
        *   [Prompts](/v0.1/docs/modules/model_io/prompts/)
            
        *   [LLMs](/v0.1/docs/modules/model_io/llms/)
            
        *   [Chat Models](/v0.1/docs/modules/model_io/chat/)
            
        *   [Model I/O](/v0.1/docs/modules/model_io/)
        *   [Output Parsers](/v0.1/docs/modules/model_io/output_parsers/)
            
            *   [Quick Start](/v0.1/docs/modules/model_io/output_parsers/quick_start/)
            *   [Custom output parsers](/v0.1/docs/modules/model_io/output_parsers/custom/)
            *   [Output Parser Types](/v0.1/docs/modules/model_io/output_parsers/types/)
                
                *   [String output parser](/v0.1/docs/modules/model_io/output_parsers/types/string/)
                *   [HTTP Response Output Parser](/v0.1/docs/modules/model_io/output_parsers/types/http_response/)
                *   [JSON Output Functions Parser](/v0.1/docs/modules/model_io/output_parsers/types/json_functions/)
                *   [Bytes output parser](/v0.1/docs/modules/model_io/output_parsers/types/bytes/)
                *   [Combining output parsers](/v0.1/docs/modules/model_io/output_parsers/types/combining_output_parser/)
                *   [List parser](/v0.1/docs/modules/model_io/output_parsers/types/csv/)
                *   [Custom list parser](/v0.1/docs/modules/model_io/output_parsers/types/custom_list_parser/)
                *   [Datetime parser](/v0.1/docs/modules/model_io/output_parsers/types/datetime/)
                *   [OpenAI Tools](/v0.1/docs/modules/model_io/output_parsers/types/openai_tools/)
                *   [Auto-fixing parser](/v0.1/docs/modules/model_io/output_parsers/types/output_fixing/)
                *   [Structured output parser](/v0.1/docs/modules/model_io/output_parsers/types/structured/)
                *   [XML output parser](/v0.1/docs/modules/model_io/output_parsers/types/xml/)
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Modules](/v0.1/docs/modules/)
*   [Model I/O](/v0.1/docs/modules/model_io/)
*   [Output Parsers](/v0.1/docs/modules/model_io/output_parsers/)
*   [Output Parser Types](/v0.1/docs/modules/model_io/output_parsers/types/)
*   OpenAI Tools

On this page

OpenAI Tools
============

These output parsers extract tool calls from OpenAI’s function calling API responses. This means they are only usable with models that support function calling, and specifically the latest `tools` and `tool_choice` parameters. We recommend familiarizing yourself with [function calling](/v0.1/docs/modules/model_io/chat/function_calling/) before reading this guide.

There are a few different variants of output parsers:

*   [`JsonOutputToolsParser`](https://api.js.langchain.com/classes/langchain_output_parsers.JsonOutputToolsParser.html): Returns the arguments of the function call as JSON
*   [`JsonOutputKeyToolsParser`](https://api.js.langchain.com/classes/langchain_output_parsers.JsonOutputKeyToolsParser.html): Returns the value of specific key in the function call as JSON

    import { ChatPromptTemplate } from "@langchain/core/prompts";import { ChatOpenAI } from "@langchain/openai";

    const properties = {  setup: {    type: "string",    description: "The setup for the joke",  },  punchline: {    type: "string",    description: "The joke's punchline",  },};const tool = {  type: "function" as const,  function: {    name: "joke",    description: "Joke to tell user.",    parameters: {      $schema: "http://json-schema.org/draft-07/schema#",      title: "Joke",      type: "object",      properties,      required: ["setup", "punchline"],    },  },};

    import { ChatPromptTemplate } from "@langchain/core/prompts";const llm = new ChatOpenAI();// Use `.bind` to attach the tool to the modelconst llmWithTools = llm.bind({  tools: [tool],  // Optionally, we can pass the tool to the `tool_choice` parameter to  // force the model to call the tool.  tool_choice: tool,});const prompt = ChatPromptTemplate.fromMessages([  [    "system",    "You are the funniest comedian, tell the user a joke about their topic.",  ],  ["human", "Topic: {topic}"],]);

Now we can use LCEL to pipe our prompt and LLM together.

    const chain = prompt.pipe(llmWithTools);

    const result = await chain.invoke({ topic: "Large Language Models" });

    result.additional_kwargs;

    {  function_call: undefined,  tool_calls: [    {      id: "call_vo9oYcHXKWzS6bJ4bK7Eghmz",      type: "function",      function: {        name: "joke",        arguments: "{\n" +          '  "setup": "Why did the large language model go on a diet?",\n' +          '  "punchline": "It wanted to reduce i'... 17 more characters      }    }  ]}

> #### Inspect the [LangSmith trace](https://smith.langchain.com/public/f2f34c8d-8193-40cb-b3ef-f186fb4de73e/r) from the call above[​](#inspect-the-langsmith-trace-from-the-call-above "Direct link to inspect-the-langsmith-trace-from-the-call-above")

`JsonOutputToolsParser`[​](#jsonoutputtoolsparser "Direct link to jsonoutputtoolsparser")
-----------------------------------------------------------------------------------------

    import { JsonOutputToolsParser } from "langchain/output_parsers";const outputParser = new JsonOutputToolsParser();

    const chain = prompt.pipe(llmWithTools).pipe(outputParser);

    await chain.invoke({ topic: "Large Language Models" });

    [  {    type: "joke",    args: {      setup: "Why did the large language model go to therapy?",      punchline: "It had too many layers!"    }  }]

> #### Inspect the [LangSmith trace](https://smith.langchain.com/public/61ce7b9f-d462-499e-be65-8a165d2b47a7/r) with the `JsonOutputToolsParser`[​](#inspect-the-langsmith-trace-with-the-jsonoutputtoolsparser "Direct link to inspect-the-langsmith-trace-with-the-jsonoutputtoolsparser")

`JsonOutputKeyToolsParser`[​](#jsonoutputkeytoolsparser "Direct link to jsonoutputkeytoolsparser")
--------------------------------------------------------------------------------------------------

This merely extracts a single key from the returned response. This is useful for when you are passing in a single tool and just want it’s arguments.

    import { JsonOutputKeyToolsParser } from "langchain/output_parsers";const outputParser = new JsonOutputKeyToolsParser({ keyName: "joke" });

    const chain = prompt.pipe(llmWithTools).pipe(outputParser);

    await chain.invoke({ topic: "Large Language Models" });

    [  {    setup: "Why did the large language model go to therapy?",    punchline: "It had too many layers!"  }]

> #### Inspect the [LangSmith trace](https://smith.langchain.com/public/2c9c93d2-d789-4e45-9f9f-e942eace8aed/r) with the `JsonOutputKeyToolsParser`[​](#inspect-the-langsmith-trace-with-the-jsonoutputkeytoolsparser "Direct link to inspect-the-langsmith-trace-with-the-jsonoutputkeytoolsparser")

Some LLMs have support for calling multiple tools in a single response. Because of this, the result of invoking `JsonOutputKeyToolsParser` is always an array. If you would only like a single result to be returned, you can specify `returnSingle` in the constructor.

    const outputParserSingle = new JsonOutputKeyToolsParser({  keyName: "joke",  returnSingle: true,});

    const chain = prompt.pipe(llmWithTools);

    const response = await chain.invoke({ topic: "Large Language Models" });

    await outputParserSingle.invoke(response);

    {  setup: "Why did the large language model go on a diet?",  punchline: "It wanted to shed some excess bytes!"}

> #### See the [LangSmith trace](https://smith.langchain.com/public/c05e0409-8085-487d-aee2-2d42b64b9f6d/r) from this output parser.[​](#see-the-langsmith-trace-from-this-output-parser. "Direct link to see-the-langsmith-trace-from-this-output-parser.")

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Datetime parser

](/v0.1/docs/modules/model_io/output_parsers/types/datetime/)[

Next

Auto-fixing parser

](/v0.1/docs/modules/model_io/output_parsers/types/output_fixing/)

*   [`JsonOutputToolsParser`](#jsonoutputtoolsparser)
*   [`JsonOutputKeyToolsParser`](#jsonoutputkeytoolsparser)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.