!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

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
        *   [Quickstart](/v0.1/docs/use_cases/tool_use/quickstart/)
        *   [Agents](/v0.1/docs/use_cases/tool_use/agents/)
        *   [Choosing between multiple tools](/v0.1/docs/use_cases/tool_use/multiple_tools/)
        *   [Parallel tool use](/v0.1/docs/use_cases/tool_use/parallel/)
        *   [Tool error handling](/v0.1/docs/use_cases/tool_use/tool_error_handling/)
        *   [Human-in-the-loop](/v0.1/docs/use_cases/tool_use/human_in_the_loop/)
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   [Tool use](/v0.1/docs/use_cases/tool_use/)
*   Tool error handling

On this page

Tool error handling
===================

Using a model to invoke a tool has some obvious potential failure modes. Firstly, the model needs to return a output that can be parsed at all. Secondly, the model needs to return tool arguments that are valid.

We can build error handling into our chains to mitigate these failure modes.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

We’ll use OpenAI for this guide, and will need to install its partner package:

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

You'll need to sign up for an OpenAI key and set it as an environment variable named `OPENAI_API_KEY`.

We'll also use the popular validation library [Zod](https://zod.dev) to define our tool schemas. It's already a dependency of `langchain`, but you can install it explicitly like this too:

*   npm
*   Yarn
*   pnpm

    npm install zod

    yarn add zod

    pnpm add zod

Chain[​](#chain "Direct link to Chain")
---------------------------------------

Suppose we have the following (dummy) tool and tool-calling chain. We’ll make our tool intentionally convoluted to try and trip up the model.

    import { z } from "zod";import { DynamicStructuredTool } from "@langchain/core/tools";const complexTool = new DynamicStructuredTool({  name: "complex_tool",  description: "Do something complex with a complex tool.",  schema: z.object({    intArg: z.number(),    intArg2: z.number(),    dictArg: z.object({      test: z.object({}),    }),  }),  func: async ({ intArg, intArg2, dictArg }) => {    // Unused for demo purposes    console.log(dictArg);    return (intArg * intArg2).toString();  },});import { ChatOpenAI } from "@langchain/openai";const model = new ChatOpenAI({  model: "gpt-3.5-turbo-1106",  temperature: 0,});import { convertToOpenAITool } from "@langchain/core/utils/function_calling";const formattedTools = [convertToOpenAITool(complexTool)];const modelWithTools = model.bind({  tools: formattedTools,  // We specify tool_choice to enforce that the 'multiply' function is called by the model.  tool_choice: {    type: "function",    function: { name: "complex_tool" },  },});import { JsonOutputKeyToolsParser } from "langchain/output_parsers";import { RunnableSequence } from "@langchain/core/runnables";const chain = RunnableSequence.from([  modelWithTools,  new JsonOutputKeyToolsParser({ keyName: "complex_tool", returnSingle: true }),  complexTool,]);

We can see that when we try to invoke this chain with certain inputs, the model fails to correctly call the tool (it fails to provide a valid object to the `dictArg` parameter, and instead passes `potato`).

    await chain.invoke("use complex tool. the args are 5, 2.1, potato.");

    ToolInputParsingException [Error]: Received tool input did not match expected schema    at DynamicStructuredTool.call (file:///Users/jacoblee/langchain/langchainjs/langchain-core/dist/tools.js:63:19)    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)    at async RunnableSequence.invoke (file:///Users/jacoblee/langchain/langchainjs/langchain-core/dist/runnables/base.js:818:27)    at <anonymous> (/Users/jacoblee/langchain/langchainjs/examples/src/use_cases/tool_use/tool_error_handling_intro.ts:50:3) {  output: '{"intArg":5,"floatArg":2.1,"dictArg":"potato"}'}

Fallbacks[​](#fallbacks "Direct link to Fallbacks")
---------------------------------------------------

One way to solve this is to fallback to a better model in the event of a tool invocation error. In this case we’ll fall back to an identical chain that uses `gpt-4-1106-preview` instead of `gpt-3.5-turbo`.

    const betterModel = new ChatOpenAI({  model: "gpt-4-1106-preview",  temperature: 0,}).bind({  tools: formattedTools,  // We specify tool_choice to enforce that the 'multiply' function is called by the model.  tool_choice: {    type: "function",    function: { name: "complex_tool" },  },});const betterChain = RunnableSequence.from([  betterModel,  new JsonOutputKeyToolsParser({ keyName: "complex_tool", returnSingle: true }),  complexTool,]);const chainWithFallback = chain.withFallbacks({  fallbacks: [betterChain],});

info

You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/a1f0cac4-4b59-4d0e-8612-44a2e4b51c75/r)

    await chainWithFallback.invoke(  "use complex tool. the args are 5, 2.1, potato.");

    10.5

Looking at the [Langsmith trace](https://smith.langchain.com/public/a1f0cac4-4b59-4d0e-8612-44a2e4b51c75/r) for this chain run, we can see that the first chain call fails as expected and it’s the fallback that succeeds.

* * *

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

[

Previous

Parallel tool use

](/v0.1/docs/use_cases/tool_use/parallel/)[

Next

Human-in-the-loop

](/v0.1/docs/use_cases/tool_use/human_in_the_loop/)

*   [Setup](#setup)
*   [Chain](#chain)
*   [Fallbacks](#fallbacks)

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.