Suggested diff for guardrails error handling

- ValidationError is now a subclass of Exception
- Each OutputParser can now declare its own subclass of ValidationError that people can reference in a catch statement
This commit is contained in:
Nuno Campos
2023-03-17 09:23:59 +00:00
parent e61e37a40a
commit 21ea1866de
3 changed files with 17 additions and 2 deletions

View File

@@ -11,6 +11,12 @@ from langchain.schema import Fixer, Guardrail, PromptValue, ValidationError
class BaseOutputParser(BaseModel, ABC):
"""Class to parse the output of an LLM call."""
Exception: ValidationError = ValidationError
@property
def Exception(self) -> ValidationError:
return self.__class__.Exception
@abstractmethod
def parse(self, text: str) -> Any:
"""Parse the output of an LLM call."""
@@ -41,7 +47,7 @@ class OutputGuardrail(Guardrail, BaseModel):
self.output_parser.parse(result)
return None
except Exception as e:
return ValidationError(text=e)
return self.output_parser.Exception(text=e)
def fix(
self, prompt_value: PromptValue, result: Any, error: ValidationError

View File

@@ -7,6 +7,7 @@ from pydantic import BaseModel
from langchain.output_parsers.base import BaseOutputParser
from langchain.output_parsers.format_instructions import STRUCTURED_FORMAT_INSTRUCTIONS
from langchain.schema import ValidationError
line_template = '\t"{name}": {type} // {description}'
@@ -22,7 +23,13 @@ def _get_sub_string(schema: ResponseSchema) -> str:
)
class StructuredOutputParserException(ValidationError):
pass
class StructuredOutputParser(BaseOutputParser):
Exception = StructuredOutputParserException
response_schemas: List[ResponseSchema]
@classmethod

View File

@@ -5,6 +5,7 @@ from abc import ABC, abstractmethod
from typing import Any, Dict, List, NamedTuple, Optional
from pydantic import BaseModel, Extra, Field, root_validator
from pydantic.dataclasses import dataclass
class AgentAction(NamedTuple):
@@ -222,7 +223,8 @@ class BaseMemory(BaseModel, ABC):
Memory = BaseMemory
class ValidationError(BaseModel):
@dataclass
class ValidationError(Exception):
error_message: str