Skip to content

Commit

Permalink
Update parsePartialJSON to handle trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Mar 21, 2024
1 parent 62d1098 commit 783c036
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 34 additions & 0 deletions packages/parsing/src/as-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ Here is the JSON output for the "About Us" page based on the provided props:
});
});

it('should handle JSON with trailing comma-separators', () => {
const response = `\
{
"sectionTitle": "Meet the Team",
"item1Content": "Tom Ryder - Owner & Wine Director",
"item1AttributionLine1": "Learn more about Tom's passion for wine and his journey to opening Ryders",
"item1AttributionLine2": "Read about Tom's experience in the wine industry and his approach to curating Ryders' wine list",
"item2Content": "Sarah Johnson - Wine Educator",
"item2AttributionLine1": "Discover Sarah's background in wine and her role in educating customers at Ryders",
"item2AttributionLine2": "Learn about Sarah's favorite wine pairings and her recommendations for beginners",
"item3Content": "Mike Smith - Cheese Specialist",
"item3AttributionLine1": "Find out more about Mike's expertise in cheese and his role in curating Ryders' selection",
"item3AttributionLine2": "Read about Mike's favorite cheese pairings and his recommendations for unique flavor combinations",
}`;
expect(asJSON(response)).toEqual({
item1AttributionLine1:
"Learn more about Tom's passion for wine and his journey to opening Ryders",
item1AttributionLine2:
"Read about Tom's experience in the wine industry and his approach to curating Ryders' wine list",
item1Content: 'Tom Ryder - Owner & Wine Director',
item2AttributionLine1:
"Discover Sarah's background in wine and her role in educating customers at Ryders",
item2AttributionLine2:
"Learn about Sarah's favorite wine pairings and her recommendations for beginners",
item2Content: 'Sarah Johnson - Wine Educator',
item3AttributionLine1:
"Find out more about Mike's expertise in cheese and his role in curating Ryders' selection",
item3AttributionLine2:
"Read about Mike's favorite cheese pairings and his recommendations for unique flavor combinations",
item3Content: 'Mike Smith - Cheese Specialist',
sectionTitle: 'Meet the Team',
});
});

it('should strip invalid JSON when the LLM response goes off the rails', () => {
const response = `\
Here is the JSON output for the "Meet the Team" page:
Expand Down
11 changes: 8 additions & 3 deletions packages/parsing/src/as-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,27 @@ export const parsePartialJSON = (text: string) => {
stack.pop();
} else {
// Mismatched closing character; the input is malformed.
// If this is the last character in the text, remove it, else return null.
// If this is the last character in the text, just remove it.
// Otherwise, return null.
if (index === text.length - 1) {
console.log('mismatched closing character', char);
char = '';
} else {
return null;
}
}
} else if (char === ',') {
// If this is a trailing comma, remove it.
if (/^[^"]*[\]}]/.test(text.slice(index + 1))) {
char = '';
}
}
}

// Append the processed character to the new string.
newText += char;
}

// If we're still inside a string at the end of processing,
// If were still inside a string at the end of processing,
// we need to close the string.
if (isInsideString) {
newText += '"';
Expand Down

0 comments on commit 783c036

Please sign in to comment.