langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.71k stars 2.19k forks source link

Fix structured parser with triple backticks #7199

Open johnguirgis opened 3 days ago

johnguirgis commented 3 days ago

Fixes #6734

Corrects an issue where the output parser naïvely attempts to parse contents of triple backticks as JSON.

  1. Output parser falls back to parse the entire string when extracting from within triple backticks fails
  2. Adds tests to account for different backtick positions in JSONs
vercel[bot] commented 3 days ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Nov 15, 2024 3:07am
1 Skipped Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **langchainjs-api-refs** | ⬜️ Ignored ([Inspect](https://vercel.com/langchain/langchainjs-api-refs/9pcG5LgpE48HpAsHUc6nepmim5ho)) | | | Nov 15, 2024 3:07am |
harrisbchong commented 3 days ago

To maintain backwards compatibility, we modified our solution attempt at #6734 to leave the original parsing and behaviour in the first try block. Instead, we move our new parsing code inside the catch block, ensuring it executes only when the original code fails. The logic within the new try block is as follows: we know parsing the contents within the brace has failed, meaning that the only hope is to parse the string as whole (while trimming the leading or trailing whitespace).