Core: fix Anthropic json issue in streaming (#16670)

**Description:** fix ChatAnthropic json issue in streaming 
**Issue:** https://github.com/langchain-ai/langchain/issues/16423
**Dependencies:** n/a

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
Tze Min 2024-01-29 08:41:17 +08:00 committed by GitHub
parent e451c8adc1
commit 6ef718c5f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View File

@ -138,7 +138,7 @@ def parse_json_markdown(
The parsed JSON object as a Python dictionary.
"""
# Try to find JSON string within triple backticks
match = re.search(r"```(json)?(.*)```", json_string, re.DOTALL)
match = re.search(r"```(json)?(.*)(```)?", json_string, re.DOTALL)
# If no match found, assume the entire string is a JSON string
if match is None:

View File

@ -137,6 +137,43 @@ TEXT_BEFORE_AND_AFTER = """Action: Testing
```
This should do the trick"""
WITHOUT_END_BRACKET = """Here is a response formatted as schema:
```json
{
"foo": "bar"
"""
WITH_END_BRACKET = """Here is a response formatted as schema:
```json
{
"foo": "bar"
}
"""
WITH_END_TICK = """Here is a response formatted as schema:
```json
{
"foo": "bar"
}
```
"""
WITH_END_TEXT = """Here is a response formatted as schema:
```
{
"foo": "bar"
```
This should do the trick
"""
TEST_CASES = [
GOOD_JSON,
JSON_WITH_NEW_LINES,
@ -148,6 +185,10 @@ TEST_CASES = [
TEXT_BEFORE,
TEXT_AFTER,
TEXT_BEFORE_AND_AFTER,
WITHOUT_END_BRACKET,
WITH_END_BRACKET,
WITH_END_TICK,
WITH_END_TEXT,
]