Skip to content

Commit

Permalink
Fix parsing failures whtn ChatGPT returns JSON with trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
faceleg committed Apr 18, 2024
1 parent d35918a commit 7c68374
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 38 additions & 0 deletions typescript/src/ts/strip-whitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* The following code snippet is copied from the index.ts file of the strip-json-trailing-commas project
* (commit 3cfa16a2aa6bbef8ed740c346321ef6c2b42237d),
* available at: https://github.com/nokazn/strip-json-trailing-commas/blob/3cfa16a2aa6bbef8ed740c346321ef6c2b42237d/src/index.ts
*
* The original code is licensed under the MIT License:
*
* MIT License
*
* Copyright 2021 https://github.com/nokazn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

export default function stripJsonTrailingCommas(content: string): string {
/**
* preceded by number or string or boolean (true/false) or null or '}' or ']' (and with 0 or more spaces)
* match with ','
* followed by '}' or ']'
*/
return content.replace(/(?<=(true|false|null|["\d}\]])\s*),(?=\s*[}\]])/g, '');
}
3 changes: 2 additions & 1 deletion typescript/src/typechat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Result, success, error } from "./result";
import { TypeChatLanguageModel, PromptSection } from "./model";
import stripJsonTrailingCommas from "./ts/strip-whitespace";

/**
* Represents an object that can translate natural language requests in JSON objects of the given type.
Expand Down Expand Up @@ -136,7 +137,7 @@ export function createJsonTranslator<T extends object>(model: TypeChatLanguageMo
if (!(startIndex >= 0 && endIndex > startIndex)) {
return error(`Response is not JSON:\n${responseText}`);
}
const jsonText = responseText.slice(startIndex, endIndex + 1);
const jsonText = stripJsonTrailingCommas(responseText.slice(startIndex, endIndex + 1));
let jsonObject;
try {
jsonObject = JSON.parse(jsonText) as object;
Expand Down

0 comments on commit 7c68374

Please sign in to comment.