Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: fix Anthropic json issue in streaming #16670

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/core/langchain_core/output_parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
41 changes: 41 additions & 0 deletions libs/core/tests/unit_tests/output_parsers/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@
```
This should do the trick"""

ANTHROPIC_STREAMING_WITHOUT_END_BRACKET = """Here is a response formatted as schema:
hwchase17 marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"foo": "bar"


"""

ANTHROPIC_STREAMING_WITH_END_BRACKET = """Here is a response formatted as schema:
hwchase17 marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"foo": "bar"
}

"""

ANTHROPIC_STREAMING_WITH_END_TICK = """Here is a response formatted as schema:
hwchase17 marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"foo": "bar"
}
```
"""

ANTHROPIC_STREAMING_WITH_END_TEXT = """Here is a response formatted as schema:
hwchase17 marked this conversation as resolved.
Show resolved Hide resolved

```
{
"foo": "bar"

```
This should do the trick
"""

TEST_CASES = [
GOOD_JSON,
JSON_WITH_NEW_LINES,
Expand All @@ -148,6 +185,10 @@
TEXT_BEFORE,
TEXT_AFTER,
TEXT_BEFORE_AND_AFTER,
ANTHROPIC_STREAMING_WITHOUT_END_BRACKET,
ANTHROPIC_STREAMING_WITH_END_BRACKET,
ANTHROPIC_STREAMING_WITH_END_TICK,
ANTHROPIC_STREAMING_WITH_END_TEXT,
hwchase17 marked this conversation as resolved.
Show resolved Hide resolved
]


Expand Down
Loading