From 201ea565f329c04c021f3fb63d3e0f0363e8ad22 Mon Sep 17 00:00:00 2001 From: Shreya Rajpal Date: Mon, 4 Mar 2024 23:46:11 -0800 Subject: [PATCH] fix bug in pydantic type casting for union of lists and dicts --- guardrails/classes/output_type.py | 2 +- guardrails/classes/validation_outcome.py | 4 ++++ guardrails/utils/reask_utils.py | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/guardrails/classes/output_type.py b/guardrails/classes/output_type.py index f40714047..2aff9fc82 100644 --- a/guardrails/classes/output_type.py +++ b/guardrails/classes/output_type.py @@ -1,3 +1,3 @@ from typing import Dict, List, TypeVar -OT = TypeVar("OT", str, Dict, List) +OT = TypeVar("OT", str, List, Dict) diff --git a/guardrails/classes/validation_outcome.py b/guardrails/classes/validation_outcome.py index 4a7586e8b..4f9df16ae 100644 --- a/guardrails/classes/validation_outcome.py +++ b/guardrails/classes/validation_outcome.py @@ -1,6 +1,7 @@ from typing import Generic, Iterator, Optional, Tuple, Union, cast from pydantic import Field +from rich.pretty import pretty_repr from guardrails.classes.history import Call, Iteration from guardrails.classes.output_type import OT @@ -83,3 +84,6 @@ def __iter__( def __getitem__(self, keys): """Get a subset of the ValidationOutcome's fields.""" return iter(getattr(self, k) for k in keys) + + def __str__(self) -> str: + return pretty_repr(self) diff --git a/guardrails/utils/reask_utils.py b/guardrails/utils/reask_utils.py index c42aba16c..d6a1a4faf 100644 --- a/guardrails/utils/reask_utils.py +++ b/guardrails/utils/reask_utils.py @@ -82,6 +82,10 @@ def _gather_reasks_in_list( valid_output = deepcopy(validated_output) _gather_reasks_in_dict(validated_output, valid_output) return reasks, valid_output + elif isinstance(validated_output, List): + valid_output = deepcopy(validated_output) + _gather_reasks_in_list(validated_output, valid_output) + return reasks, valid_output return reasks, None