Skip to content

Commit 60bac10

Browse files
committed
fix: accept JSON schemas in structured output parser
1 parent 87655f8 commit 60bac10

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

scrapegraphai/utils/output_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from typing import Any, Callable, Dict, List, Type, Union
66

77
from langchain_core.exceptions import OutputParserException
8-
from langchain_core.outputs import Generation
98
from langchain_core.output_parsers import JsonOutputParser
9+
from langchain_core.outputs import Generation
1010
from pydantic import BaseModel as BaseModelV2
1111
from pydantic.v1 import BaseModel as BaseModelV1
1212

@@ -58,6 +58,9 @@ def get_structured_output_parser(
5858
Returns:
5959
Callable: The output parser function.
6060
"""
61+
if isinstance(schema, dict):
62+
return _dict_output_parser
63+
6164
if issubclass(schema, BaseModelV1):
6265
return _base_model_v1_output_parser
6366

tests/utils/output_parser_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from scrapegraphai.utils.output_parser import (
66
TolerantJsonOutputParser,
77
_strip_doubled_braces,
8+
get_structured_output_parser,
89
)
910

1011

@@ -22,6 +23,19 @@ def test_strip_doubled_braces_ignores_unbalanced():
2223
assert _strip_doubled_braces(text) == text
2324

2425

26+
def test_structured_output_parser_accepts_json_schema():
27+
schema = {
28+
"title": "Person",
29+
"type": "object",
30+
"properties": {"name": {"type": "string"}},
31+
}
32+
output = {"name": "Ada"}
33+
34+
parser = get_structured_output_parser(schema)
35+
36+
assert parser(output) == output
37+
38+
2539
def test_tolerant_parser_parses_clean_json_unchanged():
2640
parser = TolerantJsonOutputParser()
2741
assert parser.parse('{"content": "hi"}') == {"content": "hi"}

0 commit comments

Comments
 (0)