Skip to content

Commit

Permalink
Handle function arguments parsing errors (langchain-ai#1893)
Browse files Browse the repository at this point in the history
* Handle function arguments parsing errors

This PR introduces error handling for function arguments parsing in the `parseOutput` function.

**Changes**:

- A try-catch block has been added around the parsing of `function_call.arguments` with `JSON.parse` in the `parseOutput` function.
- If an error occurs during parsing, an error message is thrown. This message includes the text that failed to parse and the original error message.

**Rationale**:

The previous code did not handle errors when parsing `function_call.arguments`. This could lead to unclear feedback to the user when incorrect argument formats were provided. This change addresses this issue and provides clearer feedback when an error occurs.

* Use an OutputParserException for output errors

* Update error message

---------

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
ywkim and jacoblee93 authored Jul 13, 2023
1 parent 37dfc77 commit 6986e03
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions langchain/src/agents/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@ import {
} from "../../prompts/chat.js";
import { BaseLanguageModel } from "../../base_language/index.js";
import { LLMChain } from "../../chains/llm_chain.js";
import { OutputParserException } from "../../schema/output_parser.js";

function parseOutput(message: BaseMessage): AgentAction | AgentFinish {
if (message.additional_kwargs.function_call) {
// eslint-disable-next-line prefer-destructuring
const function_call: ChatCompletionRequestMessageFunctionCall =
message.additional_kwargs.function_call;
return {
tool: function_call.name as string,
toolInput: function_call.arguments
try {
const toolInput = function_call.arguments
? JSON.parse(function_call.arguments)
: {},
log: message.content,
};
: {};
return {
tool: function_call.name as string,
toolInput,
log: message.content,
};
} catch (error) {
throw new OutputParserException(
`Failed to parse function arguments from chat model response. Text: "${function_call.arguments}". ${error}`
);
}
} else {
return { returnValues: { output: message.content }, log: message.content };
}
Expand Down

0 comments on commit 6986e03

Please sign in to comment.