11from typing import Any , Dict , Generator , Iterable , List , Optional , Tuple , Union , cast
22
3+
34from guardrails import validator_service
45from guardrails .classes .history import Call , Inputs , Iteration , Outputs
56from guardrails .classes .output_type import OT , OutputTypes
7+ from guardrails .classes .validation .validation_summary import ValidationSummary
68from guardrails .classes .validation_outcome import ValidationOutcome
79from guardrails .llm_providers import (
810 LiteLLMCallable ,
@@ -176,7 +178,9 @@ def prepare_chunk_generator(stream) -> Iterable[Tuple[Any, bool]]:
176178 "$" ,
177179 validate_subschema = True ,
178180 )
179-
181+ # Not sure I like adding all this info to every chunk
182+ # maybe move last chunk?
183+ validator_logs = iteration .validator_logs
180184 for res in gen :
181185 chunk = res .chunk
182186 original_text = res .original_text
@@ -195,13 +199,19 @@ def prepare_chunk_generator(stream) -> Iterable[Tuple[Any, bool]]:
195199 )
196200 # 5. Convert validated fragment to a pretty JSON string
197201 validation_response += cast (str , chunk )
202+ validator_logs = call_log .iterations .last .validator_logs
203+
204+ validation_summaries = ValidationSummary .from_validator_logs (
205+ validator_logs
206+ )
198207 passed = call_log .status == pass_status
199208 yield ValidationOutcome (
200209 call_id = call_log .id , # type: ignore
201210 # The chunk or the whole output?
202211 raw_llm_output = original_text ,
203212 validated_output = chunk ,
204213 validation_passed = passed ,
214+ validation_summaries = validation_summaries ,
205215 )
206216
207217 # handle non string schema
@@ -246,11 +256,17 @@ def prepare_chunk_generator(stream) -> Iterable[Tuple[Any, bool]]:
246256 else :
247257 validation_response = cast (dict , validated_fragment )
248258 # 5. Convert validated fragment to a pretty JSON string
259+
260+ validator_logs = iteration .validator_logs
261+ validation_summaries = ValidationSummary .from_validator_logs (
262+ validator_logs
263+ )
249264 yield ValidationOutcome (
250265 call_id = call_log .id , # type: ignore
251266 raw_llm_output = fragment ,
252267 validated_output = validated_fragment ,
253268 validation_passed = validated_fragment is not None ,
269+ validation_summaries = validation_summaries ,
254270 )
255271
256272 # # Finally, add to logs
0 commit comments