mirror of
https://github.com/hwchase17/langchain.git
synced 2026-07-13 20:47:15 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user