Skip to content

The agent engineering platform.

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

Tze Min6ef718c5f460
diff --git a/libs/core/langchain_core/output_parsers/json.py b/libs/core/langchain_core/output_parsers/json.py
index 4f41fb254b..e644d6c870 100644
--- a/libs/core/langchain_core/output_parsers/json.py
+++ b/libs/core/langchain_core/output_parsers/json.py
@@ -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:
diff --git a/libs/core/tests/unit_tests/output_parsers/test_json.py b/libs/core/tests/unit_tests/output_parsers/test_json.py
index 3f8ee573b1..5559768bb0 100644
--- a/libs/core/tests/unit_tests/output_parsers/test_json.py
+++ b/libs/core/tests/unit_tests/output_parsers/test_json.py
@@ -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,
]