mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
bugfix: throw exception if structured output parser doesn't get what it wants (#3044)
allows the user to catch the issue and handle it rather than failing hard. This happens more than you'd expect when using output parsers with chatgpt, especially if the temp is anything but 0. Sometimes it doesn't want to listen and just does its own thing.
This commit is contained in:
parent
afd3e70ae5
commit
dcdcd3f636
@ -38,6 +38,12 @@ class StructuredOutputParser(BaseOutputParser):
|
||||
return STRUCTURED_FORMAT_INSTRUCTIONS.format(format=schema_str)
|
||||
|
||||
def parse(self, text: str) -> Any:
|
||||
if "```json" not in text:
|
||||
raise OutputParserException(
|
||||
f"Got invalid return object. Expected markdown code snippet with JSON "
|
||||
f"object, but got:\n{text}"
|
||||
)
|
||||
|
||||
json_string = text.split("```json")[1].strip().strip("```").strip()
|
||||
try:
|
||||
json_obj = json.loads(json_string)
|
||||
|
Loading…
Reference in New Issue
Block a user